File size: 2,026 Bytes
7355fc4
459f30e
07ce5d9
3ab3af5
 
07ce5d9
 
7355fc4
459f30e
 
07ce5d9
459f30e
7355fc4
459f30e
 
 
07ce5d9
 
7355fc4
07ce5d9
459f30e
 
 
7355fc4
459f30e
 
7355fc4
07ce5d9
 
41632ff
07ce5d9
 
7355fc4
459f30e
07ce5d9
459f30e
 
07ce5d9
459f30e
 
 
 
 
07ce5d9
 
7355fc4
 
 
07ce5d9
7355fc4
07ce5d9
459f30e
7355fc4
 
459f30e
 
7355fc4
 
07ce5d9
7355fc4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()