Files changed (1) hide show
  1. app.py +130 -50
app.py CHANGED
@@ -3,6 +3,7 @@ import base64
3
  import requests
4
  import secrets
5
  import os
 
6
 
7
  from io import BytesIO
8
  from pydub import AudioSegment
@@ -11,8 +12,67 @@ from pydub import AudioSegment
11
  LOCAL_API_ENDPOINT = "http://localhost:5000"
12
  PUBLIC_API_ENDPOINT = "http://121.176.153.117:5000"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def create_chat_session():
15
- r = requests.post(PUBLIC_API_ENDPOINT + "/create")
16
 
17
  if (r.status_code != 201):
18
  raise Exception("Failed to create chat session")
@@ -23,9 +83,6 @@ def create_chat_session():
23
 
24
  return session_id
25
 
26
- session_id = create_chat_session()
27
- chat_history = []
28
-
29
  def create_new_or_change_session(history, id):
30
  global session_id
31
  global chat_history
@@ -59,7 +116,7 @@ def add_audio(history, audio):
59
  history = history + [((f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",), None)]
60
 
61
  response = requests.post(
62
- PUBLIC_API_ENDPOINT + "/transcribe",
63
  files={'audio': audio_file.getvalue()}
64
  )
65
 
@@ -77,7 +134,7 @@ def reset_chat_session(history):
77
  global chat_history
78
 
79
  response = requests.post(
80
- PUBLIC_API_ENDPOINT + f"/reset/{session_id}"
81
  )
82
 
83
  if (response.status_code != 200):
@@ -95,7 +152,7 @@ def bot(history):
95
  message = history[-2][0]
96
 
97
  response = requests.post(
98
- PUBLIC_API_ENDPOINT + f"/send/text/{session_id}",
99
  headers={'Content-type': 'application/json'},
100
  json={
101
  'message': message,
@@ -129,7 +186,7 @@ def change_session(history, id):
129
  global chat_history
130
 
131
  response = requests.get(
132
- PUBLIC_API_ENDPOINT + f"/{id}"
133
  )
134
 
135
  if (response.status_code != 200):
@@ -176,57 +233,80 @@ def load_chat_history(history):
176
  history = chat_history
177
  return history
178
 
 
 
 
179
 
180
- with gr.Blocks() as demo:
181
- with gr.Row():
182
- # change session id
183
- change_session_txt = gr.Textbox(
184
- show_label=False,
185
- placeholder=session_id,
186
- ).style(container=False)
187
- with gr.Row():
188
- # button to create new or change session id
189
- change_session_button = gr.Button(
190
- "Create new or change session", type='success', size="sm"
191
- ).style(margin="0 10px 0 0", container=False)
192
-
193
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
194
-
195
- demo.load(load_chat_history, [chatbot], [chatbot], queue=False)
196
 
197
- with gr.Row():
198
- with gr.Column(scale=0.85):
199
- txt = gr.Textbox(
 
200
  show_label=False,
201
- placeholder="Enter text and press enter, or record audio",
202
  ).style(container=False)
203
- with gr.Column(scale=0.15, min_width=0):
204
- audio = gr.Audio(
205
- source="microphone", type="numpy", show_label=False, format="mp3"
 
206
  ).style(container=False)
207
 
208
-
209
- with gr.Row():
210
- reset_button = gr.Button(
211
- "Reset Chat Session", type='stop', size="sm"
212
- ).style(margin="0 10px 0 0", container=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
215
- bot, chatbot, chatbot
216
- )
217
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
 
 
218
 
219
- audio_msg = audio.change(add_audio, [chatbot, audio], [chatbot, audio], queue=False, preprocess=False, postprocess=False).then(
220
- bot, chatbot, chatbot
221
- )
222
- audio_msg.then(lambda: gr.update(interactive=True, value=None), None, [audio], queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
- reset_button.click(reset_chat_session, [chatbot], [chatbot], queue=False)
225
 
226
- chgn_msg = change_session_txt.submit(change_session, [chatbot, change_session_txt], [chatbot, change_session_txt], queue=False)
227
- chgn_msg.then(lambda: gr.update(interactive=True, placeholder=session_id), None, [change_session_txt], queue=False)
 
 
 
228
 
229
- create_new_or_change_session_btn = change_session_button.click(create_new_or_change_session, [chatbot, change_session_txt], [chatbot, change_session_txt], queue=False)
230
- create_new_or_change_session_btn.then(lambda: gr.update(interactive=True, placeholder=session_id), None, [change_session_txt], queue=False)
231
 
232
- demo.launch(show_error=True)
 
 
3
  import requests
4
  import secrets
5
  import os
6
+ import argparse
7
 
8
  from io import BytesIO
9
  from pydub import AudioSegment
 
12
  LOCAL_API_ENDPOINT = "http://localhost:5000"
13
  PUBLIC_API_ENDPOINT = "http://121.176.153.117:5000"
14
 
15
+ API_ENDPOINT = PUBLIC_API_ENDPOINT
16
+
17
+ session_id = ""
18
+ chat_history = []
19
+
20
+ css = """
21
+ #audio_input {
22
+ margin-top: -30px; !important;
23
+ margin-left: -15px; !important;
24
+ width: 100% !important;
25
+ }
26
+
27
+ #audio_input button {
28
+ height:50px !important;
29
+ font-size: 0px !important;
30
+ width: 110% !important;
31
+ }
32
+
33
+ #audio_input button:after {
34
+ content: '🎤' !important;
35
+ font-size: 16px !important;
36
+ }
37
+
38
+ audio {
39
+ min-width: 200px !important;
40
+ }
41
+
42
+ @media (max-width : 480px) {
43
+ #audio_input {
44
+ width: 120% !important;
45
+ }
46
+
47
+ #audio_input button:after {
48
+ content: '' !important;
49
+ }
50
+
51
+ #txt_input_container {
52
+ flex-grow: 70% !important;
53
+ }
54
+
55
+ #audio_input_container {
56
+ flex-grow: 30% !important;
57
+ }
58
+ }
59
+
60
+
61
+ """
62
+
63
+ js_audio_auto_play = """
64
+ () => {
65
+ // select last audio element
66
+ const audio = document.getElementsByTagName('audio');
67
+ const last_audio = audio[audio.length - 1];
68
+
69
+ // set autoplay attribute
70
+ last_audio.setAttribute('autoplay', true);
71
+ }
72
+ """
73
+
74
  def create_chat_session():
75
+ r = requests.post(API_ENDPOINT + "/create")
76
 
77
  if (r.status_code != 201):
78
  raise Exception("Failed to create chat session")
 
83
 
84
  return session_id
85
 
 
 
 
86
  def create_new_or_change_session(history, id):
87
  global session_id
88
  global chat_history
 
116
  history = history + [((f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",), None)]
117
 
118
  response = requests.post(
119
+ API_ENDPOINT + "/transcribe",
120
  files={'audio': audio_file.getvalue()}
121
  )
122
 
 
134
  global chat_history
135
 
136
  response = requests.post(
137
+ API_ENDPOINT + f"/reset/{session_id}"
138
  )
139
 
140
  if (response.status_code != 200):
 
152
  message = history[-2][0]
153
 
154
  response = requests.post(
155
+ API_ENDPOINT + f"/send/text/{session_id}",
156
  headers={'Content-type': 'application/json'},
157
  json={
158
  'message': message,
 
186
  global chat_history
187
 
188
  response = requests.get(
189
+ API_ENDPOINT + f"/{id}"
190
  )
191
 
192
  if (response.status_code != 200):
 
233
  history = chat_history
234
  return history
235
 
236
+ def main():
237
+ global session_id
238
+ global chat_history
239
 
240
+ session_id = create_chat_session()
241
+ chat_history = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
+ with gr.Blocks(css=css) as demo:
244
+ with gr.Row():
245
+ # change session id
246
+ change_session_txt = gr.Textbox(
247
  show_label=False,
248
+ placeholder=session_id,
249
  ).style(container=False)
250
+ with gr.Row():
251
+ # button to create new or change session id
252
+ change_session_button = gr.Button(
253
+ "Create new or change session", type='success', size="sm"
254
  ).style(container=False)
255
 
256
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
257
+
258
+ demo.load(load_chat_history, [chatbot], [chatbot], queue=False)
259
+
260
+ with gr.Row():
261
+ with gr.Column(scale=0.85, min_width=0, elem_id="txt_input_container"):
262
+ txt = gr.Textbox(
263
+ show_label=False,
264
+ placeholder="Enter text and press enter, or record audio",
265
+ elem_id="txt_input"
266
+ ).style(container=False)
267
+ with gr.Column(scale=0.15, min_width=0, elem_id="audio_input_container"):
268
+ audio = gr.Audio(
269
+ source="microphone", type="numpy", show_label=False, format="mp3", min_width=0, container=False, elem_id="audio_input"
270
+ )
271
+
272
+ with gr.Row():
273
+ reset_button = gr.Button(
274
+ "Reset Chat Session", type='stop', size="sm"
275
+ ).style(container=False)
276
 
277
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
278
+ bot, chatbot, chatbot
279
+ ).then(
280
+ None, [], [], queue=False, _js=js_audio_auto_play
281
+ )
282
+ txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
283
 
284
+ audio_msg = audio.change(add_audio, [chatbot, audio], [chatbot, audio], queue=False, preprocess=False, postprocess=False).then(
285
+ bot, chatbot, chatbot
286
+ ).then(
287
+ None, [], [], queue=False, _js=js_audio_auto_play
288
+ )
289
+ audio_msg.then(lambda: gr.update(interactive=True, value=None), None, [audio], queue=False)
290
+
291
+ reset_button.click(reset_chat_session, [chatbot], [chatbot], queue=False)
292
+
293
+ chgn_msg = change_session_txt.submit(change_session, [chatbot, change_session_txt], [chatbot, change_session_txt], queue=False)
294
+ chgn_msg.then(lambda: gr.update(interactive=True, placeholder=session_id), None, [change_session_txt], queue=False)
295
+
296
+ create_new_or_change_session_btn = change_session_button.click(create_new_or_change_session, [chatbot, change_session_txt], [chatbot, change_session_txt], queue=False)
297
+ create_new_or_change_session_btn.then(lambda: gr.update(interactive=True, placeholder=session_id), None, [change_session_txt], queue=False)
298
+
299
+ return demo
300
 
 
301
 
302
+ if __name__ == "__main__":
303
+ # arguments --local
304
+ parser = argparse.ArgumentParser()
305
+ parser.add_argument("--local", action="store_true", help="Use local API endpoint")
306
+ args = parser.parse_args()
307
 
308
+ if args.local:
309
+ API_ENDPOINT = LOCAL_API_ENDPOINT
310
 
311
+ demo = main()
312
+ demo.launch(show_error=True, server_name="0.0.0.0")