Update app.py
Browse files
app.py
CHANGED
@@ -2,72 +2,88 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
5 |
-
# Dify API
|
6 |
-
api_key = "app-0UV1vRHHnChGssQ2Kc5UK9gg" #
|
7 |
api_url = "https://api.dify.ai/v1/chat-messages"
|
8 |
|
9 |
-
#
|
10 |
headers = {
|
11 |
"Authorization": f"Bearer {api_key}",
|
12 |
"Content-Type": "application/json"
|
13 |
}
|
14 |
|
15 |
-
#
|
16 |
conversation_history = []
|
17 |
|
18 |
def chat_with_dify(message, history):
|
19 |
"""
|
20 |
-
|
21 |
"""
|
22 |
-
#
|
23 |
user_message = {"role": "user", "content": message}
|
24 |
conversation_history.append(user_message)
|
25 |
|
26 |
-
#
|
27 |
data = {
|
28 |
"inputs": {},
|
29 |
"query": message,
|
30 |
-
"response_mode": "streaming",
|
31 |
-
"conversation_id": "",
|
32 |
"user": "gradio-user"
|
33 |
}
|
34 |
|
35 |
-
#
|
36 |
try:
|
|
|
|
|
|
|
|
|
37 |
response = requests.post(api_url, headers=headers, json=data)
|
38 |
-
response.raise_for_status() # Raise exception for HTTP errors
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
|
|
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
except requests.exceptions.RequestException as e:
|
49 |
-
error_message = f"
|
50 |
return error_message
|
51 |
|
52 |
-
#
|
53 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
54 |
-
gr.Markdown("# Dify AI
|
55 |
-
gr.Markdown("
|
56 |
|
57 |
chatbot = gr.Chatbot(height=400)
|
58 |
-
msg = gr.Textbox(placeholder="
|
59 |
-
clear = gr.Button("
|
60 |
|
61 |
def user(message, history):
|
62 |
-
#
|
63 |
return "", history + [[message, None]]
|
64 |
|
65 |
def bot(history):
|
66 |
-
#
|
67 |
user_message = history[-1][0]
|
68 |
-
#
|
69 |
bot_response = chat_with_dify(user_message, history)
|
70 |
-
#
|
71 |
history[-1][1] = bot_response
|
72 |
return history
|
73 |
|
@@ -76,12 +92,12 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
|
76 |
conversation_history = []
|
77 |
return None
|
78 |
|
79 |
-
#
|
80 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
81 |
bot, chatbot, chatbot
|
82 |
)
|
83 |
|
84 |
clear.click(clear_conversation, None, chatbot)
|
85 |
|
86 |
-
#
|
87 |
demo.launch()
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
5 |
+
# Dify API yapılandırması
|
6 |
+
api_key = "app-0UV1vRHHnChGssQ2Kc5UK9gg" # API anahtarınız
|
7 |
api_url = "https://api.dify.ai/v1/chat-messages"
|
8 |
|
9 |
+
# API istekleri için başlıklar
|
10 |
headers = {
|
11 |
"Authorization": f"Bearer {api_key}",
|
12 |
"Content-Type": "application/json"
|
13 |
}
|
14 |
|
15 |
+
# Konuşma geçmişini başlat
|
16 |
conversation_history = []
|
17 |
|
18 |
def chat_with_dify(message, history):
|
19 |
"""
|
20 |
+
Dify API ile iletişim kurma ve yanıt alma fonksiyonu
|
21 |
"""
|
22 |
+
# Kullanıcı mesajını konuşma geçmişine ekle
|
23 |
user_message = {"role": "user", "content": message}
|
24 |
conversation_history.append(user_message)
|
25 |
|
26 |
+
# API isteği için veriyi hazırla
|
27 |
data = {
|
28 |
"inputs": {},
|
29 |
"query": message,
|
|
|
|
|
30 |
"user": "gradio-user"
|
31 |
}
|
32 |
|
33 |
+
# API isteği yap
|
34 |
try:
|
35 |
+
print(f"API isteği gönderiliyor: {api_url}")
|
36 |
+
print(f"Headers: {headers}")
|
37 |
+
print(f"Data: {data}")
|
38 |
+
|
39 |
response = requests.post(api_url, headers=headers, json=data)
|
|
|
40 |
|
41 |
+
# API yanıtını kontrol et
|
42 |
+
print(f"API Yanıt Kodu: {response.status_code}")
|
43 |
+
print(f"API Yanıt İçeriği: {response.text[:500]}...") # İlk 500 karakter
|
44 |
|
45 |
+
# HTTP hata kodu kontrolü
|
46 |
+
if response.status_code != 200:
|
47 |
+
return f"API Hatası: HTTP {response.status_code} - {response.text}"
|
48 |
|
49 |
+
# Yanıtı parse et
|
50 |
+
if response.text.strip(): # Yanıt boş değilse
|
51 |
+
try:
|
52 |
+
response_data = response.json()
|
53 |
+
ai_message = response_data.get("answer", "Üzgünüm, bir yanıt oluşturamadım.")
|
54 |
+
|
55 |
+
# AI yanıtını konuşma geçmişine ekle
|
56 |
+
conversation_history.append({"role": "assistant", "content": ai_message})
|
57 |
+
|
58 |
+
return ai_message
|
59 |
+
except json.JSONDecodeError as json_err:
|
60 |
+
return f"JSON çözümleme hatası: {str(json_err)}\nYanıt: {response.text[:200]}..."
|
61 |
+
else:
|
62 |
+
return "API'den boş yanıt alındı"
|
63 |
+
|
64 |
except requests.exceptions.RequestException as e:
|
65 |
+
error_message = f"Dify API ile iletişim hatası: {str(e)}"
|
66 |
return error_message
|
67 |
|
68 |
+
# Gradio arayüzünü oluştur
|
69 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
70 |
+
gr.Markdown("# Dify AI Sohbet Botu")
|
71 |
+
gr.Markdown("Herhangi bir şey sorun, Dify AI kullanarak yanıt vereceğim!")
|
72 |
|
73 |
chatbot = gr.Chatbot(height=400)
|
74 |
+
msg = gr.Textbox(placeholder="Mesajınızı buraya yazın...", show_label=False)
|
75 |
+
clear = gr.Button("Konuşmayı Temizle")
|
76 |
|
77 |
def user(message, history):
|
78 |
+
# Kullanıcı mesajını geçmişe ekle ve dön
|
79 |
return "", history + [[message, None]]
|
80 |
|
81 |
def bot(history):
|
82 |
+
# Son kullanıcı mesajını al
|
83 |
user_message = history[-1][0]
|
84 |
+
# Bot yanıtını al
|
85 |
bot_response = chat_with_dify(user_message, history)
|
86 |
+
# Son etkileşimi bot yanıtıyla güncelle
|
87 |
history[-1][1] = bot_response
|
88 |
return history
|
89 |
|
|
|
92 |
conversation_history = []
|
93 |
return None
|
94 |
|
95 |
+
# Sohbet akışını ayarla
|
96 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
97 |
bot, chatbot, chatbot
|
98 |
)
|
99 |
|
100 |
clear.click(clear_conversation, None, chatbot)
|
101 |
|
102 |
+
# Uygulamayı başlat
|
103 |
demo.launch()
|