Etikos_AI / app.py
omersaidd's picture
Update app.py
745d95f verified
raw
history blame
2.4 kB
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()