"""
Tıbbi Soru-Cevap Chatbot Web Arayüzü
Streamlit ile oluşturulmuş RAG tabanlı tıbbi chatbot uygulaması.
"""
import streamlit as st
import sys
import os
from pathlib import Path
# Src klasörünü path'e ekle
sys.path.append(str(Path(__file__).parent / "src"))
from rag_pipeline import MedicalRAGSystem
# Sayfa konfigürasyonu
st.set_page_config(
page_title="Tıbbi Soru-Cevap Chatbot",
page_icon="🏥",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS
st.markdown("""
""", unsafe_allow_html=True)
@st.cache_resource
def load_rag_system():
"""RAG sistemini yükle (cache ile)"""
try:
return MedicalRAGSystem()
except FileNotFoundError as e:
st.error(str(e))
st.info("Lütfen önce vector database'i oluşturun: `python src/build_vector_db.py`")
st.stop()
except Exception as e:
st.error(f"Sistem yüklenirken hata oluştu: {e}")
st.stop()
def display_source(source, index):
"""Kaynak bilgisini göster"""
st.markdown(f"""
Kaynak {index}
Uzmanlık: {source['speciality']}
Doktor: {source['doctor']}
İlgili Soru: {source['preview']}...
""", unsafe_allow_html=True)
def main():
"""Ana uygulama"""
# Başlık
st.markdown('🏥 Tıbbi Soru-Cevap Chatbot
', unsafe_allow_html=True)
# Uyarı mesajı
st.markdown("""
⚠️ ÖNEMLİ UYARI: Bu chatbot sadece bilgilendirme amaçlıdır.
Verilen bilgiler kesin tıbbi tavsiye yerine geçmez. Sağlık sorunlarınız için
mutlaka bir sağlık uzmanına başvurun.
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.header("⚙️ Ayarlar")
# Kaç doküman retrieve edilecek
k_docs = st.slider(
"Kaynak Doküman Sayısı",
min_value=1,
max_value=5,
value=3,
help="Her sorgu için kaç benzer soru-cevap çifti kullanılacak"
)
# Uzmanlık alanı filtresi (opsiyonel)
use_filter = st.checkbox("Uzmanlık Alanı Filtrele", value=False)
speciality_filter = None
if use_filter:
speciality_filter = st.text_input(
"Uzmanlık Alanı",
placeholder="Örn: Dahiliye, Kardiyoloji"
)
st.markdown("---")
# Hakkında
st.header("ℹ️ Hakkında")
st.markdown("""
Bu chatbot, 167.000+ tıbbi soru-cevap verisi kullanılarak
RAG (Retrieval-Augmented Generation) teknolojisi ile geliştirilmiştir.
**Teknolojiler:**
- Gemini API (LLM)
- LangChain (RAG Framework)
- ChromaDB (Vector Database)
- Streamlit (Web Framework)
**Veri Seti:**
- Kaynak: Huggingface
- Boyut: 167,732 kayıt
- Dil: Türkçe
""")
st.markdown("---")
# Örnek sorular
st.header("💡 Örnek Sorular")
example_questions = [
"Baş ağrısı için ne yapmalıyım?",
"Grip olduğumda ne yemem gerekir?",
"Yüksek tansiyon belirtileri nelerdir?",
"Vitamin D eksikliği nasıl anlaşılır?",
"Uykusuzluk için doğal çözümler nelerdir?"
]
for question in example_questions:
if st.button(question, key=f"example_{question[:20]}"):
st.session_state.current_question = question
# Ana içerik alanı
st.header("💬 Soru Sorun")
# Session state'i başlat
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'current_question' not in st.session_state:
st.session_state.current_question = ""
# RAG sistemini yükle
with st.spinner("Sistem yükleniyor..."):
rag_system = load_rag_system()
st.success("✓ Sistem hazır! Sorularınızı sorabilirsiniz.")
# Soru input
col1, col2 = st.columns([6, 1])
with col1:
user_question = st.text_input(
"Sorunuzu yazın:",
value=st.session_state.current_question,
placeholder="Örn: Mide ağrısı için ne yapmalıyım?",
key="question_input"
)
with col2:
st.write("") # Boşluk için
st.write("") # Boşluk için
ask_button = st.button("🔍 Sor", type="primary", use_container_width=True)
# Soru sorulduğunda
if ask_button and user_question:
with st.spinner("Cevap hazırlanıyor..."):
try:
# RAG sisteminden cevap al
result = rag_system.ask(
query=user_question,
k=k_docs,
speciality_filter=speciality_filter if use_filter and speciality_filter else None
)
# Sohbet geçmişine ekle
st.session_state.chat_history.append({
"question": user_question,
"answer": result['answer'],
"sources": result['sources']
})
# Input'u temizle
st.session_state.current_question = ""
except Exception as e:
st.error(f"Hata oluştu: {e}")
# Sohbet geçmişini göster
if st.session_state.chat_history:
st.markdown("---")
st.header("📝 Sohbet Geçmişi")
# En son soruyu en üstte göster
for idx, chat in enumerate(reversed(st.session_state.chat_history)):
st.markdown(f"""
Soru: {chat['question']}
""", unsafe_allow_html=True)
st.markdown(f"""
Cevap:
{chat['answer']}
""", unsafe_allow_html=True)
# Kaynakları göster
with st.expander("📚 Kaynakları Görüntüle"):
for i, source in enumerate(chat['sources'], 1):
display_source(source, i)
st.markdown("---")
# Geçmişi temizle butonu
if st.button("🗑️ Sohbet Geçmişini Temizle"):
st.session_state.chat_history = []
st.rerun()
else:
st.info("👆 Yukarıdaki kutuya sorunuzu yazın veya yan taraftaki örnek sorulardan birini seçin.")
if __name__ == "__main__":
main()