File size: 1,677 Bytes
e60c0a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f07257
e60c0a9
 
 
 
 
 
 
77ad04a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)