syurek commited on
Commit
3dd307b
·
verified ·
1 Parent(s): 8f732cb

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +14 -22
chatbot.py CHANGED
@@ -3,22 +3,20 @@ import pandas as pd
3
  import json
4
  from transformers import pipeline
5
 
6
- st.title("💬 Chatbot - Kurum Verileriyle Sohbet (JSON)")
7
 
8
- # Hugging Face modelini yükle
9
  @st.cache_resource
10
  def load_model():
11
- # Hafif bir model (daha hızlı yükleme için)
12
- return pipeline("text-generation", model="distilgpt2")
13
 
14
  model = load_model()
15
 
16
  # JSON dosyasını yükleme fonksiyonu
17
  def load_data(file):
18
  try:
19
- # JSON dosyasını oku
20
  json_data = json.load(file)
21
- # JSON’u DataFrame’e çevir (records formatı varsayılıyor)
22
  df = pd.DataFrame(json_data)
23
  return df
24
  except json.JSONDecodeError as e:
@@ -44,27 +42,21 @@ user_input = st.text_input("💡 Soru sor:", placeholder="Örneğin: 'Hangi çal
44
  # Soru ve veri varsa modele gönder
45
  if user_input and data is not None:
46
  with st.spinner("⏳ Cevap bekleniyor..."):
47
- prompt = f"""
48
- Sen gelişmiş bir yapay zeka asistanısın. Aşağıda, bir kurumun çalışan bilgileri JSON formatında yer alıyor.
49
- Kullanıcının sorduğu sorulara, aşağıdaki verilere dayanarak doğru ve insansı bir şekilde cevap ver.
50
- **ÖNEMLİ:** Yalnızca verilen veriler doğrultusunda yanıt ver, hayal ürünü bilgiler uydurma.
51
-
52
- 📋 **Çalışan Listesi (İlk 10 kişi gösteriliyor):**
53
- {data.head(10).to_string(index=False)}
54
-
55
- ❓ **Soru:** {user_input}
56
- """
57
 
58
  try:
59
- # Model ile yanıt üret
60
- response = model(prompt, max_length=200, num_return_sequences=1, truncation=True)[0]["generated_text"]
61
- # Prompt’un kendisini yanıtın başında tekrar etmesini önlemek için kırpma
62
- response = response[len(prompt):].strip()
 
63
  except Exception as e:
64
  response = f"❌ Model hatası: {e}"
 
65
 
66
  st.subheader("📝 Yanıt:")
67
- st.text_area("", response, height=200, key="response_area")
68
  st.download_button(
69
  label="📥 Yanıtı Kopyala",
70
  data=response,
@@ -78,4 +70,4 @@ else:
78
  st.info("✅ JSON yüklendi. Şimdi bir soru sorabilirsiniz.")
79
 
80
  st.markdown("---")
81
- st.caption("🔹 Hugging Face & distilgpt2 ile desteklenmektedir. | syurek/chatboot | Tarih: 05 Nisan 2025")
 
3
  import json
4
  from transformers import pipeline
5
 
6
+ st.title("💬 Chatbot - Kurum Verileriyle Soru-Yanıt (JSON)")
7
 
8
+ # Hugging Face soru-yanıt modelini yükle
9
  @st.cache_resource
10
  def load_model():
11
+ # Soru-yanıt için popüler bir model
12
+ return pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
13
 
14
  model = load_model()
15
 
16
  # JSON dosyasını yükleme fonksiyonu
17
  def load_data(file):
18
  try:
 
19
  json_data = json.load(file)
 
20
  df = pd.DataFrame(json_data)
21
  return df
22
  except json.JSONDecodeError as e:
 
42
  # Soru ve veri varsa modele gönder
43
  if user_input and data is not None:
44
  with st.spinner("⏳ Cevap bekleniyor..."):
45
+ # Veriyi metin olarak hazırlama (modelin context’i olarak kullanılacak)
46
+ context = data.to_string(index=False)
 
 
 
 
 
 
 
 
47
 
48
  try:
49
+ # Model ile soru-yanıt
50
+ result = model(question=user_input, context=context)
51
+ response = result["answer"]
52
+ # Güven skoru (isteğe bağlı)
53
+ confidence = f"(Güven skoru: {result['score']:.2f})"
54
  except Exception as e:
55
  response = f"❌ Model hatası: {e}"
56
+ confidence = ""
57
 
58
  st.subheader("📝 Yanıt:")
59
+ st.text_area("", f"{response} {confidence}", height=200, key="response_area")
60
  st.download_button(
61
  label="📥 Yanıtı Kopyala",
62
  data=response,
 
70
  st.info("✅ JSON yüklendi. Şimdi bir soru sorabilirsiniz.")
71
 
72
  st.markdown("---")
73
+ st.caption("🔹 Hugging Face & distilbert-base-uncased-distilled-squad ile desteklenmektedir. | syurek/chatboot | Tarih: 05 Nisan 2025")