chatboot / app.py
syurek's picture
Rename chatbot.py to app.py
6852be2 verified
import streamlit as st
import pandas as pd
import json
from transformers import pipeline
st.title("💬 Chatbot - Kurum Verileriyle Soru-Yanıt (JSON)")
# Hugging Face soru-yanıt modelini yükle
@st.cache_resource
def load_model():
# Soru-yanıt için popüler bir model
return pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
model = load_model()
# JSON dosyasını yükleme fonksiyonu
def load_data(file):
try:
json_data = json.load(file)
df = pd.DataFrame(json_data)
return df
except json.JSONDecodeError as e:
st.error(f"❌ JSON dosyası geçersiz: {e}")
return None
except Exception as e:
st.error(f"❌ Dosya yüklenirken hata oluştu: {e}")
return None
# Kullanıcıdan JSON yüklemesini iste
uploaded_file = st.file_uploader("📂 JSON dosyanızı yükleyin", type=["json"])
data = None
if uploaded_file is not None:
data = load_data(uploaded_file)
if data is not None:
st.success("✅ JSON başarıyla yüklendi!")
st.dataframe(data)
# Kullanıcıdan soru alma
user_input = st.text_input("💡 Soru sor:", placeholder="Örneğin: 'Hangi çalışanlar 30 yaşından büyük?'")
# Soru ve veri varsa modele gönder
if user_input and data is not None:
with st.spinner("⏳ Cevap bekleniyor..."):
# Veriyi metin olarak hazırlama (modelin context’i olarak kullanılacak)
context = data.to_string(index=False)
try:
# Model ile soru-yanıt
result = model(question=user_input, context=context)
response = result["answer"]
# Güven skoru (isteğe bağlı)
confidence = f"(Güven skoru: {result['score']:.2f})"
except Exception as e:
response = f"❌ Model hatası: {e}"
confidence = ""
st.subheader("📝 Yanıt:")
st.text_area("", f"{response} {confidence}", height=200, key="response_area")
st.download_button(
label="📥 Yanıtı Kopyala",
data=response,
file_name="yanit.txt",
mime="text/plain"
)
else:
if not uploaded_file:
st.info("📂 Lütfen önce bir JSON dosyası yükleyin.")
elif not user_input:
st.info("✅ JSON yüklendi. Şimdi bir soru sorabilirsiniz.")
st.markdown("---")
st.caption("🔹 Hugging Face & distilbert-base-uncased-distilled-squad ile desteklenmektedir. | syurek/chatboot | Tarih: 05 Nisan 2025")