Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +132 -59
src/streamlit_app.py
CHANGED
|
@@ -121,63 +121,140 @@ def evaluate_contexts(student_texts):
|
|
| 121 |
return scores
|
| 122 |
|
| 123 |
# --- STREAMLIT UI ---
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
st.title("
|
| 127 |
|
| 128 |
-
st.markdown("""
|
| 129 |
<div style="background-color: #262730; padding: 20px; border-radius: 10px; border: 1px solid #4B4B4B; margin-bottom: 20px;">
|
| 130 |
-
<h3>
|
| 131 |
-
<p>
|
| 132 |
-
|
| 133 |
-
<
|
| 134 |
-
<blockquote style="background-color: #3e3e3e; color: white;"><em>„Welche unterschiedlichen Bräuche, Symbole und Traditionen prägen das Weihnachtsfest in Deutschland, und welche regionalen Unterschiede gibt es dabei?“</em></blockquote>
|
| 135 |
</div>
|
| 136 |
-
""", unsafe_allow_html=True
|
| 137 |
-
)
|
| 138 |
|
| 139 |
-
with st.expander("
|
| 140 |
-
st.markdown(""
|
| 141 |
-
### 1. Deine Mission
|
| 142 |
-
Deine Aufgabe ist es, **Quellen auszuwählen**, die dem Elf helfen, eine **vollständige** und **richtige** Antwort zu geben. Du lernst dabei, wie man einer KI Kontext gibt (Prompt Engineering / RAG).
|
| 143 |
-
|
| 144 |
-
### 2. Werkzeugkiste: Suchstrategien
|
| 145 |
-
Der Elf akzeptiert nur **Wikipedia-Links**. Nutze Google clever, um die besten Artikel zu finden:
|
| 146 |
-
|
| 147 |
-
* **Der Basis-Trick:** Suche nur auf Wikipedia.
|
| 148 |
-
* `site:de.wikipedia.org Weihnachten`
|
| 149 |
-
* **Der Präzisions-Laser (Exakte Suche):** Finde genaue Phrasen.
|
| 150 |
-
* `"Weihnachtsbräuche in Norddeutschland"` (in Anführungszeichen!)
|
| 151 |
-
* **Der Inhalt-Scanner (Intext):** Suche Wörter, die im Text vorkommen müssen.
|
| 152 |
-
* `intext:Erzgebirge intext:Weihnachten`
|
| 153 |
-
* **Qualität vor Quantität:** Wähle max. 8 Artikel aus, die *wirklich* die Frage beantworten.
|
| 154 |
-
|
| 155 |
-
### 3. So bewertet der Elf dein Training
|
| 156 |
-
Sobald du die Links eingibst, prüft der Elf dein Trainingsmaterial auf drei Werte:
|
| 157 |
-
|
| 158 |
-
* 🧠 **Relevanz:** Passt der Artikel wirklich zum Thema "Weihnachten in Deutschland"? (Semantic Similarity)
|
| 159 |
-
* 📚 **Vollständigkeit:** Kennt der Elf danach die wichtigsten Begriffe (Adventskranz, Nikolaus, etc.)?
|
| 160 |
-
* 🌍 **Regionale Abdeckung:** Weiß der Elf, dass man in Bayern anders feiert als an der Ostsee?
|
| 161 |
-
""")
|
| 162 |
|
| 163 |
st.write("---")
|
| 164 |
-
st.subheader("
|
| 165 |
|
| 166 |
-
|
|
|
|
| 167 |
|
| 168 |
with st.form("training_form"):
|
| 169 |
urls = []
|
| 170 |
-
st.caption("
|
|
|
|
| 171 |
for i in range(num_links):
|
| 172 |
urls.append(st.text_input(f"Link {i + 1}", placeholder="https://de.wikipedia.org/wiki/..."))
|
| 173 |
|
| 174 |
-
submitted = st.form_submit_button("
|
| 175 |
|
| 176 |
if submitted:
|
| 177 |
student_texts = []
|
| 178 |
|
| 179 |
-
with st.spinner("
|
| 180 |
-
# Fetch content
|
| 181 |
for url in urls:
|
| 182 |
if url.strip():
|
| 183 |
content = fetch_wikipedia_content(url)
|
|
@@ -188,47 +265,43 @@ if submitted:
|
|
| 188 |
scores = evaluate_contexts(student_texts)
|
| 189 |
|
| 190 |
st.write("---")
|
| 191 |
-
st.subheader("
|
| 192 |
|
| 193 |
-
# Metrics Columns
|
| 194 |
col1, col2, col3 = st.columns(3)
|
| 195 |
-
col1.metric("
|
| 196 |
-
col2.metric("
|
| 197 |
-
col3.metric("
|
| 198 |
|
| 199 |
-
# Feedback Logic (Rewritten Tone)
|
| 200 |
feedback = []
|
| 201 |
|
| 202 |
# Relevance Feedback
|
| 203 |
if scores["relevance"] > 0.8:
|
| 204 |
-
feedback.append("
|
| 205 |
elif scores["relevance"] > 0.5:
|
| 206 |
-
feedback.append("
|
| 207 |
else:
|
| 208 |
-
feedback.append("
|
| 209 |
|
| 210 |
# Completeness Feedback
|
| 211 |
if scores["completeness"] > 0.8:
|
| 212 |
-
feedback.append("
|
| 213 |
elif scores["completeness"] > 0.5:
|
| 214 |
-
feedback.append("
|
| 215 |
else:
|
| 216 |
-
feedback.append("
|
| 217 |
|
| 218 |
# Regional Feedback
|
| 219 |
if scores["regional_completeness"] > 0.8:
|
| 220 |
-
feedback.append("
|
| 221 |
elif scores["regional_completeness"] > 0.5:
|
| 222 |
-
feedback.append("
|
| 223 |
else:
|
| 224 |
-
feedback.append("
|
| 225 |
|
| 226 |
-
# Display Feedback
|
| 227 |
st.info("\n\n".join(feedback))
|
| 228 |
|
| 229 |
-
|
| 230 |
-
if scores["relevance"] > 0.8 and scores["completeness"] > 0.9 and scores["regional_completeness"] > 0.8:
|
| 231 |
st.balloons()
|
| 232 |
-
st.success("
|
| 233 |
else:
|
| 234 |
-
st.error("
|
|
|
|
| 121 |
return scores
|
| 122 |
|
| 123 |
# --- STREAMLIT UI ---
|
| 124 |
+
TEXTS = {
|
| 125 |
+
"Deutsch": {
|
| 126 |
+
"page_title": "Weihnachts-Elf Trainer",
|
| 127 |
+
"main_title": "🎄 Weihnachts-Traditionen: Kontextbewertung für KI",
|
| 128 |
+
"task_header": "🎅 Aufgabe: Trainiere den Weihnachts-Elf",
|
| 129 |
+
"task_desc_1": "Wir bauen einen KI-Agenten, der Touristen Fragen zu Weihnachten in Deutschland beantwortet. Aber: <strong>Der Elf weiß nur das, was DU ihm zu lesen gibst!</strong>",
|
| 130 |
+
"task_desc_2": "Finde Wikipedia-Artikel, die diese Frage perfekt beantworten:",
|
| 131 |
+
"question": "„Welche unterschiedlichen Bräuche, Symbole und Traditionen prägen das Weihnachtsfest in Deutschland, und welche regionalen Unterschiede gibt es dabei?“",
|
| 132 |
+
"expander_label": "📜 Anleitung für Wissens-Trainer (Hier klicken)",
|
| 133 |
+
"instructions": """
|
| 134 |
+
### 1. Deine Mission
|
| 135 |
+
Deine Aufgabe ist es, **Quellen auszuwählen**, die dem Elf helfen, eine **vollständige** und **richtige** Antwort zu geben.
|
| 136 |
+
|
| 137 |
+
### 2. Werkzeugkiste: Suchstrategien
|
| 138 |
+
Der Elf akzeptiert nur **Wikipedia-Links**. Nutze Google clever:
|
| 139 |
+
* `site:de.wikipedia.org Weihnachten`
|
| 140 |
+
* `"Weihnachtsbräuche in Norddeutschland"`
|
| 141 |
+
|
| 142 |
+
### 3. So bewertet der Elf dein Training
|
| 143 |
+
* 🧠 **Relevanz:** Passt der Artikel zum Thema?
|
| 144 |
+
* 📚 **Vollständigkeit:** Sind alle wichtigen Begriffe enthalten?
|
| 145 |
+
* 🌍 **Regionale Abdeckung:** Sind Nord, Süd, Ost und West abgedeckt?
|
| 146 |
+
""",
|
| 147 |
+
"input_header": "🔗 Füttere den Elf mit Wissen",
|
| 148 |
+
"num_input_label": "Wie viele Artikel hast du gefunden? (1-8)",
|
| 149 |
+
"link_caption": "Füge hier deine Wikipedia-Links ein:",
|
| 150 |
+
"submit_btn": "🚀 Training starten & Auswerten",
|
| 151 |
+
"spinner": "Der Elf liest deine Artikel... 📖",
|
| 152 |
+
"res_title": "📊 Trainings-Ergebnis",
|
| 153 |
+
"metric_rel": "🧠 Relevanz",
|
| 154 |
+
"metric_comp": "📚 Vollständigkeit",
|
| 155 |
+
"metric_reg": "🌍 Regional",
|
| 156 |
+
"err_link": "⚠️ Hoppla! Der Elf konnte die Links nicht lesen. Sind es echte Wikipedia-Links?",
|
| 157 |
+
"fb_rel_good": "✅ **Super Fokus:** Die Artikel passen perfekt zum Thema.",
|
| 158 |
+
"fb_rel_med": "⚠️ **Geht so:** Die Artikel passen grob, aber es ist viel unwichtiges Zeug dabei.",
|
| 159 |
+
"fb_rel_bad": "❌ **Thema verfehlt:** Der Elf ist verwirrt. Die Artikel handeln kaum von Weihnachten.",
|
| 160 |
+
"fb_comp_good": "✅ **Weihnachts-Profi:** Du hast fast alle wichtigen Traditionen abgedeckt!",
|
| 161 |
+
"fb_comp_med": "⚠️ **Lückenhaft:** Einige Klassiker fehlen.",
|
| 162 |
+
"fb_comp_bad": "❌ **Anfänger-Wissen:** Da fehlen sehr viele Grundlagen.",
|
| 163 |
+
"fb_reg_good": "✅ **Deutschlandreise:** Von den Alpen bis zur See ist alles dabei!",
|
| 164 |
+
"fb_reg_med": "⚠️ **Lokalpatriot:** Du hast manche Regionen gut abgedeckt, aber andere fehlen.",
|
| 165 |
+
"fb_reg_bad": "❌ **Zu allgemein:** Der Elf kennt keine regionalen Unterschiede.",
|
| 166 |
+
"final_success": "🎉 HERVORRAGEND! Der Elf ist bereit für seinen Einsatz! Die Auswahl der Artikel ist sehr gut!"
|
| 167 |
+
},
|
| 168 |
+
"English": {
|
| 169 |
+
"page_title": "Christmas Elf Trainer",
|
| 170 |
+
"main_title": "🎄 Christmas Traditions: Context Evaluation for AI",
|
| 171 |
+
"task_header": "🎅 Mission: Train the Christmas Elf",
|
| 172 |
+
"task_desc_1": "We are building an AI agent to answer tourist questions about Christmas in Germany. But: <strong>The Elf only knows what YOU give him to read!</strong>",
|
| 173 |
+
"task_desc_2": "Find Wikipedia articles that answer this question perfectly:",
|
| 174 |
+
"question": "“What distinct customs, symbols, and traditions define Christmas in Germany, and what regional differences exist?”",
|
| 175 |
+
"expander_label": "📜 Instructions for Knowledge Trainers (Click here)",
|
| 176 |
+
"instructions": """
|
| 177 |
+
### ⚠️ IMPORTANT: LANGUAGE RULE
|
| 178 |
+
The Elf is training for **Germany**. Even though this interface is in English, **you must find GERMAN Wikipedia articles** (`de.wikipedia.org`).
|
| 179 |
+
* Do not use English Wikipedia links!
|
| 180 |
+
* Search for terms like *Weihnachten*, *Erzgebirge*, *Adventskranz*.
|
| 181 |
+
|
| 182 |
+
### 1. Your Mission
|
| 183 |
+
Select sources that help the Elf give a **complete** and **correct** answer about German traditions.
|
| 184 |
+
|
| 185 |
+
### 2. Toolbox: Search Strategies
|
| 186 |
+
The Elf only accepts **Wikipedia links**. Use Google smartly:
|
| 187 |
+
* `site:de.wikipedia.org Weihnachten`
|
| 188 |
+
* `"Weihnachtsbräuche in Norddeutschland"`
|
| 189 |
+
|
| 190 |
+
### 3. How the Elf Grades You
|
| 191 |
+
* 🧠 **Relevance:** Does the article actually talk about German Christmas?
|
| 192 |
+
* 📚 **Completeness:** Did you catch the key terms (Nikolaus, Advent, etc.)?
|
| 193 |
+
* 🌍 **Regional Coverage:** Did you cover North, South, East, and West?
|
| 194 |
+
""",
|
| 195 |
+
"input_header": "🔗 Feed the Elf with Knowledge",
|
| 196 |
+
"num_input_label": "How many articles did you find? (1-8)",
|
| 197 |
+
"link_caption": "Paste your German Wikipedia links here:",
|
| 198 |
+
"submit_btn": "🚀 Start Training & Evaluate",
|
| 199 |
+
"spinner": "The Elf is reading your articles... 📖",
|
| 200 |
+
"res_title": "📊 Training Results",
|
| 201 |
+
"metric_rel": "🧠 Relevance",
|
| 202 |
+
"metric_comp": "📚 Completeness",
|
| 203 |
+
"metric_reg": "🌍 Regional",
|
| 204 |
+
"err_link": "⚠️ Oops! The Elf couldn't read the links. Are they valid Wikipedia URLs?",
|
| 205 |
+
"fb_rel_good": "✅ **Great Focus:** The articles fit the topic perfectly.",
|
| 206 |
+
"fb_rel_med": "⚠️ **Okay:** The articles fit roughly, but there is a lot of unrelated noise.",
|
| 207 |
+
"fb_rel_bad": "❌ **Off Topic:** The Elf is confused. These articles are hardly about Christmas.",
|
| 208 |
+
"fb_comp_good": "✅ **Christmas Pro:** You covered almost all important traditions!",
|
| 209 |
+
"fb_comp_med": "⚠️ **Incomplete:** Some classics are missing.",
|
| 210 |
+
"fb_comp_bad": "❌ **Beginner Knowledge:** Many basics are missing.",
|
| 211 |
+
"fb_reg_good": "✅ **Germany Tour:** From the Alps to the Sea, everything is covered!",
|
| 212 |
+
"fb_reg_med": "⚠️ **Local Patriot:** You covered some regions well, but others are missing.",
|
| 213 |
+
"fb_reg_bad": "❌ **Too General:** The Elf knows no regional differences.",
|
| 214 |
+
"final_success": "🎉 OUTSTANDING! The Elf is ready for duty! Your selection of articles is excellent!"
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
# --- STREAMLIT UI ---
|
| 219 |
+
st.set_page_config(page_title="Elf Trainer", page_icon="🎅")
|
| 220 |
+
|
| 221 |
+
# Language Selector (Top Rightish)
|
| 222 |
+
lang = st.radio("Sprache / Language", ["Deutsch", "English"], horizontal=True)
|
| 223 |
+
t = TEXTS[lang]
|
| 224 |
|
| 225 |
+
st.title(t["main_title"])
|
| 226 |
|
| 227 |
+
st.markdown(f"""
|
| 228 |
<div style="background-color: #262730; padding: 20px; border-radius: 10px; border: 1px solid #4B4B4B; margin-bottom: 20px;">
|
| 229 |
+
<h3>{t['task_header']}</h3>
|
| 230 |
+
<p>{t['task_desc_1']}</p>
|
| 231 |
+
<p>{t['task_desc_2']}</p>
|
| 232 |
+
<blockquote style="background-color: #3e3e3e; color: white;"><em>{t['question']}</em></blockquote>
|
|
|
|
| 233 |
</div>
|
| 234 |
+
""", unsafe_allow_html=True)
|
|
|
|
| 235 |
|
| 236 |
+
with st.expander(t["expander_label"], expanded=True):
|
| 237 |
+
st.markdown(t["instructions"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
st.write("---")
|
| 240 |
+
st.subheader(t["input_header"])
|
| 241 |
|
| 242 |
+
# Input OUTSIDE form
|
| 243 |
+
num_links = st.number_input(t["num_input_label"], min_value=1, max_value=8, value=1)
|
| 244 |
|
| 245 |
with st.form("training_form"):
|
| 246 |
urls = []
|
| 247 |
+
st.caption(t["link_caption"])
|
| 248 |
+
|
| 249 |
for i in range(num_links):
|
| 250 |
urls.append(st.text_input(f"Link {i + 1}", placeholder="https://de.wikipedia.org/wiki/..."))
|
| 251 |
|
| 252 |
+
submitted = st.form_submit_button(t["submit_btn"])
|
| 253 |
|
| 254 |
if submitted:
|
| 255 |
student_texts = []
|
| 256 |
|
| 257 |
+
with st.spinner(t["spinner"]):
|
|
|
|
| 258 |
for url in urls:
|
| 259 |
if url.strip():
|
| 260 |
content = fetch_wikipedia_content(url)
|
|
|
|
| 265 |
scores = evaluate_contexts(student_texts)
|
| 266 |
|
| 267 |
st.write("---")
|
| 268 |
+
st.subheader(t["res_title"])
|
| 269 |
|
|
|
|
| 270 |
col1, col2, col3 = st.columns(3)
|
| 271 |
+
col1.metric(t["metric_rel"], f"{scores['relevance']:.2f}", help="> 0.7 is good")
|
| 272 |
+
col2.metric(t["metric_comp"], f"{int(scores['completeness']*100)}%", help="Keywords found")
|
| 273 |
+
col3.metric(t["metric_reg"], f"{int(scores['regional_completeness']*100)}%", help="Regions covered")
|
| 274 |
|
|
|
|
| 275 |
feedback = []
|
| 276 |
|
| 277 |
# Relevance Feedback
|
| 278 |
if scores["relevance"] > 0.8:
|
| 279 |
+
feedback.append(t["fb_rel_good"])
|
| 280 |
elif scores["relevance"] > 0.5:
|
| 281 |
+
feedback.append(t["fb_rel_med"])
|
| 282 |
else:
|
| 283 |
+
feedback.append(t["fb_rel_bad"])
|
| 284 |
|
| 285 |
# Completeness Feedback
|
| 286 |
if scores["completeness"] > 0.8:
|
| 287 |
+
feedback.append(t["fb_comp_good"])
|
| 288 |
elif scores["completeness"] > 0.5:
|
| 289 |
+
feedback.append(t["fb_comp_med"])
|
| 290 |
else:
|
| 291 |
+
feedback.append(t["fb_comp_bad"])
|
| 292 |
|
| 293 |
# Regional Feedback
|
| 294 |
if scores["regional_completeness"] > 0.8:
|
| 295 |
+
feedback.append(t["fb_reg_good"])
|
| 296 |
elif scores["regional_completeness"] > 0.5:
|
| 297 |
+
feedback.append(t["fb_reg_med"])
|
| 298 |
else:
|
| 299 |
+
feedback.append(t["fb_reg_bad"])
|
| 300 |
|
|
|
|
| 301 |
st.info("\n\n".join(feedback))
|
| 302 |
|
| 303 |
+
if scores["relevance"] > 0.8 and scores["completeness"] > 0.9 and scores["regional_completeness"] > 0.9:
|
|
|
|
| 304 |
st.balloons()
|
| 305 |
+
st.success(t["final_success"])
|
| 306 |
else:
|
| 307 |
+
st.error(t["err_link"])
|