deneme / app.py
syurek's picture
Update app.py
1613beb verified
raw
history blame
1.35 kB
import gradio as gr
import pandas as pd
import json
from transformers import pipeline
# Modeli yükle
model = pipeline("question-answering", model="savasy/bert-base-turkish-squad")
# Soru-cevap fonksiyonu
def answer_question(file, question):
try:
if file is None:
return "Lütfen önce bir JSON dosyası yükleyin."
# Dosyayı aç ve JSON olarak yükle
with open(file.name, "r", encoding="utf-8") as f:
data = json.load(f)
# JSON verisini bir metin olarak oluştur
context = json.dumps(data, ensure_ascii=False)
# Hugging Face pipeline kullanarak soru-cevap yap
result = model(question=question, context=context)
answer = result["answer"]
score = result["score"]
return f"Yanıt: {answer}\n(Güven skoru: {score:.2f})"
except Exception as e:
return f"Hata: {e}"
# Gradio arayüzü
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. Model: savasy/bert-base-turkish-squad"
)
if __name__ == "__main__":
demo.launch()