Spaces:
Paused
Paused
fix: ?dateien=1 filter time (single NER pass, 420 s timeout, clearer errors)
Browse files- iserv-filter/nerfilter.py +1 -1
- iserv-filter/taskfilter.py +23 -16
- src/api/iserv-filtered.js +4 -4
iserv-filter/nerfilter.py
CHANGED
|
@@ -114,7 +114,7 @@ def _spans(text):
|
|
| 114 |
|
| 115 |
|
| 116 |
def filter_text(text: str, stats: Counter | None = None) -> str:
|
| 117 |
-
if not text:
|
| 118 |
return ""
|
| 119 |
merged = [] # [start, end, label]
|
| 120 |
for s, e, lab in _spans(text):
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
def filter_text(text: str, stats: Counter | None = None) -> str:
|
| 117 |
+
if not text or not text.strip(): # the pipeline fails on blank input
|
| 118 |
return ""
|
| 119 |
merged = [] # [start, end, label]
|
| 120 |
for s, e, lab in _spans(text):
|
iserv-filter/taskfilter.py
CHANGED
|
@@ -101,26 +101,31 @@ def _pdf_clean(data):
|
|
| 101 |
return out.getvalue()
|
| 102 |
|
| 103 |
|
| 104 |
-
def _file_out(ext, data, text):
|
| 105 |
-
"""{mime, base64} of the original file if nothing in it is a name
|
| 106 |
-
|
|
|
|
| 107 |
if ext not in _MIME or not text or len(text) > READ_LIMIT:
|
| 108 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
if ext == "docx":
|
| 111 |
checked = _docx_checked(data)
|
| 112 |
if checked is None:
|
| 113 |
-
return None
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
elif ext == "pdf":
|
| 116 |
data = _pdf_clean(data)
|
| 117 |
-
found = Counter()
|
| 118 |
-
filter_text(text, found)
|
| 119 |
-
if found["replacements"]:
|
| 120 |
-
return None
|
| 121 |
except Exception:
|
| 122 |
-
return None
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
def _unchecked(out, att, fid, release):
|
|
@@ -161,15 +166,17 @@ def describe_attachment(att, stats=None, files=False, fid=None, release=()):
|
|
| 161 |
if not text:
|
| 162 |
out["hinweis"] = NOT_READABLE
|
| 163 |
return _unchecked(out, att, fid, release) if files else out
|
|
|
|
| 164 |
if files:
|
| 165 |
-
datei = _file_out(ext, att["data"], text)
|
| 166 |
if datei:
|
| 167 |
out["datei"] = datei
|
| 168 |
else:
|
| 169 |
_unchecked(out, att, fid, release)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
|
| 175 |
def filter_task(task, stats: Counter | None = None, files=False, release=()):
|
|
|
|
| 101 |
return out.getvalue()
|
| 102 |
|
| 103 |
|
| 104 |
+
def _file_out(ext, data, text, stats=None):
|
| 105 |
+
"""(datei, filtered): {mime, base64} of the original file if nothing in it is a name (else None), and the filtered
|
| 106 |
+
text, so the NER reads the file only once. Only what the NER can read is checked, so scans, images and audio never
|
| 107 |
+
go out; a file cut at READ_LIMIT was not read completely (filtered is None: the caller filters the part it shows)."""
|
| 108 |
if ext not in _MIME or not text or len(text) > READ_LIMIT:
|
| 109 |
+
return None, None
|
| 110 |
+
found = Counter()
|
| 111 |
+
filtered = filter_text(text, found)
|
| 112 |
+
if stats is not None:
|
| 113 |
+
stats["replacements"] += found["replacements"]
|
| 114 |
try:
|
| 115 |
if ext == "docx":
|
| 116 |
checked = _docx_checked(data)
|
| 117 |
if checked is None:
|
| 118 |
+
return None, filtered
|
| 119 |
+
if checked[0].strip():
|
| 120 |
+
filter_text(checked[0], found)
|
| 121 |
+
data = checked[1]
|
| 122 |
elif ext == "pdf":
|
| 123 |
data = _pdf_clean(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
except Exception:
|
| 125 |
+
return None, filtered
|
| 126 |
+
if found["replacements"]:
|
| 127 |
+
return None, filtered
|
| 128 |
+
return {"mime": _MIME[ext], "base64": base64.b64encode(data).decode()}, filtered
|
| 129 |
|
| 130 |
|
| 131 |
def _unchecked(out, att, fid, release):
|
|
|
|
| 166 |
if not text:
|
| 167 |
out["hinweis"] = NOT_READABLE
|
| 168 |
return _unchecked(out, att, fid, release) if files else out
|
| 169 |
+
filtered = None
|
| 170 |
if files:
|
| 171 |
+
datei, filtered = _file_out(ext, att["data"], text, stats)
|
| 172 |
if datei:
|
| 173 |
out["datei"] = datei
|
| 174 |
else:
|
| 175 |
_unchecked(out, att, fid, release)
|
| 176 |
+
shown = filtered if filtered is not None else text
|
| 177 |
+
if len(shown) > MAX_TEXT: # cut at a space so no name is split in half
|
| 178 |
+
shown, out["gekuerzt"] = shown[:MAX_TEXT].rsplit(None, 1)[0], True
|
| 179 |
+
return {**out, "text": shown if filtered is not None else filter_text(shown, stats)}
|
| 180 |
|
| 181 |
|
| 182 |
def filter_task(task, stats: Counter | None = None, files=False, release=()):
|
src/api/iserv-filtered.js
CHANGED
|
@@ -158,11 +158,11 @@ async function cleanTasks(includeDone, withFiles, released) {
|
|
| 158 |
method: 'POST',
|
| 159 |
headers: { 'Content-Type': 'application/json', 'X-App-Token': process.env.API_BEARER_TOKEN },
|
| 160 |
body: JSON.stringify(raw),
|
| 161 |
-
signal: AbortSignal.timeout(
|
| 162 |
});
|
| 163 |
-
return { status: res.ok ? 200 : 502, body: res.ok ? await res.json() : { error: 'filter' } };
|
| 164 |
-
} catch {
|
| 165 |
-
return { status: 502, body: { error: 'filter' } };
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
|
|
|
| 158 |
method: 'POST',
|
| 159 |
headers: { 'Content-Type': 'application/json', 'X-App-Token': process.env.API_BEARER_TOKEN },
|
| 160 |
body: JSON.stringify(raw),
|
| 161 |
+
signal: AbortSignal.timeout(420_000), // with ?dateien=1 the filter reads every file completely
|
| 162 |
});
|
| 163 |
+
return { status: res.ok ? 200 : 502, body: res.ok ? await res.json() : { error: 'filter', status: res.status } };
|
| 164 |
+
} catch (e) {
|
| 165 |
+
return { status: 502, body: { error: e.name === 'TimeoutError' ? 'filter_timeout' : 'filter' } };
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|