|
import gradio as gr |
|
import pandas as pd |
|
from transformers import pipeline |
|
|
|
|
|
chatbot = pipeline("text-generation", model="gpt2") |
|
|
|
|
|
def load_csv(file): |
|
if file is None: |
|
return "CSV dosyası yükleyin." |
|
try: |
|
|
|
df = pd.read_csv(file.name, encoding="ISO-8859-1") |
|
return df |
|
except Exception as e: |
|
return f"Hata: {e}" |
|
|
|
|
|
def chat_with_bot(user_input, csv_file): |
|
|
|
csv_data = load_csv(csv_file) |
|
|
|
if isinstance(csv_data, str): |
|
return csv_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(csv_data) |
|
bot_reply += f" CSV dosyasındaki kişi sayısı: {num_people}." |
|
|
|
|
|
if "yaş" in user_input.lower(): |
|
avg_age = csv_data['Yaş'].mean() if 'Yaş' in csv_data.columns else "bulunamadı" |
|
bot_reply += f" CSV 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="📂 CSV Dosyası", file_types=[".csv"]), |
|
], |
|
outputs="text", |
|
title="🧠 GPT-2 Sohbet Botu ve CSV Entegrasyonu", |
|
description="CSV dosyasındaki verilerle etkileşime girerek GPT-2 tabanlı bir sohbet botu ile doğal dilde sohbet edin.", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|