| import gradio as gr |
| import requests |
| import json |
| import os |
|
|
| |
| API_URL = os.environ.get("MY_SECRET_KEY") |
|
|
| |
| conversation_history = [] |
| session_id = None |
|
|
| def send_message(message, history): |
| global session_id |
| |
| |
| payload = { |
| "message": message |
| } |
| |
| |
| if session_id: |
| payload["session_id"] = session_id |
| |
| |
| headers = { |
| "Content-Type": "application/json" |
| } |
| |
| try: |
| response = requests.post(API_URL, headers=headers, data=json.dumps(payload)) |
| response_data = response.json() |
| |
| |
| response_text = response_data.get("response", "No response received") |
| session_id = response_data.get("session_id") |
| |
| |
| return response_text |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| |
| 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): |
| |
| history.append((message, None)) |
| return "", history |
| |
| def bot_message(history): |
| |
| user_message = history[-1][0] |
| |
| |
| bot_response = send_message(user_message, history) |
| |
| |
| history[-1] = (user_message, bot_response) |
| return history |
| |
| |
| 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) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |