Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import base64 | |
def encode_file_to_base64(file_path): | |
with open(file_path, "rb") as file: | |
return base64.b64encode(file.read()).decode('utf-8') | |
def bot_audio_interface(wav): | |
try: | |
print(wav) | |
url = "https://0537-35-247-65-73.ngrok-free.app/speech_to_speech" | |
b64 = encode_file_to_base64(wav) | |
data = {"b64": b64} | |
print('1') | |
response = requests.post(url, json=data) | |
print('2') | |
if response.status_code == 200: | |
json_response = response.json() | |
text = json_response.get("text") | |
response_text = json_response.get("response") | |
audio_base64 = json_response.get("audio_base64") | |
audio_data = base64.b64decode(audio_base64) | |
audio_path = "response_audio1.wav" | |
with open(audio_path, "wb") as audio_file: | |
audio_file.write(audio_data) | |
else: | |
print('switch') | |
url = "https://b434-34-148-80-250.ngrok-free.app/speech_to_speech" | |
b64 = encode_file_to_base64(wav) | |
data = {"b64": b64} | |
print('1') | |
response = requests.post(url, json=data) | |
print('2') | |
if response.status_code == 200: | |
json_response = response.json() | |
text = json_response.get("text") | |
response_text = json_response.get("response") | |
audio_base64 = json_response.get("audio_base64") | |
audio_data = base64.b64decode(audio_base64) | |
audio_path = "response_audio1.wav" | |
with open(audio_path, "wb") as audio_file: | |
audio_file.write(audio_data) | |
return audio_path, text, response_text | |
except: | |
return 'final_output.wav', 'text', 'response_text' | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
audio_input = gr.Audio(sources="microphone", type="filepath", label="Input Audio") # Input âm thanh | |
recognized_textbox = gr.Textbox(label="Recognized Text") # Text được nhận dạng bên trái | |
with gr.Column(): | |
audio_output = gr.Audio(type="filepath", label="Response Audio") # Output âm thanh bên phải | |
response_textbox = gr.Textbox(label="ChatWOW Response") # Phản hồi từ bot | |
audio_input.change(fn=bot_audio_interface, inputs=audio_input, outputs=[audio_output, recognized_textbox, response_textbox]) | |
demo.launch(share = True) |