-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (63 loc) · 2.29 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import streamlit as st
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events
from PIL import Image
def main_loop():
st.title("Secret Chat with Avatars")
st.subheader("Imagine & Say Anything!")
#st.text("Some Avatars are Real ;)")
image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg'])
if not image_file:
return None
original_image = Image.open(image_file)
st.image(original_image)
stt_button = Button(label="Say", width=100)
stt_button.js_on_event("button_click", CustomJS(code="""
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function (e) {
var value = "";
for (var i = e.resultIndex; i < e.results.length; ++i) {
if (e.results[i].isFinal) {
value += e.results[i][0].transcript;
}
}
if ( value != "") {
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
}
}
recognition.start();
"""))
tts_button = Button(label="Listen", width=100)
result = streamlit_bokeh_events(
stt_button,
events="GET_TEXT",
key="listen",
refresh_on_update=False,
override_height=75,
debounce_time=0)
if result:
if "GET_TEXT" in result:
text = result.get("GET_TEXT")
st.write(text)
prompt_data = {
"prompt": "\n\n### Instructions:\n"+text+"\n\n### Response:\n",
"stop": ["\n","###"]
}
url_post = "http://localhost:8000/v1/completions"
response = requests.post(url_post, json=prompt_data)
json = response.json()
ans = json['choices'][0]['text']
st.write(ans)
tts_button.js_on_event("button_click", CustomJS(code=f"""
var u = new SpeechSynthesisUtterance();
u.text = "{ans}";
u.lang = 'en-US';
speechSynthesis.speak(u);
"""))
st.bokeh_chart(tts_button)
if __name__ == '__main__':
main_loop()