File size: 2,382 Bytes
43d008e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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()