File size: 1,584 Bytes
142a536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gtts import gTTS
from nltk import tokenize
import os

# Import necessary nltk libraries
import nltk
nltk.download('punkt')

# Global variable to store sentences
sentences = []

# Function to process text and generate sentence options
def process_text(mytext):
    global sentences
    sentences = tokenize.sent_tokenize(mytext)
    return [f"{i + 1}. {s}" for i, s in enumerate(sentences)]

# Function to generate audio for the selected sentence
def generate_audio(selected_item):
    if not selected_item:
        return None

    index = int(selected_item.split('.')[0]) - 1  # Adjust for 0-based index

    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

# Function to update dropdown choices based on text input
def update_dropdown(mytext):
    choices = process_text(mytext)
    return gr.update(choices=choices)

# Create a Gradio Blocks app
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()