|
import gradio as gr |
|
from gtts import gTTS |
|
from nltk import tokenize |
|
import os |
|
|
|
|
|
import nltk |
|
nltk.download('punkt') |
|
|
|
|
|
sentences = [] |
|
|
|
|
|
def process_text(mytext): |
|
global sentences |
|
sentences = tokenize.sent_tokenize(mytext) |
|
return [f"{i + 1}. {s}" for i, s in enumerate(sentences)] |
|
|
|
|
|
def generate_audio(selected_item): |
|
if not selected_item: |
|
return None |
|
|
|
index = int(selected_item.split('.')[0]) - 1 |
|
|
|
if 0 <= index < len(sentences): |
|
sentence = sentences[index] |
|
tts = gTTS(text=sentence, lang='en') |
|
audio_path = f'sentence_{index + 1}.mp3' |
|
tts.save(audio_path) |
|
return audio_path |
|
else: |
|
return None |
|
|
|
|
|
def update_dropdown(mytext): |
|
choices = process_text(mytext) |
|
return gr.update(choices=choices) |
|
|
|
|
|
with gr.Blocks() as app: |
|
with gr.Row(): |
|
textbox = gr.Textbox(label="Enter your text here") |
|
with gr.Row(): |
|
submit_button = gr.Button("Submit") |
|
with gr.Row(): |
|
dropdown = gr.Dropdown(choices=[], label="Select Sentence") |
|
with gr.Row(): |
|
audio_output = gr.Audio(label="Audio of Selected Sentence") |
|
|
|
submit_button.click(fn=update_dropdown, inputs=textbox, outputs=dropdown) |
|
dropdown.change(fn=generate_audio, inputs=dropdown, outputs=audio_output) |
|
|
|
app.launch() |
|
|