Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,8 @@ import gradio as gr
|
|
2 |
import json
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# GPT-2
|
6 |
-
chatbot = pipeline("
|
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 |
-
#
|
37 |
-
|
38 |
|
39 |
-
#
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="
|
59 |
-
gr.File(label="📂 JSON Dosyası", type="filepath", file_types=[".json"]),
|
60 |
],
|
61 |
outputs="text",
|
62 |
-
title="🧠
|
63 |
-
description="JSON dosyasındaki verilerle
|
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
|