syurek commited on
Commit
1da3868
·
verified ·
1 Parent(s): e55bbe2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -2,8 +2,8 @@ import gradio as gr
2
  import json
3
  from transformers import pipeline
4
 
5
- # GPT-2 modelini yükle
6
- chatbot = pipeline("text-generation", model="gpt2")
7
 
8
  # JSON dosyasını yükle ve verileri al
9
  def load_json(file):
@@ -33,13 +33,29 @@ def chat_with_bot(user_input, json_file):
33
  if isinstance(json_data, str): # Eğer hata mesajı döndürdüyse
34
  return json_data
35
 
36
- # Kullanıcıdan gelen metinle modelden yanıt al
37
- response = chatbot(user_input, max_length=100, num_return_sequences=1)
38
 
39
- # Modelin cevabını al
40
- bot_reply = response[0]['generated_text']
 
 
 
 
 
 
 
41
 
42
- # JSON verileriyle ilgili bilgi ekle
 
 
 
 
 
 
 
 
 
43
  if "kaç kişi" in user_input.lower():
44
  num_people = len(json_data)
45
  bot_reply += f" JSON dosyasındaki kişi sayısı: {num_people}."
@@ -55,12 +71,12 @@ def chat_with_bot(user_input, json_file):
55
  demo = gr.Interface(
56
  fn=chat_with_bot,
57
  inputs=[
58
- gr.Textbox(label="💬 Senin Mesajın", placeholder="Buraya yaz ve botla sohbet et."),
59
- gr.File(label="📂 JSON Dosyası", type="filepath", file_types=[".json"]), # type="file" yerine type="filepath"
60
  ],
61
  outputs="text",
62
- title="🧠 GPT-2 Sohbet Botu ve JSON Entegrasyonu",
63
- description="JSON dosyasındaki verilerle etkileşime girerek GPT-2 tabanlı bir sohbet botu ile doğal dilde sohbet edin.",
64
  )
65
 
66
  # Arayüzü başlat
 
2
  import json
3
  from transformers import pipeline
4
 
5
+ # GPT-2 yerine soru-yanıt modeli kullanıyoruz (Türkçe destekli)
6
+ chatbot = pipeline("question-answering", model="savasy/bert-base-turkish-squad")
7
 
8
  # JSON dosyasını yükle ve verileri al
9
  def load_json(file):
 
33
  if isinstance(json_data, str): # Eğer hata mesajı döndürdüyse
34
  return json_data
35
 
36
+ # JSON verisini metne çevir (context olarak kullanılacak)
37
+ context = json.dumps(json_data, ensure_ascii=False)
38
 
39
+ # "JSON dosyası isimleri neler?" sorusuna özel mantık
40
+ if "isimleri neler" in user_input.lower() or "isimler" in user_input.lower():
41
+ if isinstance(json_data, list):
42
+ names = [entry.get("İsim", "Bilinmiyor") for entry in json_data if "İsim" in entry]
43
+ if names:
44
+ return f"JSON dosyasındaki isimler: {', '.join(names)}"
45
+ else:
46
+ return "JSON dosyasında isim bulunamadı."
47
+ return "JSON dosyası bir liste değil, isimler çıkarılamadı."
48
 
49
+ # Genel soru-yanıt için modelden cevap al
50
+ try:
51
+ response = chatbot(question=user_input, context=context)
52
+ bot_reply = response["answer"]
53
+ confidence = response["score"]
54
+ bot_reply += f" (Güven skoru: {confidence:.2f})"
55
+ except Exception as e:
56
+ bot_reply = f"Hata: Model yanıtı üretirken bir sorun oluştu: {e}"
57
+
58
+ # Ek sorular için mantık (örneğin, "kaç kişi" veya "yaş")
59
  if "kaç kişi" in user_input.lower():
60
  num_people = len(json_data)
61
  bot_reply += f" JSON dosyasındaki kişi sayısı: {num_people}."
 
71
  demo = gr.Interface(
72
  fn=chat_with_bot,
73
  inputs=[
74
+ gr.Textbox(label="💬 Senin Mesajın", placeholder="Örneğin: 'JSON dosyası isimleri neler?'"),
75
+ gr.File(label="📂 JSON Dosyası", type="filepath", file_types=[".json"]),
76
  ],
77
  outputs="text",
78
+ title="🧠 Türkçe Sohbet Botu ve JSON Entegrasyonu",
79
+ description="JSON dosyasındaki verilerle Türkçe sorular sorarak doğal dilde cevaplar alın.",
80
  )
81
 
82
  # Arayüzü başlat