Lukas
commited on
Commit
·
72f94ce
1
Parent(s):
bbdca94
Improved source view.3
Browse files
app.py
CHANGED
|
@@ -194,7 +194,7 @@ if prompt := st.chat_input("Ihre Frage eingeben..."):
|
|
| 194 |
|
| 195 |
message_placeholder.markdown(full_response)
|
| 196 |
|
| 197 |
-
# G) Quellen Anzeige (Hierarchie-Shift
|
| 198 |
st.markdown("---")
|
| 199 |
st.markdown(f"**Top-Quellen (Gefiltert aus 100):**")
|
| 200 |
st.markdown(", ".join(list(unique_sources_labels)))
|
|
@@ -202,29 +202,39 @@ if prompt := st.chat_input("Ihre Frage eingeben..."):
|
|
| 202 |
with st.expander("Details anzeigen"):
|
| 203 |
st.caption(f"Modell: {model_name} | Sortiert durch FlashRank")
|
| 204 |
|
|
|
|
|
|
|
|
|
|
| 205 |
for i, item in enumerate(source_details):
|
| 206 |
# 1. Die Quelle selbst wird nun zum obersten Header (#)
|
| 207 |
st.markdown(f"# {item['label']}")
|
| 208 |
st.caption(f"Relevanz-Score: {item['score']:.4f}")
|
| 209 |
|
| 210 |
-
# 2.
|
| 211 |
-
# WICHTIG: Wir ersetzen von groß nach klein, um Doppel-Ersetzungen zu vermeiden
|
| 212 |
text = item['text']
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
st.markdown(
|
| 222 |
|
| 223 |
-
#
|
| 224 |
if i < len(source_details) - 1:
|
| 225 |
st.markdown("---")
|
| 226 |
|
| 227 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 228 |
-
|
| 229 |
except Exception as e:
|
| 230 |
st.error(f"Fehler: {e}")
|
|
|
|
| 194 |
|
| 195 |
message_placeholder.markdown(full_response)
|
| 196 |
|
| 197 |
+
# G) Quellen Anzeige (Hierarchie-Shift + Keyword Highlighting)
|
| 198 |
st.markdown("---")
|
| 199 |
st.markdown(f"**Top-Quellen (Gefiltert aus 100):**")
|
| 200 |
st.markdown(", ".join(list(unique_sources_labels)))
|
|
|
|
| 202 |
with st.expander("Details anzeigen"):
|
| 203 |
st.caption(f"Modell: {model_name} | Sortiert durch FlashRank")
|
| 204 |
|
| 205 |
+
# Suchbegriffe für das Highlighting vorbereiten (kleine Wörter ignorieren)
|
| 206 |
+
search_terms = [word.strip(",.?!") for word in prompt.split() if len(word) > 3]
|
| 207 |
+
|
| 208 |
for i, item in enumerate(source_details):
|
| 209 |
# 1. Die Quelle selbst wird nun zum obersten Header (#)
|
| 210 |
st.markdown(f"# {item['label']}")
|
| 211 |
st.caption(f"Relevanz-Score: {item['score']:.4f}")
|
| 212 |
|
| 213 |
+
# 2. HIERARCHIE-SHIFT: Alle Header um eine Ebene nach unten
|
|
|
|
| 214 |
text = item['text']
|
| 215 |
+
# Von klein nach groß ersetzen, um Kaskaden-Effekte zu vermeiden
|
| 216 |
+
for level in range(5, 0, -1):
|
| 217 |
+
old_prefix = "#" * level + " "
|
| 218 |
+
new_prefix = "#" * (level + 1) + " "
|
| 219 |
+
text = text.replace(old_prefix, new_prefix)
|
| 220 |
+
|
| 221 |
+
# 3. KEYWORD HIGHLIGHTING (Optionaler Bonus)
|
| 222 |
+
# Wir ersetzen die Begriffe im Text durch eine fette Version
|
| 223 |
+
highlighted_text = text
|
| 224 |
+
for term in search_terms:
|
| 225 |
+
# Case-insensitive Ersetzung für die Anzeige
|
| 226 |
+
import re
|
| 227 |
+
pattern = re.compile(re.escape(term), re.IGNORECASE)
|
| 228 |
+
highlighted_text = pattern.sub(f"**\g<0>**", highlighted_text)
|
| 229 |
|
| 230 |
+
# 4. Text ausgeben
|
| 231 |
+
st.markdown(highlighted_text)
|
| 232 |
|
| 233 |
+
# 5. Saubere Trennung
|
| 234 |
if i < len(source_details) - 1:
|
| 235 |
st.markdown("---")
|
| 236 |
|
| 237 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 238 |
+
|
| 239 |
except Exception as e:
|
| 240 |
st.error(f"Fehler: {e}")
|