Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ from sklearn.preprocessing import LabelEncoder
|
|
| 5 |
from sklearn.linear_model import LogisticRegression
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.pipeline import Pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# 1. Laden und Vorbereiten des Datensatzes (einmalig beim Start)
|
| 10 |
try:
|
|
@@ -17,10 +21,12 @@ try:
|
|
| 17 |
train_texts, test_texts, train_labels, test_labels = train_test_split(
|
| 18 |
texts, numerical_labels, test_size=0.2, random_state=42, stratify=numerical_labels
|
| 19 |
)
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
print(f"Fehler beim Laden des Datensatzes: {e}")
|
| 22 |
label_names = ["Fehler beim Laden"]
|
| 23 |
pipeline = None
|
|
|
|
| 24 |
|
| 25 |
# 2. Trainieren des Modells (einmalig beim Start)
|
| 26 |
if 'pipeline' not in locals() or pipeline is None:
|
|
@@ -30,39 +36,125 @@ if 'pipeline' not in locals() or pipeline is None:
|
|
| 30 |
('classifier', LogisticRegression(solver='liblinear', multi_class='ovr', random_state=42))
|
| 31 |
])
|
| 32 |
pipeline.fit(train_texts, train_labels)
|
| 33 |
-
print("Modell erfolgreich trainiert.")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Fehler beim Trainieren des Modells: {e}")
|
| 36 |
pipeline = None
|
|
|
|
| 37 |
|
| 38 |
# 3. Funktion für die Vorhersage
|
| 39 |
def predict_intent(text):
|
| 40 |
if pipeline is not None and label_names:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
return "Fehler", {"Fehler": "Modell nicht geladen oder trainiert."}
|
| 48 |
|
| 49 |
# 4. Erstellen der Gradio Interface
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=predict_intent,
|
| 52 |
-
inputs=gr.Textbox(label="Gib deine Kundenanfrage ein:"),
|
| 53 |
outputs=[
|
| 54 |
gr.Label(label="Vorhergesagte Kundenintention:"),
|
| 55 |
gr.JSON(label="Konfidenzwerte:")
|
| 56 |
],
|
| 57 |
-
title
|
| 58 |
-
|
|
|
|
| 59 |
examples=[
|
| 60 |
["Ich habe mein Passwort vergessen."],
|
| 61 |
["Wie kann ich Geld überweisen?"],
|
| 62 |
["Meine Karte ist verloren gegangen."],
|
| 63 |
["Was ist der aktuelle Zinssatz für ein Sparkonto?"]
|
| 64 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
# 5. Starten der Gradio App (wird beim Ausführen des Skripts aktiv)
|
| 68 |
-
iface.launch(share=
|
|
|
|
| 5 |
from sklearn.linear_model import LogisticRegression
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.pipeline import Pipeline
|
| 8 |
+
import warnings
|
| 9 |
+
|
| 10 |
+
# Filtert die UserWarning von scikit-learn wegen der fehlenden Stichhaltigkeit von Klassen im Klassifikationsbericht heraus
|
| 11 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 12 |
|
| 13 |
# 1. Laden und Vorbereiten des Datensatzes (einmalig beim Start)
|
| 14 |
try:
|
|
|
|
| 21 |
train_texts, test_texts, train_labels, test_labels = train_test_split(
|
| 22 |
texts, numerical_labels, test_size=0.2, random_state=42, stratify=numerical_labels
|
| 23 |
)
|
| 24 |
+
print("Datensatz 'banking77' erfolgreich geladen.") # Ausgabe zur Bestätigung
|
| 25 |
except Exception as e:
|
| 26 |
print(f"Fehler beim Laden des Datensatzes: {e}")
|
| 27 |
label_names = ["Fehler beim Laden"]
|
| 28 |
pipeline = None
|
| 29 |
+
print("Modell wird nicht trainiert, da der Datensatz nicht geladen werden konnte.") # Ausgabe zur Info
|
| 30 |
|
| 31 |
# 2. Trainieren des Modells (einmalig beim Start)
|
| 32 |
if 'pipeline' not in locals() or pipeline is None:
|
|
|
|
| 36 |
('classifier', LogisticRegression(solver='liblinear', multi_class='ovr', random_state=42))
|
| 37 |
])
|
| 38 |
pipeline.fit(train_texts, train_labels)
|
| 39 |
+
print("Modell erfolgreich trainiert.") # Ausgabe zur Bestätigung
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Fehler beim Trainieren des Modells: {e}")
|
| 42 |
pipeline = None
|
| 43 |
+
print("Modell konnte nicht trainiert werden.") # Ausgabe zur Info
|
| 44 |
|
| 45 |
# 3. Funktion für die Vorhersage
|
| 46 |
def predict_intent(text):
|
| 47 |
if pipeline is not None and label_names:
|
| 48 |
+
try:
|
| 49 |
+
prediction = pipeline.predict([text])[0]
|
| 50 |
+
predicted_label = label_names[prediction]
|
| 51 |
+
probabilities = pipeline.predict_proba([text])[0]
|
| 52 |
+
confidences = {label_names[i]: f"{probabilities[i]:.2f}" for i in range(len(label_names))}
|
| 53 |
+
return predicted_label, confidences
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return "Fehler bei der Vorhersage", {"Fehler": f"Ein Fehler ist bei der Vorhersage aufgetreten: {e}"}
|
| 56 |
else:
|
| 57 |
return "Fehler", {"Fehler": "Modell nicht geladen oder trainiert."}
|
| 58 |
|
| 59 |
# 4. Erstellen der Gradio Interface
|
| 60 |
iface = gr.Interface(
|
| 61 |
fn=predict_intent,
|
| 62 |
+
inputs=gr.Textbox(label="Gib deine Kundenanfrage ein:", placeholder="z.B. Ich habe mein Passwort vergessen."),
|
| 63 |
outputs=[
|
| 64 |
gr.Label(label="Vorhergesagte Kundenintention:"),
|
| 65 |
gr.JSON(label="Konfidenzwerte:")
|
| 66 |
],
|
| 67 |
+
# title und description auf Deutsch
|
| 68 |
+
title="KI-gestützte Vorhersage von Kundenanfragen",
|
| 69 |
+
description="Diese Anwendung sagt die Absicht einer Kundenanfrage voraus. Gib eine Anfrage ein, um die vorhergesagte Kategorie und die Konfidenzwerte zu sehen. Das Modell wurde auf dem Datensatz Banking77 trainiert.",
|
| 70 |
examples=[
|
| 71 |
["Ich habe mein Passwort vergessen."],
|
| 72 |
["Wie kann ich Geld überweisen?"],
|
| 73 |
["Meine Karte ist verloren gegangen."],
|
| 74 |
["Was ist der aktuelle Zinssatz für ein Sparkonto?"]
|
| 75 |
+
],
|
| 76 |
+
css="""
|
| 77 |
+
.container {
|
| 78 |
+
margin: 0 auto;
|
| 79 |
+
max-width: 700px;
|
| 80 |
+
padding: 20px;
|
| 81 |
+
text-align: center;
|
| 82 |
+
}
|
| 83 |
+
.input_output_section {
|
| 84 |
+
display: flex;
|
| 85 |
+
flex-direction: column;
|
| 86 |
+
align-items: center;
|
| 87 |
+
margin-bottom: 20px;
|
| 88 |
+
}
|
| 89 |
+
.label {
|
| 90 |
+
font-weight: bold;
|
| 91 |
+
margin-bottom: 5px;
|
| 92 |
+
color: #4a5568; /* Dunkleres Grau für bessere Lesbarkeit */
|
| 93 |
+
}
|
| 94 |
+
.textbox {
|
| 95 |
+
border: 1px solid #cbd5e0; /* Etwas hellerer Rahmen */
|
| 96 |
+
border-radius: 0.375rem; /* Abgerundete Ecken gemäß Tailwind */
|
| 97 |
+
padding: 0.75rem;
|
| 98 |
+
width: 100%;
|
| 99 |
+
max-width: 400px; /* Begrenze die Breite des Textfelds */
|
| 100 |
+
margin-bottom: 1rem;
|
| 101 |
+
font-size: 1rem;
|
| 102 |
+
box-shadow: inset 0 2px 4px rgba(0,0,0,0.06); /* Subtiler Schatten */
|
| 103 |
+
transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out; /* Sanfte Übergänge */
|
| 104 |
+
}
|
| 105 |
+
.textbox:focus {
|
| 106 |
+
outline: none;
|
| 107 |
+
border-color: #3182ce; /* Blauer Fokus-Rand */
|
| 108 |
+
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.16); /* Heller Fokus-Schatten */
|
| 109 |
+
}
|
| 110 |
+
.label_output {
|
| 111 |
+
font-size: 1.25rem;
|
| 112 |
+
font-weight: 600;
|
| 113 |
+
color: #2d3748; /* Noch dunkler für die Ausgabe */
|
| 114 |
+
margin-bottom: 1.5rem;
|
| 115 |
+
padding: 0.5rem;
|
| 116 |
+
border-radius: 0.375rem;
|
| 117 |
+
background-color: #edf2f7; /* Sehr helles Grau für Hintergrund der Ausgabe */
|
| 118 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.08); /* Sehr schwacher Schatten */
|
| 119 |
+
min-width: 200px; /* Mindestbreite für die Ausgabe */
|
| 120 |
+
text-align: center;
|
| 121 |
+
}
|
| 122 |
+
.json_output {
|
| 123 |
+
background-color: #f7fafc; /* Noch helleres Grau für JSON */
|
| 124 |
+
border: 1px solid #e2e8f0;
|
| 125 |
+
border-radius: 0.375rem;
|
| 126 |
+
padding: 1rem;
|
| 127 |
+
font-family: 'Menlo', monospace; /* Monospace-Schriftart für JSON */
|
| 128 |
+
font-size: 0.875rem;
|
| 129 |
+
line-height: 1.5rem;
|
| 130 |
+
overflow-x: auto; /* Horizontal scrollbar bei Überlauf */
|
| 131 |
+
max-width: 400px; /* Maximale Breite */
|
| 132 |
+
margin: 0 auto; /* Zentrieren */
|
| 133 |
+
}
|
| 134 |
+
.examples {
|
| 135 |
+
margin-top: 2rem;
|
| 136 |
+
text-align: center;
|
| 137 |
+
}
|
| 138 |
+
.example_item {
|
| 139 |
+
cursor: pointer;
|
| 140 |
+
padding: 0.5rem 1rem;
|
| 141 |
+
margin: 0.5rem;
|
| 142 |
+
background-color: #e2e8f0; /* Hellgrauer Hintergrund für Beispiele */
|
| 143 |
+
color: #2d3748;
|
| 144 |
+
border-radius: 0.375rem;
|
| 145 |
+
border: 1px solid #f0f4f8;
|
| 146 |
+
transition: background-color 0.2s ease-in-out, transform 0.1s ease;
|
| 147 |
+
display: inline-block; /* Damit die Breite automatisch angepasst wird */
|
| 148 |
+
font-size: 0.9rem;
|
| 149 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
|
| 150 |
+
}
|
| 151 |
+
.example_item:hover {
|
| 152 |
+
background-color: #cbd5e0; /* Dunkleres Grau bei Hover */
|
| 153 |
+
transform: translateY(-2px); /* Leichter Hover-Effekt */
|
| 154 |
+
border-color: #a0aec0;
|
| 155 |
+
}
|
| 156 |
+
""",
|
| 157 |
)
|
| 158 |
|
| 159 |
# 5. Starten der Gradio App (wird beim Ausführen des Skripts aktiv)
|
| 160 |
+
iface.launch(share=True)
|