argon / app.py
imaigen's picture
Create app.py
43d008e verified
Raw
History Blame Contribute Delete
2.38 kB
import gradio as gr
import requests
import json
import os
# API endpoint
API_URL = os.environ.get("MY_SECRET_KEY")
# Store conversation history
conversation_history = []
session_id = None
def send_message(message, history):
global session_id
# Prepare the request payload
payload = {
"message": message
}
# Add session_id if we have one from previous interactions
if session_id:
payload["session_id"] = session_id
# Send the request to the API
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
response_data = response.json()
# Extract the response text and session_id
response_text = response_data.get("response", "No response received")
session_id = response_data.get("session_id")
# Update conversation history
return response_text
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface
with gr.Blocks(title="Argonneo Chat") as demo:
gr.Markdown("# Argonneo Chat")
gr.Markdown("Chat with the Argonneo API!! (This is a demo version not intended for commercial usage)")
chatbot = gr.Chatbot(height=400)
with gr.Row():
msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
submit_button = gr.Button("Submit")
clear = gr.Button("Clear")
def user_message(message, history):
# Add user message to history
history.append((message, None))
return "", history
def bot_message(history):
# Get the last user message
user_message = history[-1][0]
# Get response from API
bot_response = send_message(user_message, history)
# Update history with bot response
history[-1] = (user_message, bot_response)
return history
# Connect the submit button and Enter key to the same functions
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
bot_message, chatbot, chatbot
)
submit_button.click(user_message, [msg, chatbot], [msg, chatbot]).then(
bot_message, chatbot, chatbot
)
clear.click(lambda: [], None, chatbot)
# Launch the app
if __name__ == "__main__":
demo.launch()