deneme / app.py
syurek's picture
Update app.py
459f30e verified
raw
history blame
2.03 kB
import gradio as gr
import json
import pandas as pd
from transformers import pipeline
# GPT-2 modelini yükle
chatbot = pipeline("text-generation", model="gpt2")
# JSON dosyasını yükle ve verileri al
def load_json(file):
if file is None:
return "JSON dosyası yükleyin."
try:
# JSON dosyasını yükle
data = json.load(file)
return data
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
# Kullanıcıdan gelen metinle modelden yanıt al
response = chatbot(user_input, max_length=100, num_return_sequences=1)
# Modelin cevabını al
bot_reply = response[0]['generated_text']
# JSON verileriyle ilgili bilgi ekle (örneğin, JSON'daki kişi sayısını sormak)
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}."
# Eğer JSON verisindeki bir özellik sorulursa (örneğin 'Yaş' sütunu varsa)
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="Buraya yaz ve botla sohbet et."),
gr.File(label="📂 JSON Dosyası", file_types=[".json"]),
],
outputs="text",
title="🧠 GPT-2 Sohbet Botu ve JSON Entegrasyonu",
description="JSON dosyasındaki verilerle etkileşime girerek GPT-2 tabanlı bir sohbet botu ile doğal dilde sohbet edin.",
)
# Arayüzü başlat
if __name__ == "__main__":
demo.launch()