Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from docling.datamodel.pipeline_options import (
|
|
| 7 |
)
|
| 8 |
import spaces
|
| 9 |
from docling.datamodel.base_models import InputFormat
|
| 10 |
-
from marker.converters.
|
| 11 |
from marker.models import create_model_dict
|
| 12 |
from marker.output import text_from_rendered
|
| 13 |
from wordfreq import word_frequency
|
|
@@ -23,7 +23,6 @@ import time
|
|
| 23 |
_data = requests.get("https://specialist-it.de/data.json").json()
|
| 24 |
meine_orte = _data.get("meine_orte", [])
|
| 25 |
|
| 26 |
-
|
| 27 |
dummy = """
|
| 28 |
___|___|___|___|___|___|___|___|___|___|___|___|__
|
| 29 |
___|__<(-^,^-)=b_|___|___|___|___|___|___|___|___|
|
|
@@ -38,7 +37,7 @@ _|___|___|___|__d-(^_^)z_|___|___|___|___|___|___|
|
|
| 38 |
DUMMYIMAGE
|
| 39 |
"""
|
| 40 |
|
| 41 |
-
bad_string ="""<span style="color: red; text-decoration: underline;" title="Möglicher Tippfehler gefunden. (Vorschläge: Page, Lage, Tage)">page</span>"""
|
| 42 |
|
| 43 |
css = """
|
| 44 |
#spinner_md.pending::before {
|
|
@@ -47,7 +46,7 @@ css = """
|
|
| 47 |
width: 20px;
|
| 48 |
height: 20px;
|
| 49 |
border: 3px solid #eee;
|
| 50 |
-
border-top-color: #2563eb;
|
| 51 |
border-radius: 50%;
|
| 52 |
animation: spin 1s linear infinite;
|
| 53 |
margin-right: 10px;
|
|
@@ -63,14 +62,13 @@ css = """
|
|
| 63 |
font-weight: bold;
|
| 64 |
color: #2563eb;
|
| 65 |
vertical-align: middle;
|
| 66 |
-
|
| 67 |
"""
|
| 68 |
|
| 69 |
tool = language_tool_python.LanguageTool('de-DE', new_spellings=meine_orte)
|
| 70 |
|
| 71 |
def replace_markdown_images(text):
|
| 72 |
pattern = r'!\[\]\(_page_\d+_Picture_\d+\.jpeg\)'
|
| 73 |
-
replacement = '<img src="dummy.svg">'
|
| 74 |
return re.sub(pattern, dummy, text)
|
| 75 |
|
| 76 |
def remove_hashes(text):
|
|
@@ -95,17 +93,13 @@ docling_converter = DocumentConverter(
|
|
| 95 |
}
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# Marker
|
| 99 |
-
marker_converter =
|
| 100 |
-
|
| 101 |
-
artifact_dict=create_model_dict()
|
| 102 |
-
)
|
| 103 |
|
| 104 |
def check_spelling(text_input: str) -> str:
|
| 105 |
-
"""Prüft den Text mit LanguageTool und markiert Fundstellen in HTML."""
|
| 106 |
-
|
| 107 |
IGNORE_RULES = {'DE_CASE'}
|
| 108 |
-
|
| 109 |
if tool is None:
|
| 110 |
return "❌ **Fehler:** LanguageTool konnte nicht geladen werden."
|
| 111 |
text = text_input or ""
|
|
@@ -113,19 +107,14 @@ def check_spelling(text_input: str) -> str:
|
|
| 113 |
return "*Bitte lade eine Datei hoch oder füge Text ein...*"
|
| 114 |
if text.startswith("❌") or text.startswith("Fehler"):
|
| 115 |
return text
|
| 116 |
-
|
| 117 |
matches = tool.check(text)
|
| 118 |
-
|
| 119 |
-
# Regel-Filter
|
| 120 |
matches = [m for m in matches if m.rule_id not in IGNORE_RULES]
|
| 121 |
-
|
| 122 |
-
# Englische Wörter herausfiltern
|
| 123 |
matches = _filter_english_words(text, matches)
|
| 124 |
-
|
| 125 |
if not matches:
|
| 126 |
return "<p style='color: green;'>✅ Keine Fehler gefunden!</p>"
|
| 127 |
-
|
| 128 |
-
# HTML-Aufbau für die Anzeige der Fehler
|
| 129 |
parts = []
|
| 130 |
text_len = len(text)
|
| 131 |
last_idx = text_len
|
|
@@ -151,7 +140,6 @@ def check_spelling(text_input: str) -> str:
|
|
| 151 |
|
| 152 |
|
| 153 |
def _filter_english_words(text: str, matches: list) -> list:
|
| 154 |
-
"""Filtert Treffer heraus, die gültige englische Wörter sind."""
|
| 155 |
clean_matches = []
|
| 156 |
for m in matches:
|
| 157 |
if m.rule_id == 'GERMAN_SPELLER_RULE':
|
|
@@ -161,19 +149,16 @@ def _filter_english_words(text: str, matches: list) -> list:
|
|
| 161 |
clean_matches.append(m)
|
| 162 |
return clean_matches
|
| 163 |
|
|
|
|
| 164 |
def convert_document(file):
|
| 165 |
-
# load json from specialist-it.de/data.json and convert to python list. Name of python list is meine_orte.
|
| 166 |
_data = requests.get("https://specialist-it.de/data.json").json()
|
| 167 |
meine_orte = _data.get("meine_orte", [])
|
| 168 |
-
rendered = marker_converter(file.name)
|
| 169 |
-
# Extrahiert den Text aus dem gerenderten Objekt
|
| 170 |
text, _, _ = text_from_rendered(rendered)
|
| 171 |
-
|
| 172 |
return check_spelling(text).replace(bad_string, "")
|
| 173 |
|
| 174 |
|
| 175 |
def add_ort(neuer_ort: str) -> str:
|
| 176 |
-
"""Sendet einen neuen Ort per POST an das PHP-Backend."""
|
| 177 |
neuer_ort = neuer_ort.strip()
|
| 178 |
if not neuer_ort:
|
| 179 |
return "⚠️ Bitte einen Ort eingeben."
|
|
@@ -195,7 +180,6 @@ def add_ort(neuer_ort: str) -> str:
|
|
| 195 |
|
| 196 |
|
| 197 |
with gr.Blocks() as app:
|
| 198 |
-
|
| 199 |
gr.Markdown("# Language Tool \n")
|
| 200 |
output_text = gr.HTML(label="Ergebnis", elem_id="spinner_md")
|
| 201 |
file_input = gr.File(label="PDF hochladen", file_types=[".pdf"])
|
|
@@ -225,4 +209,4 @@ with gr.Blocks() as app:
|
|
| 225 |
outputs=[add_status]
|
| 226 |
)
|
| 227 |
|
| 228 |
-
app.launch(debug=True, show_error=True, css=css)
|
|
|
|
| 7 |
)
|
| 8 |
import spaces
|
| 9 |
from docling.datamodel.base_models import InputFormat
|
| 10 |
+
from marker.converters.pdf import PdfConverter # ✅ FIXED import
|
| 11 |
from marker.models import create_model_dict
|
| 12 |
from marker.output import text_from_rendered
|
| 13 |
from wordfreq import word_frequency
|
|
|
|
| 23 |
_data = requests.get("https://specialist-it.de/data.json").json()
|
| 24 |
meine_orte = _data.get("meine_orte", [])
|
| 25 |
|
|
|
|
| 26 |
dummy = """
|
| 27 |
___|___|___|___|___|___|___|___|___|___|___|___|__
|
| 28 |
___|__<(-^,^-)=b_|___|___|___|___|___|___|___|___|
|
|
|
|
| 37 |
DUMMYIMAGE
|
| 38 |
"""
|
| 39 |
|
| 40 |
+
bad_string = """<span style="color: red; text-decoration: underline;" title="Möglicher Tippfehler gefunden. (Vorschläge: Page, Lage, Tage)">page</span>"""
|
| 41 |
|
| 42 |
css = """
|
| 43 |
#spinner_md.pending::before {
|
|
|
|
| 46 |
width: 20px;
|
| 47 |
height: 20px;
|
| 48 |
border: 3px solid #eee;
|
| 49 |
+
border-top-color: #2563eb;
|
| 50 |
border-radius: 50%;
|
| 51 |
animation: spin 1s linear infinite;
|
| 52 |
margin-right: 10px;
|
|
|
|
| 62 |
font-weight: bold;
|
| 63 |
color: #2563eb;
|
| 64 |
vertical-align: middle;
|
| 65 |
+
}
|
| 66 |
"""
|
| 67 |
|
| 68 |
tool = language_tool_python.LanguageTool('de-DE', new_spellings=meine_orte)
|
| 69 |
|
| 70 |
def replace_markdown_images(text):
|
| 71 |
pattern = r'!\[\]\(_page_\d+_Picture_\d+\.jpeg\)'
|
|
|
|
| 72 |
return re.sub(pattern, dummy, text)
|
| 73 |
|
| 74 |
def remove_hashes(text):
|
|
|
|
| 93 |
}
|
| 94 |
)
|
| 95 |
|
| 96 |
+
# Marker — ✅ FIXED: PdfConverter replaces MarkerConverter
|
| 97 |
+
marker_converter = PdfConverter(artifact_dict=create_model_dict())
|
| 98 |
+
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def check_spelling(text_input: str) -> str:
|
|
|
|
|
|
|
| 101 |
IGNORE_RULES = {'DE_CASE'}
|
| 102 |
+
|
| 103 |
if tool is None:
|
| 104 |
return "❌ **Fehler:** LanguageTool konnte nicht geladen werden."
|
| 105 |
text = text_input or ""
|
|
|
|
| 107 |
return "*Bitte lade eine Datei hoch oder füge Text ein...*"
|
| 108 |
if text.startswith("❌") or text.startswith("Fehler"):
|
| 109 |
return text
|
| 110 |
+
|
| 111 |
matches = tool.check(text)
|
|
|
|
|
|
|
| 112 |
matches = [m for m in matches if m.rule_id not in IGNORE_RULES]
|
|
|
|
|
|
|
| 113 |
matches = _filter_english_words(text, matches)
|
| 114 |
+
|
| 115 |
if not matches:
|
| 116 |
return "<p style='color: green;'>✅ Keine Fehler gefunden!</p>"
|
| 117 |
+
|
|
|
|
| 118 |
parts = []
|
| 119 |
text_len = len(text)
|
| 120 |
last_idx = text_len
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
def _filter_english_words(text: str, matches: list) -> list:
|
|
|
|
| 143 |
clean_matches = []
|
| 144 |
for m in matches:
|
| 145 |
if m.rule_id == 'GERMAN_SPELLER_RULE':
|
|
|
|
| 149 |
clean_matches.append(m)
|
| 150 |
return clean_matches
|
| 151 |
|
| 152 |
+
|
| 153 |
def convert_document(file):
|
|
|
|
| 154 |
_data = requests.get("https://specialist-it.de/data.json").json()
|
| 155 |
meine_orte = _data.get("meine_orte", [])
|
| 156 |
+
rendered = marker_converter(file.name) # ✅ same call, new converter
|
|
|
|
| 157 |
text, _, _ = text_from_rendered(rendered)
|
|
|
|
| 158 |
return check_spelling(text).replace(bad_string, "")
|
| 159 |
|
| 160 |
|
| 161 |
def add_ort(neuer_ort: str) -> str:
|
|
|
|
| 162 |
neuer_ort = neuer_ort.strip()
|
| 163 |
if not neuer_ort:
|
| 164 |
return "⚠️ Bitte einen Ort eingeben."
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
with gr.Blocks() as app:
|
|
|
|
| 183 |
gr.Markdown("# Language Tool \n")
|
| 184 |
output_text = gr.HTML(label="Ergebnis", elem_id="spinner_md")
|
| 185 |
file_input = gr.File(label="PDF hochladen", file_types=[".pdf"])
|
|
|
|
| 209 |
outputs=[add_status]
|
| 210 |
)
|
| 211 |
|
| 212 |
+
app.launch(debug=True, show_error=True, css=css)
|