Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,54 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os, textwrap
|
| 3 |
-
import streamlit as st
|
| 4 |
-
from rag_pipeline import load_vectorstore, search_chunks, generate_answer
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
st.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
st.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
st.exception(e)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os, textwrap
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from rag_pipeline import load_vectorstore, search_chunks, generate_answer
|
| 5 |
+
VSTORE_DIR = os.path.join(os.path.dirname(_file_), "vectorstore")
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Turkish Wikipedia Q&A (Gemini RAG)", page_icon="🧠")
|
| 8 |
+
|
| 9 |
+
@st.cache_resource(show_spinner=False)
|
| 10 |
+
def _load_vdb():
|
| 11 |
+
return load_vectorstore(VSTORE_DIR)
|
| 12 |
+
|
| 13 |
+
st.title("🧠 Turkish Wikipedia Q&A")
|
| 14 |
+
|
| 15 |
+
with st.expander("⚙️ Ayarlar", expanded=False):
|
| 16 |
+
top_k = st.slider("Top K (kaç pasaj getirilsin?)", 2, 8, 5)
|
| 17 |
+
show_passages = st.checkbox("Getirilen pasaj özetini göster", value=True)
|
| 18 |
+
st.info("LLM: " + ("Gemini ✅" if os.getenv("GOOGLE_API_KEY") else "Yapılandırılmadı ❌"))
|
| 19 |
+
|
| 20 |
+
query = st.text_input("📝 Sorunuzu yazın (ör. “Türkiye'nin ilk kadın pilotu kimdir?”)")
|
| 21 |
+
|
| 22 |
+
if st.button("Cevabı Getir", type="primary", use_container_width=True) and query.strip():
|
| 23 |
+
try:
|
| 24 |
+
with st.spinner("Aranıyor ve cevap oluşturuluyor..."):
|
| 25 |
+
index, records = _load_vdb()
|
| 26 |
+
answer = generate_answer(query.strip(), index, records, top_k=top_k)
|
| 27 |
+
hits = search_chunks(query.strip(), index, records, top_k=top_k)
|
| 28 |
+
|
| 29 |
+
st.subheader("✅ Yanıt")
|
| 30 |
+
st.write(answer)
|
| 31 |
+
|
| 32 |
+
st.subheader("🔎 Kaynaklar")
|
| 33 |
+
if hits:
|
| 34 |
+
for i, h in enumerate(hits, 1):
|
| 35 |
+
title = h.get("title") or "(başlık yok)"
|
| 36 |
+
url = h.get("source") or ""
|
| 37 |
+
score = h.get("score_rerank", 0.0)
|
| 38 |
+
lead = textwrap.shorten((h.get("text") or "").replace("\n", " "), width=220, placeholder="…")
|
| 39 |
+
if url:
|
| 40 |
+
st.markdown(f"**{i}.** [{title}]({url}) \nRerank skoru: `{score:.3f}`")
|
| 41 |
+
else:
|
| 42 |
+
st.markdown(f"**{i}.** {title} \nRerank skoru: `{score:.3f}`")
|
| 43 |
+
if show_passages and lead:
|
| 44 |
+
st.caption(lead)
|
| 45 |
+
st.markdown("---")
|
| 46 |
+
else:
|
| 47 |
+
st.write("_Kaynak bulunamadı._")
|
| 48 |
+
|
| 49 |
+
except FileNotFoundError as e:
|
| 50 |
+
st.error("Vektör deposu bulunamadı. Önce `python data_preparation.py` çalıştırın.")
|
| 51 |
+
st.code(str(e))
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error("Bir hata oluştu.")
|
| 54 |
st.exception(e)
|