meandyou200175 commited on
Commit
77ad04a
·
verified ·
1 Parent(s): 145d7d7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ def encode_file_to_base64(file_path):
5
+ with open(file_path, "rb") as file:
6
+ return base64.b64encode(file.read()).decode('utf-8')
7
+ def bot_audio_interface(wav):
8
+ print(wav)
9
+ url = "https://0e85-34-145-27-120.ngrok-free.app/speech_to_speech"
10
+ b64 = encode_file_to_base64(wav)
11
+ data = {"b64": b64}
12
+ print('1')
13
+ response = requests.post(url, json=data)
14
+ print('2')
15
+ if response.status_code == 200:
16
+ json_response = response.json()
17
+ text = json_response.get("text")
18
+ response_text = json_response.get("response")
19
+ audio_base64 = json_response.get("audio_base64")
20
+ audio_data = base64.b64decode(audio_base64)
21
+ audio_path = "response_audio.wav"
22
+ with open(audio_path, "wb") as audio_file:
23
+ audio_file.write(audio_data)
24
+ else:
25
+ print('kkk')
26
+ audio_path =''
27
+ text=''
28
+ response_text=''
29
+ return audio_path, text, response_text
30
+
31
+
32
+ with gr.Blocks() as demo:
33
+ with gr.Row():
34
+ with gr.Column():
35
+ audio_input = gr.Audio(type="filepath", label="Input Audio") # Input âm thanh
36
+ recognized_textbox = gr.Textbox(label="Recognized Text") # Text được nhận dạng bên trái
37
+ with gr.Column():
38
+ audio_output = gr.Audio(type="filepath", label="Response Audio") # Output âm thanh bên phải
39
+ response_textbox = gr.Textbox(label="ChatWOW Response") # Phản hồi từ bot
40
+
41
+ audio_input.change(fn=bot_audio_interface, inputs=audio_input, outputs=[audio_output, recognized_textbox, response_textbox])
42
+
43
+ demo.launch(share = True)