File size: 2,399 Bytes
7eb2d16
 
 
 
 
 
 
 
 
 
 
a3bcd4f
7eb2d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745d95f
 
 
 
 
 
7eb2d16
745d95f
 
 
7eb2d16
745d95f
 
 
 
7eb2d16
 
 
 
 
 
 
 
745d95f
7eb2d16
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
import requests

# Dify API yapılandırması
api_key = "app-0UV1vRHHnChGssQ2Kc5UK9gg"
api_url = "https://api.dify.ai/v1/chat-messages"

# Konuşma ID'sini saklamak için
conversation_id = ""

def chat_with_dify(message, history):
    """Etikos AI olarak sorularınıza cevap vermek için buradayım."""
    global conversation_id
    
    # API isteği için payload
    payload = {
        "inputs": {},
        "query": message,
        "response_mode": "blocking",
        "conversation_id": conversation_id,
        "user": "user"
    }
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    try:
        response = requests.post(api_url, headers=headers, json=payload)
        
        if response.status_code == 200:
            response_data = response.json()
            
            # Konuşma ID'sini güncelle
            if "conversation_id" in response_data and response_data["conversation_id"]:
                conversation_id = response_data["conversation_id"]
            
            return response_data.get("answer", "Yanıt alınamadı")
        else:
            return f"Hata: {response.status_code}"
    
    except Exception as e:
        return f"İstek gönderilirken hata: {str(e)}"

# Gradio arayüzünü oluştur
with gr.Blocks(theme=gr.themes.Default(primary_hue="black")) as demo:
    with gr.Row():
        with gr.Column(scale=4):
            gr.Markdown("# Etikos AI Chatbot")
        with gr.Column(scale=1, min_width=100):
            gr.Image(value="logo.png", label="", show_label=False, height=50, container=False)
    
    with gr.Row():
        clear = gr.Button("Konuşmayı Temizle")
        
    chatbot = gr.Chatbot(height=400)
    
    with gr.Row():
        msg = gr.Textbox(label="Mesajınız", placeholder="Mesajınızı buraya yazın...")
        send = gr.Button("Gönder")
    
    def respond(message, chat_history):
        bot_message = chat_with_dify(message, chat_history)
        chat_history.append((message, bot_message))
        return "", chat_history
    
    # Buton olayları
    msg.submit(respond, [msg, chatbot], [msg, chatbot])
    send.click(respond, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: [], outputs=[chatbot])
    clear.click(lambda: "", outputs=[msg])

# Arayüzü başlat
if __name__ == "__main__":
    demo.launch()