Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
-
result = converter.convert(pdf_file.name)
|
| 53 |
-
extracted_text = result.document.export_to_markdown()
|
| 54 |
-
return extracted_text.replace(bad_string, "")
|
| 55 |
-
except Exception as e:
|
| 56 |
-
return f"Fehler bei der Verarbeitung: {str(e)}"
|
| 57 |
-
|
| 58 |
-
def process_text(text_input):
|
| 59 |
-
"""Verarbeitet direkt eingegebenen Text und prüft die Rechtschreibung."""
|
| 60 |
-
if not text_input or not text_input.strip():
|
| 61 |
-
return "*Bitte Text eingeben...*"
|
| 62 |
-
return check_spelling(text_input)
|
| 63 |
-
|
| 64 |
-
def process_pdf_with_spelling(pdf_file):
|
| 65 |
-
"""Verarbeitet PDF und führt anschließend Rechtschreibprüfung durch."""
|
| 66 |
-
extracted = process_pdf(pdf_file)
|
| 67 |
-
if extracted is None or str(extracted).startswith("Fehler"):
|
| 68 |
-
return extracted
|
| 69 |
-
#return extracted
|
| 70 |
-
return check_spelling(extracted)
|
| 71 |
-
|
| 72 |
-
# Gradio Interface
|
| 73 |
-
with gr.Blocks(title="Smart PDF OCR (Column-Aware)") as demo:
|
| 74 |
-
gr.Markdown("# 📑 Smart PDF Layout-Aware OCR")
|
| 75 |
-
|
| 76 |
-
with gr.Tabs():
|
| 77 |
-
# --- Tab 1: PDF Upload ---
|
| 78 |
-
with gr.TabItem("📄 PDF hochladen"):
|
| 79 |
-
with gr.Row():
|
| 80 |
-
pdf_input = gr.File(label="PDF hochladen", file_types=[".pdf"])
|
| 81 |
-
|
| 82 |
-
with gr.Row():
|
| 83 |
-
text_output = gr.Markdown(label="Extrahierter Text (Markdown Format)")
|
| 84 |
-
|
| 85 |
-
submit_btn = gr.Button("Dokument analysieren", variant="primary")
|
| 86 |
-
submit_btn.click(fn=process_pdf_with_spelling, inputs=pdf_input, outputs=text_output)
|
| 87 |
-
|
| 88 |
-
# --- Tab 2: Text direkt eingeben ---
|
| 89 |
-
with gr.TabItem("✏️ Text eingeben"):
|
| 90 |
-
with gr.Row():
|
| 91 |
-
text_input = gr.Textbox(
|
| 92 |
-
label="Text eingeben",
|
| 93 |
-
placeholder="Geben Sie hier Ihren deutschen Text ein...",
|
| 94 |
-
lines=10,
|
| 95 |
-
max_lines=30,
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
with gr.Row():
|
| 99 |
-
spell_output = gr.HTML(label="Rechtschreibprüfung")
|
| 100 |
-
|
| 101 |
-
with gr.Row():
|
| 102 |
-
check_btn = gr.Button("Rechtschreibung prüfen", variant="primary")
|
| 103 |
-
clear_btn = gr.Button("Leeren", variant="secondary")
|
| 104 |
-
|
| 105 |
-
check_btn.click(fn=process_text, inputs=text_input, outputs=spell_output)
|
| 106 |
-
clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[text_input, spell_output])
|
| 107 |
-
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from spellchecker import SpellChecker
|
| 3 |
+
|
| 4 |
+
# 1. Logik: Rechtschreibprüfung initialisieren (Deutsch)
|
| 5 |
+
spell = SpellChecker(language='de')
|
| 6 |
+
|
| 7 |
+
def process_text(text):
|
| 8 |
+
if not text or text.strip() == "":
|
| 9 |
+
return "Bitte gib einen Text ein."
|
| 10 |
+
|
| 11 |
+
words = text.split()
|
| 12 |
+
misspelled = spell.unknown([w.strip('.,!?:;') for w in words])
|
| 13 |
+
|
| 14 |
+
corrected_words = []
|
| 15 |
+
for word in words:
|
| 16 |
+
clean = word.strip('.,!?:;')
|
| 17 |
+
# Wenn das Wort falsch geschrieben ist, Vorschlag in Klammern zeigen
|
| 18 |
+
if clean.lower() in [m.lower() for m in misspelled]:
|
| 19 |
+
correction = spell.correction(clean)
|
| 20 |
+
corrected_words.append(f"❌ {word} ({correction}?)")
|
| 21 |
+
else:
|
| 22 |
+
corrected_words.append(word)
|
| 23 |
+
|
| 24 |
+
return " ".join(corrected_words)
|
| 25 |
+
|
| 26 |
+
# 2. UI: Gradio Interface Aufbau
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("# 📝 Deutsche Rechtschreibprüfung")
|
| 29 |
+
|
| 30 |
+
with gr.Column():
|
| 31 |
+
text_input = gr.Textbox(lines=5, label="Dein Text", placeholder="Schreibe hier etwas auf Deutsch...")
|
| 32 |
+
spell_output = gr.Textbox(label="Ergebnis / Korrekturvorschläge")
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
check_btn = gr.Button("Rechtschreibung prüfen", variant="primary")
|
| 36 |
+
clear_btn = gr.Button("Leeren", variant="secondary")
|
| 37 |
+
|
| 38 |
+
# 3. Events: Verknüpfung der Buttons mit der Logik
|
| 39 |
+
check_btn.click(
|
| 40 |
+
fn=process_text,
|
| 41 |
+
inputs=text_input,
|
| 42 |
+
outputs=spell_output
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
clear_btn.click(
|
| 46 |
+
fn=lambda: ("", ""),
|
| 47 |
+
inputs=None,
|
| 48 |
+
outputs=[text_input, spell_output]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# App starten
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|