File size: 3,155 Bytes
7355fc4 459f30e 3ab3af5 1da3868 7355fc4 459f30e 07ce5d9 459f30e 7355fc4 e55bbe2 f258c7f 459f30e f258c7f e55bbe2 f258c7f e55bbe2 f258c7f 07ce5d9 7355fc4 07ce5d9 459f30e 7355fc4 459f30e 7355fc4 1da3868 41632ff 1da3868 7355fc4 1da3868 07ce5d9 459f30e 07ce5d9 459f30e 07ce5d9 7355fc4 07ce5d9 7355fc4 1da3868 7355fc4 1da3868 7355fc4 07ce5d9 7355fc4 f258c7f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import gradio as gr
import json
from transformers import pipeline
# GPT-2 yerine soru-yanıt modeli kullanıyoruz (Türkçe destekli)
chatbot = pipeline("question-answering", model="savasy/bert-base-turkish-squad")
# JSON dosyasını yükle ve verileri al
def load_json(file):
if file is None:
return "JSON dosyası yükleyin."
try:
# UTF-8 ile dene
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)
return data
except UnicodeDecodeError:
# Eğer UTF-8 başarısız olursa, Windows-1254 ile dene
try:
with open(file, "r", encoding="windows-1254") as f:
data = json.load(f)
return data
except Exception as e:
return f"Hata: Dosya kodlaması uyumsuz: {e}"
except Exception as e:
return f"Hata: {e}"
# Sohbet fonksiyonu
def chat_with_bot(user_input, json_file):
# JSON dosyasından verileri al
json_data = load_json(json_file)
if isinstance(json_data, str): # Eğer hata mesajı döndürdüyse
return json_data
# JSON verisini metne çevir (context olarak kullanılacak)
context = json.dumps(json_data, ensure_ascii=False)
# "JSON dosyası isimleri neler?" sorusuna özel mantık
if "isimleri neler" in user_input.lower() or "isimler" in user_input.lower():
if isinstance(json_data, list):
names = [entry.get("İsim", "Bilinmiyor") for entry in json_data if "İsim" in entry]
if names:
return f"JSON dosyasındaki isimler: {', '.join(names)}"
else:
return "JSON dosyasında isim bulunamadı."
return "JSON dosyası bir liste değil, isimler çıkarılamadı."
# Genel soru-yanıt için modelden cevap al
try:
response = chatbot(question=user_input, context=context)
bot_reply = response["answer"]
confidence = response["score"]
bot_reply += f" (Güven skoru: {confidence:.2f})"
except Exception as e:
bot_reply = f"Hata: Model yanıtı üretirken bir sorun oluştu: {e}"
# Ek sorular için mantık (örneğin, "kaç kişi" veya "yaş")
if "kaç kişi" in user_input.lower():
num_people = len(json_data)
bot_reply += f" JSON dosyasındaki kişi sayısı: {num_people}."
if "yaş" in user_input.lower() and isinstance(json_data, list):
ages = [entry['Yaş'] for entry in json_data if 'Yaş' in entry]
avg_age = sum(ages) / len(ages) if ages else "bulunamadı"
bot_reply += f" JSON dosyasındaki ortalama yaş: {avg_age}."
return bot_reply
# Gradio arayüzü
demo = gr.Interface(
fn=chat_with_bot,
inputs=[
gr.Textbox(label="💬 Senin Mesajın", placeholder="Örneğin: 'JSON dosyası isimleri neler?'"),
gr.File(label="📂 JSON Dosyası", type="filepath", file_types=[".json"]),
],
outputs="text",
title="🧠 Türkçe Sohbet Botu ve JSON Entegrasyonu",
description="JSON dosyasındaki verilerle Türkçe sorular sorarak doğal dilde cevaplar alın.",
)
# Arayüzü başlat
if __name__ == "__main__":
demo.launch() |