|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium") |
|
|
|
|
|
def answer_question(file, question): |
|
try: |
|
if file is None: |
|
return "Lütfen önce bir JSON dosyası yükleyin." |
|
|
|
|
|
with open(file.name, "r", encoding="utf-8") as f: |
|
data = json.load(f) |
|
|
|
|
|
context = json.dumps(data, ensure_ascii=False) |
|
|
|
|
|
if "kaç kişi" in question.lower(): |
|
people_count = len(data) if isinstance(data, list) else 0 |
|
return f"JSON verisinde {people_count} kişi var." |
|
|
|
|
|
response = chatbot(question) |
|
return str(response) |
|
|
|
except Exception as e: |
|
return f"Hata: {e}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=answer_question, |
|
inputs=[ |
|
gr.File(label="📂 JSON Dosyası", file_types=[".json"]), |
|
gr.Textbox(label="💬 Soru", placeholder="Örn: Kişinin yaşı nedir?") |
|
], |
|
outputs="text", |
|
title="🧠 Türkçe Soru-Cevap Chatbot", |
|
description="Yüklediğiniz JSON dosyasına göre sorular sorabilirsiniz. Ayrıca doğal dilde sohbet edebilirsiniz." |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|