File size: 868 Bytes
ae4ea47
81136e1
3e994a1
 
 
 
81136e1
a7c8bb7
81136e1
 
3e994a1
 
 
 
 
 
 
 
 
81136e1
a7c8bb7
 
 
 
 
 
33072bb
 
 
a7c8bb7
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
import gradio as gr
from gtts import gTTS
import wavio
from io import BytesIO
import base64

# Function to play the original text as audio
def text_to_speech(text_to_play):
    tts = gTTS(text_to_play, lang='hr')  # Assuming the text is in English
    tts.save('original_audio.mp3')
    
    audio_bytes = BytesIO()
    tts.write_to_fp(audio_bytes)
    audio_bytes.seek(0)

    audio = base64.b64encode(audio_bytes.read()).decode("utf-8")
    audio_player = f'<audio src="data:audio/mpeg;base64,{audio}" controls autoplay></audio>'
    return audio_player
    

with gr.Blocks() as demo:
    html = gr.HTML()
    # html.visible = False
    text = gr.Text()
    btn = gr.Button("OK")
    btn.click(text_to_speech, inputs=[text], outputs=[html])
    clear = gr.Button("Clear")
    clear.click(lambda x: gr.update(value=""), None, [text], queue=False)
    
demo.launch()