-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranskribus_api_client.py
More file actions
289 lines (245 loc) · 10.3 KB
/
Copy pathtranskribus_api_client.py
File metadata and controls
289 lines (245 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Transkribus API Client
-----------------------
Client for interacting with the Transkribus Legacy API using session-based authentication.
This client fetches additional document and page metadata that is not available in
the PageXML export, such as labels, tags, and excluded status.
"""
import logging
import requests
import xml.etree.ElementTree as ET
import xmltodict
from typing import Optional, Dict, Any
logger = logging.getLogger(__name__)
class TranskribusAPIClient:
"""Client for interacting with the Transkribus Legacy API."""
AUTH_URL = "https://transkribus.eu/TrpServer/rest/auth/login"
BASE_URL = "https://transkribus.eu/TrpServer/rest"
def __init__(self, username: str, password: str):
"""
Initialize the Transkribus API client.
Args:
username: Transkribus username
password: Transkribus password
"""
self.username = username
self.password = password
self.session_id: Optional[str] = None
def authenticate(self) -> bool:
"""
Authenticate with Transkribus and obtain session ID.
Returns:
True if authentication successful, False otherwise
"""
payload = {"user": self.username, "pw": self.password}
try:
response = requests.post(self.AUTH_URL, data=payload, timeout=30)
if response.status_code != 200:
logger.error(
f"Authentication failed with status {response.status_code}"
)
logger.error(f"Response: {response.text[:500]}")
return False
# Parse XML response
xml_response = xmltodict.parse(response.text)
self.session_id = xml_response.get("trpUserLogin", {}).get("sessionId")
if not self.session_id:
logger.error("No session ID in authentication response")
return False
logger.info("Successfully authenticated with Transkribus API")
return True
except requests.exceptions.RequestException as e:
logger.error(f"Failed to authenticate with Transkribus API: {e}")
if hasattr(e, "response") and e.response is not None:
logger.error(f"Response: {e.response.text}")
return False
except Exception as e:
logger.error(f"Error parsing authentication response: {e}")
return False
def _get_cookies(self) -> Dict[str, str]:
"""
Get cookies with session ID.
Returns:
Dictionary of cookies
Raises:
ValueError: If not authenticated
"""
if not self.session_id:
raise ValueError("Not authenticated. Call authenticate() first.")
return {"JSESSIONID": self.session_id}
def get_full_document(
self, collection_id: int, document_id: int
) -> Optional[Dict[str, Any]]:
"""
Get complete document information including all metadata.
Args:
collection_id: Collection ID
document_id: Document ID
Returns:
Full document data or None if request fails
"""
url = f"{self.BASE_URL}/collections/{collection_id}/{document_id}/fulldoc"
try:
response = requests.get(url, cookies=self._get_cookies(), timeout=30)
response.raise_for_status()
logger.debug(
f"Successfully fetched full document {document_id} from collection {collection_id}"
)
return response.json()
except requests.exceptions.RequestException as e:
logger.error(f"Failed to get full document {document_id}: {e}")
if hasattr(e, "response") and e.response is not None:
logger.error(f"Response: {e.response.text}")
return None
def extract_document_labels(self, doc_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Extract label and metadata information from document data.
Args:
doc_data: Document data from API
Returns:
Dictionary with extracted metadata including:
- labels: List of document-level labels
- page_labels_available: List of available page label types
- pages: Dictionary mapping page numbers to their labels
"""
result = {"labels": [], "page_labels_available": [], "pages": {}}
# Extract document-level labels
if "md" in doc_data and "labels" in doc_data["md"]:
result["labels"] = doc_data["md"]["labels"]
# Extract available page label types
if "md" in doc_data and "pageLabels" in doc_data["md"]:
result["page_labels_available"] = doc_data["md"]["pageLabels"]
# Extract page-specific labels
if "pageList" in doc_data and "pages" in doc_data["pageList"]:
for page in doc_data["pageList"]["pages"]:
page_nr = page.get("pageNr")
page_id = page.get("pageId")
page_labels = page.get("labels", [])
if page_id:
result["pages"][str(page_id)] = {
"page_nr": page_nr,
"labels": page_labels,
"is_excluded": any(
label.get("name", "").lower() == "exclude"
for label in page_labels
),
}
return result
def upload_page_transcript(
self,
collection_id: int,
doc_id: int,
page_nr: int,
xml_content: str,
status: str = "IN_PROGRESS",
note: str = "Updated via TWF",
) -> bool:
"""
Upload a modified PAGE XML transcript to Transkribus.
Endpoint: POST /collections/{collId}/{docId}/{pageNr}/text
Sends the raw XML as the request body with Content-Type: application/xml.
Returns True on success, False on failure.
"""
url = f"{self.BASE_URL}/collections/{collection_id}/{doc_id}/{page_nr}/text"
params = {"status": status, "note": note, "overwrite": "true"}
headers = {"Content-Type": "application/xml"}
try:
response = requests.post(
url,
params=params,
headers=headers,
data=xml_content.encode("utf-8"),
cookies=self._get_cookies(),
timeout=60,
)
if response.status_code == 200:
logger.info(
f"Successfully uploaded PAGE XML for page {page_nr} "
f"(doc {doc_id}, collection {collection_id})"
)
return True
logger.error(
f"Upload failed {response.status_code}: {response.text}"
)
return False
except requests.exceptions.RequestException as e:
logger.error(f"Error uploading transcript: {e}")
return False
def get_page_status(self, page_xml: str) -> Optional[str]:
"""
Extract the page processing status from a PAGE XML string.
Reads the status attribute from the <TranskribusMetadata> element,
which Transkribus uses to track workflow state (e.g. "IN_PROGRESS", "DONE", "GT").
Args:
page_xml: PAGE XML string as returned by get_page_xml()
Returns:
Status string, or None if the element/attribute is not found
"""
PAGE_NS = "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"
try:
root = ET.fromstring(page_xml)
# Try with the standard PAGE XML namespace first
metadata = root.find(f".//{{{PAGE_NS}}}TranskribusMetadata")
if metadata is None:
# Fall back to no-namespace search (some exports omit the namespace)
metadata = root.find(".//TranskribusMetadata")
if metadata is not None:
return metadata.get("status")
logger.warning("TranskribusMetadata element not found in PAGE XML")
return None
except ET.ParseError as e:
logger.error(f"Failed to parse PAGE XML when reading status: {e}")
return None
def get_page_xml(
self, collection_id: int, doc_id: int, page_nr: int
) -> Optional[str]:
"""
Download the latest PAGE XML transcript for a given page.
Fetches the transcript list for the page, then downloads the XML
from the URL of the most recent transcript.
Args:
collection_id: Collection ID
doc_id: Document ID
page_nr: Page number (1-based)
Returns:
PAGE XML string, or None if the request fails
"""
list_url = f"{self.BASE_URL}/collections/{collection_id}/{doc_id}/{page_nr}/list"
try:
response = requests.get(
list_url, cookies=self._get_cookies(), timeout=30
)
response.raise_for_status()
transcripts = response.json()
if not transcripts:
logger.error(f"No transcripts found for page {page_nr}")
return None
xml_url = transcripts[0].get("url")
if not xml_url:
logger.error("Transcript entry has no URL")
return None
xml_response = requests.get(xml_url, timeout=30)
xml_response.raise_for_status()
logger.info(
f"Downloaded PAGE XML for page {page_nr} "
f"(doc {doc_id}, collection {collection_id})"
)
return xml_response.text
except requests.exceptions.RequestException as e:
logger.error(f"Failed to get PAGE XML for page {page_nr}: {e}")
return None
def enrich_document_metadata(
self, collection_id: int, document_id: int
) -> Optional[Dict[str, Any]]:
"""
Fetch full document data and extract relevant metadata.
Args:
collection_id: Collection ID
document_id: Document ID
Returns:
Enriched metadata dictionary or None if request fails
"""
doc_data = self.get_full_document(collection_id, document_id)
if not doc_data:
return None
return self.extract_document_labels(doc_data)