-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_app.py
59 lines (42 loc) · 1.6 KB
/
gradio_app.py
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
import random
import time
import uuid
import gradio as gr
import requests
API_URL = "http://api:8000/"
def start_conversation(user_id: str, messages: list):
response = requests.post(
API_URL + "conversations/", json={"user_id": user_id, "message": messages}
)
if response.status_code == 200:
return response.json()
return {"error": "Failed to start conversation"}
def update_conversation(user_id: str, message: list):
response = requests.post(API_URL + f"update/{user_id}", json={"message": message})
if response.status_code == 200:
return response.json()
return {"error": "Failed to update conversation"}
def get_bot_message():
return random.choice(["How are you?", "Today is a great day", "I'm very hungry"])
def respond(message: str, chat_history: list):
global user_id
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": get_bot_message()})
time.sleep(2)
update_conversation(user_id=user_id, message=chat_history)
return "", chat_history
def launch_gradio_app():
# Initialize the conversation
with gr.Blocks() as demo:
global user_id
user_id = str(uuid.uuid4())
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
start_conversation(user_id=user_id, messages=chatbot.value)
msg.submit(respond, [msg, chatbot], [msg, chatbot])
print(user_id)
print(chatbot.value)
demo.launch(server_name="0.0.0.0")
if __name__ == "__main__":
launch_gradio_app()