File size: 2,512 Bytes
e60c0a9
 
 
 
 
 
 
073ef49
 
9de31f7
073ef49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c58b29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
073ef49
 
8c58b29
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)