|
import streamlit as st |
|
import pandas as pd |
|
import json |
|
from transformers import pipeline |
|
|
|
st.title("💬 Chatbot - Kurum Verileriyle Soru-Yanıt (JSON)") |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
|
|
return pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") |
|
|
|
model = load_model() |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
user_input = st.text_input("💡 Soru sor:", placeholder="Örneğin: 'Hangi çalışanlar 30 yaşından büyük?'") |
|
|
|
|
|
if user_input and data is not None: |
|
with st.spinner("⏳ Cevap bekleniyor..."): |
|
|
|
context = data.to_string(index=False) |
|
|
|
try: |
|
|
|
result = model(question=user_input, context=context) |
|
response = result["answer"] |
|
|
|
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") |