chatbot / app.py
meandyou200175's picture
Update app.py
9f07257 verified
raw
history blame
1.68 kB
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):
print(wav)
url = "https://737e-104-199-156-254.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_audio.wav"
with open(audio_path, "wb") as audio_file:
audio_file.write(audio_data)
else:
print('kkk')
audio_path =''
text=''
response_text=''
return audio_path, 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)