Etikos_AI / app.py
omersaidd's picture
Create app.py
7eb2d16 verified
raw
history blame
1.97 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 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() as demo:
gr.Markdown("# Etikos AI Chatbot")
chatbot = gr.Chatbot(height=400)
msg = gr.Textbox(placeholder="Mesajınızı buraya yazın...")
clear = gr.Button("Konuşmayı Temizle")
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])
clear.click(lambda: [], outputs=[chatbot])
clear.click(lambda: "", outputs=[msg])
# Arayüzü başlat
if __name__ == "__main__":
demo.launch()