Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Dify API yapılandırması
|
5 |
+
api_key = "app-0UV1vRHHnChGssQ2Kc5UK9gg"
|
6 |
+
api_url = "https://api.dify.ai/v1/chat-messages"
|
7 |
+
|
8 |
+
# Konuşma ID'sini saklamak için
|
9 |
+
conversation_id = ""
|
10 |
+
|
11 |
+
def chat_with_dify(message, history):
|
12 |
+
"""Etikos AI sorularınıza cevap vermek için buradayım."""
|
13 |
+
global conversation_id
|
14 |
+
|
15 |
+
# API isteği için payload
|
16 |
+
payload = {
|
17 |
+
"inputs": {},
|
18 |
+
"query": message,
|
19 |
+
"response_mode": "blocking",
|
20 |
+
"conversation_id": conversation_id,
|
21 |
+
"user": "user"
|
22 |
+
}
|
23 |
+
|
24 |
+
headers = {
|
25 |
+
"Authorization": f"Bearer {api_key}",
|
26 |
+
"Content-Type": "application/json"
|
27 |
+
}
|
28 |
+
|
29 |
+
try:
|
30 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
31 |
+
|
32 |
+
if response.status_code == 200:
|
33 |
+
response_data = response.json()
|
34 |
+
|
35 |
+
# Konuşma ID'sini güncelle
|
36 |
+
if "conversation_id" in response_data and response_data["conversation_id"]:
|
37 |
+
conversation_id = response_data["conversation_id"]
|
38 |
+
|
39 |
+
return response_data.get("answer", "Yanıt alınamadı")
|
40 |
+
else:
|
41 |
+
return f"Hata: {response.status_code}"
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return f"İstek gönderilirken hata: {str(e)}"
|
45 |
+
|
46 |
+
# Gradio arayüzünü oluştur
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# Etikos AI Chatbot")
|
49 |
+
|
50 |
+
chatbot = gr.Chatbot(height=400)
|
51 |
+
msg = gr.Textbox(placeholder="Mesajınızı buraya yazın...")
|
52 |
+
clear = gr.Button("Konuşmayı Temizle")
|
53 |
+
|
54 |
+
def respond(message, chat_history):
|
55 |
+
bot_message = chat_with_dify(message, chat_history)
|
56 |
+
chat_history.append((message, bot_message))
|
57 |
+
return "", chat_history
|
58 |
+
|
59 |
+
# Buton olayları
|
60 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
61 |
+
clear.click(lambda: [], outputs=[chatbot])
|
62 |
+
clear.click(lambda: "", outputs=[msg])
|
63 |
+
|
64 |
+
# Arayüzü başlat
|
65 |
+
if __name__ == "__main__":
|
66 |
+
demo.launch()
|