|
import gradio as gr |
|
import json |
|
import pandas as pd |
|
from transformers import pipeline |
|
|
|
|
|
chatbot = pipeline("text-generation", model="gpt2") |
|
|
|
|
|
def load_json(file): |
|
if file is None: |
|
return "JSON dosyası yükleyin." |
|
try: |
|
|
|
data = json.load(file) |
|
return data |
|
except Exception as e: |
|
return f"Hata: {e}" |
|
|
|
|
|
def chat_with_bot(user_input, json_file): |
|
|
|
json_data = load_json(json_file) |
|
|
|
if isinstance(json_data, str): |
|
return json_data |
|
|
|
|
|
response = chatbot(user_input, max_length=100, num_return_sequences=1) |
|
|
|
|
|
bot_reply = response[0]['generated_text'] |
|
|
|
|
|
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 |
|
|
|
|
|
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.", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|