File size: 1,000 Bytes
cec32ca
f835a88
 
cec32ca
 
 
 
 
 
 
 
 
 
f835a88
 
 
 
 
 
cec32ca
 
 
 
 
255d96a
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
import gradio as gr
import os
from gtts import gTTS

def text_to_speech(text):
    # Check for specific keywords and modify input text accordingly
    if "[laughs]" in text:
        text = text.replace("[laughs]", "ha ha ha")
    if "[music]" in text or "[music in background]" in text:
        text = text.replace("[music]", "")
        text = text.replace("[music in background]", "")
        text = "♫ " + text + " ♫"
    
    # Use gTTS library to generate speech from input text
    tts = gTTS(text)
    tts.save("output.mp3")
    
    # Play the audio file using OS audio player
    os.system("mpg123 output.mp3")

# Define input text box for Gradio interface
text_input = gr.inputs.Textbox(label="Enter Text")

# Define Gradio interface and launch app
gr.Interface(text_to_speech, inputs=text_input, outputs=None, title="Text-to-Speech", description="Enter text and listen to it being spoken. Keywords [laughs], [music], and [music in background] modify the speech accordingly.").launch()