-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
71 lines (51 loc) · 1.94 KB
/
client.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
60
61
62
63
64
65
66
67
68
69
70
71
import os
import requests
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
st.title("Chatbot")
if "text_chosen" not in st.session_state:
st.session_state.text_chosen = False
if "audio_chosen" not in st.session_state:
st.session_state.audio_chosen = False
def text_clicked():
st.session_state.text_chosen = True
st.session_state.audio_chosen = False
def audio_clicked():
st.session_state.text_chosen = False
st.session_state.audio_chosen = True
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
content = message["content"]
if isinstance(content, bytes):
st.audio(content)
else:
st.markdown(content)
st.button("Text bot", on_click=text_clicked)
st.button("Audio bot", on_click=audio_clicked)
if st.session_state.text_chosen:
if prompt := st.chat_input("Write your prompt to generate text answer"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.text(prompt)
with st.chat_message("assistant"):
response = requests.get(
f"{os.getenv('API_URL', 'http://localhost:8000')}/generate/text?prompt={prompt}",
timeout=60,
).text
st.text(response)
st.session_state.messages.append(
{"role": "assistant", "content": "Here is your answer"}
)
if st.session_state.audio_chosen:
if prompt := st.chat_input("Write your prompt to generate audio"):
with st.chat_message("assistant"):
response = requests.get(
f"{os.getenv('API_URL', 'http://localhost:8000')}/generate/audio?prompt={prompt}",
timeout=60,
).content
st.text("Here is your audio")
st.audio(response)