Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# API endpoint
|
| 7 |
+
API_URL = os.environ.get("MY_SECRET_KEY")
|
| 8 |
+
|
| 9 |
+
# Store conversation history
|
| 10 |
+
conversation_history = []
|
| 11 |
+
session_id = None
|
| 12 |
+
|
| 13 |
+
def send_message(message, history):
|
| 14 |
+
global session_id
|
| 15 |
+
|
| 16 |
+
# Prepare the request payload
|
| 17 |
+
payload = {
|
| 18 |
+
"message": message
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Add session_id if we have one from previous interactions
|
| 22 |
+
if session_id:
|
| 23 |
+
payload["session_id"] = session_id
|
| 24 |
+
|
| 25 |
+
# Send the request to the API
|
| 26 |
+
headers = {
|
| 27 |
+
"Content-Type": "application/json"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
|
| 32 |
+
response_data = response.json()
|
| 33 |
+
|
| 34 |
+
# Extract the response text and session_id
|
| 35 |
+
response_text = response_data.get("response", "No response received")
|
| 36 |
+
session_id = response_data.get("session_id")
|
| 37 |
+
|
| 38 |
+
# Update conversation history
|
| 39 |
+
return response_text
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error: {str(e)}"
|
| 42 |
+
|
| 43 |
+
# Create the Gradio interface
|
| 44 |
+
with gr.Blocks(title="Argonneo Chat") as demo:
|
| 45 |
+
gr.Markdown("# Argonneo Chat")
|
| 46 |
+
gr.Markdown("Chat with the Argonneo API!! (This is a demo version not intended for commercial usage)")
|
| 47 |
+
|
| 48 |
+
chatbot = gr.Chatbot(height=400)
|
| 49 |
+
with gr.Row():
|
| 50 |
+
msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
|
| 51 |
+
submit_button = gr.Button("Submit")
|
| 52 |
+
clear = gr.Button("Clear")
|
| 53 |
+
|
| 54 |
+
def user_message(message, history):
|
| 55 |
+
# Add user message to history
|
| 56 |
+
history.append((message, None))
|
| 57 |
+
return "", history
|
| 58 |
+
|
| 59 |
+
def bot_message(history):
|
| 60 |
+
# Get the last user message
|
| 61 |
+
user_message = history[-1][0]
|
| 62 |
+
|
| 63 |
+
# Get response from API
|
| 64 |
+
bot_response = send_message(user_message, history)
|
| 65 |
+
|
| 66 |
+
# Update history with bot response
|
| 67 |
+
history[-1] = (user_message, bot_response)
|
| 68 |
+
return history
|
| 69 |
+
|
| 70 |
+
# Connect the submit button and Enter key to the same functions
|
| 71 |
+
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
|
| 72 |
+
bot_message, chatbot, chatbot
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
submit_button.click(user_message, [msg, chatbot], [msg, chatbot]).then(
|
| 76 |
+
bot_message, chatbot, chatbot
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
clear.click(lambda: [], None, chatbot)
|
| 80 |
+
|
| 81 |
+
# Launch the app
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|