aiegoo commited on
Commit
2264c63
·
1 Parent(s): 048c1a1

add app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+ import requests
4
+ import secrets
5
+ import os
6
+
7
+ from io import BytesIO
8
+ from pydub import AudioSegment
9
+
10
+
11
+ def create_chat_session():
12
+ r = requests.post("http://121.176.153.117:5000/create")
13
+
14
+ if (r.status_code != 201):
15
+ raise Exception("Failed to create chat session")
16
+
17
+ # create temp audio folder
18
+ session_id = r.json()["id"]
19
+ os.makedirs(f"./temp_audio/{session_id}")
20
+
21
+ return session_id
22
+
23
+ session_id = create_chat_session()
24
+ chat_history = []
25
+
26
+
27
+ def add_text(history, text):
28
+ history = history + [(text, None)]
29
+ return history, gr.update(value="", interactive=False)
30
+
31
+
32
+ def add_audio(history, audio):
33
+
34
+ audio_bytes = base64.b64decode(audio['data'].split(',')[-1].encode('utf-8'))
35
+
36
+ audio_file = BytesIO(audio_bytes)
37
+ AudioSegment.from_file(audio_file).export(audio_file, format="mp3")
38
+
39
+ # save audio file temporary to disk
40
+ audio_id = secrets.token_hex(8)
41
+ AudioSegment.from_file(audio_file).export(f"temp_audio/{session_id}/audio_input_{audio_id}.mp3", format="mp3")
42
+
43
+ history = history + [((f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",), None)]
44
+
45
+ response = requests.post(
46
+ "http://121.176.153.117:5000/transcribe",
47
+ files={'audio': audio_file.getvalue()}
48
+ )
49
+
50
+ if (response.status_code != 200):
51
+ raise Exception(response.text)
52
+
53
+ text = response.json()['text']
54
+
55
+ history = history + [(text, None)]
56
+
57
+ return history, gr.update(value="", interactive=False)
58
+
59
+
60
+ def bot(history):
61
+ if type(history[-1][0]) == str:
62
+ message = history[-1][0]
63
+ else:
64
+ message = history[-2][0]
65
+
66
+ response = requests.post(
67
+ f"http://121.176.153.117:5000/send/text/{session_id}",
68
+ headers={'Content-type': 'application/json'},
69
+ json={
70
+ 'message': message,
71
+ 'role': 'user'
72
+ }
73
+ )
74
+
75
+ if (response.status_code != 200):
76
+ raise Exception(f"Failed to send message, {response.text}")
77
+
78
+ response = response.json()
79
+
80
+ text, audio = response['text'], response['audio']
81
+
82
+ audio_bytes = base64.b64decode(audio.encode('utf-8'))
83
+ audio_file = BytesIO(audio_bytes)
84
+ audio_id = secrets.token_hex(8)
85
+ AudioSegment.from_file(audio_file).export(f"temp_audio/{session_id}/audio_input_{audio_id}.mp3", format="mp3")
86
+
87
+ history = history + [(None, (f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",))]
88
+ history = history + [(None, text)]
89
+
90
+ global chat_history
91
+
92
+ chat_history = history.copy()
93
+
94
+ return history
95
+
96
+ def load_chat_history(history):
97
+ global chat_history
98
+ if len(chat_history) > len(history):
99
+ history = chat_history
100
+ return history
101
+
102
+
103
+ with gr.Blocks() as demo:
104
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
105
+
106
+ demo.load(load_chat_history, [chatbot], [chatbot], queue=False)
107
+
108
+ with gr.Row():
109
+ with gr.Column(scale=0.85):
110
+ txt = gr.Textbox(
111
+ show_label=False,
112
+ placeholder="Enter text and press enter, or record audio",
113
+ ).style(container=False)
114
+ with gr.Column(scale=0.15, min_width=0):
115
+ audio = gr.Audio(
116
+ source="microphone", type="numpy", show_label=False, format="mp3"
117
+ ).style(container=False)
118
+
119
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
120
+ bot, chatbot, chatbot
121
+ )
122
+ txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
123
+
124
+ audio_msg = audio.change(add_audio, [chatbot, audio], [chatbot, audio], queue=False, preprocess=False, postprocess=False).then(
125
+ bot, chatbot, chatbot
126
+ )
127
+ audio_msg.then(lambda: gr.update(interactive=True, value=None), None, [audio], queue=False)
128
+
129
+ demo.launch(show_error=True, server_name='172.30.1.18', share=True)