Spaces:
Runtime error
Runtime error
Kontextbasierte Antwortlogik integriert
Browse files
app.py
CHANGED
|
@@ -1,32 +1,16 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
try:
|
| 7 |
-
with open("data/sachkunde.md", "r", encoding="utf-8") as f:
|
| 8 |
-
return f.read()
|
| 9 |
-
except FileNotFoundError:
|
| 10 |
-
return "Das Sachkunde-Skript wurde nicht gefunden."
|
| 11 |
|
| 12 |
-
wissen = lade_wissen()
|
| 13 |
-
|
| 14 |
-
# Chat-Logik (einfacher Bot mit statischem Wissen)
|
| 15 |
def antwortverlauf(chatverlauf):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if "34a" in eingabe:
|
| 19 |
-
antwort = "§34a regelt die Sachkundeprüfung im Bewachungsgewerbe."
|
| 20 |
-
elif "hilfe" in eingabe:
|
| 21 |
-
antwort = "Klar helfe ich dir! Frag mich einfach, ich bin da. 😊"
|
| 22 |
-
elif "datenschutz" in eingabe:
|
| 23 |
-
antwort = "Datenschutz ist zentral geregelt über DSGVO und BDSG. Willst du ein Beispiel?"
|
| 24 |
-
else:
|
| 25 |
-
antwort = "Ich bin mir nicht sicher, was du meinst – kannst du das anders formulieren?"
|
| 26 |
-
|
| 27 |
return chatverlauf + [{"role": "assistant", "content": antwort}]
|
| 28 |
|
| 29 |
-
demo = gr.ChatInterface(fn=antwortverlauf,
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from utils import lade_skript, teile_in_abschnitte, finde_passenden_abschnitt
|
| 3 |
|
| 4 |
+
text = lade_skript()
|
| 5 |
+
abschnitte = teile_in_abschnitte(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
def antwortverlauf(chatverlauf):
|
| 8 |
+
frage = chatverlauf[-1]["content"]
|
| 9 |
+
antwort = finde_passenden_abschnitt(frage, abschnitte)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return chatverlauf + [{"role": "assistant", "content": antwort}]
|
| 11 |
|
| 12 |
+
demo = gr.ChatInterface(fn=antwortverlauf,
|
| 13 |
+
title="§34a Lern-Dozent",
|
| 14 |
+
description="Ich antworte dir mit Wissen aus deinem IHK-Skript.")
|
| 15 |
|
| 16 |
demo.launch()
|
utils.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
def lade_skript(datei="data/sachkunde.md"):
|
| 4 |
+
with open(datei, "r", encoding="utf-8") as f:
|
| 5 |
+
return f.read()
|
| 6 |
+
|
| 7 |
+
def teile_in_abschnitte(text, max_länge=500):
|
| 8 |
+
roh = re.split(r"\n{2,}", text) # Trenne nach Leerzeilen
|
| 9 |
+
return [abschnitt.strip() for abschnitt in roh if len(abschnitt.strip()) > 30]
|
| 10 |
+
|
| 11 |
+
def finde_passenden_abschnitt(frage, abschnitte):
|
| 12 |
+
frage = frage.lower()
|
| 13 |
+
treffer = [a for a in abschnitte if any(w in a.lower() for w in frage.split())]
|
| 14 |
+
if treffer:
|
| 15 |
+
return max(treffer, key=lambda x: sum(w in x.lower() for w in frage.split()))
|
| 16 |
+
return "Ich konnte keine passende Stelle im Skript finden."
|