MK-316 commited on
Commit
142a536
1 Parent(s): 7a8d543

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gtts import gTTS
3
+ from nltk import tokenize
4
+ import os
5
+
6
+ # Import necessary nltk libraries
7
+ import nltk
8
+ nltk.download('punkt')
9
+
10
+ # Global variable to store sentences
11
+ sentences = []
12
+
13
+ # Function to process text and generate sentence options
14
+ def process_text(mytext):
15
+ global sentences
16
+ sentences = tokenize.sent_tokenize(mytext)
17
+ return [f"{i + 1}. {s}" for i, s in enumerate(sentences)]
18
+
19
+ # Function to generate audio for the selected sentence
20
+ def generate_audio(selected_item):
21
+ if not selected_item:
22
+ return None
23
+
24
+ index = int(selected_item.split('.')[0]) - 1 # Adjust for 0-based index
25
+
26
+ if 0 <= index < len(sentences):
27
+ sentence = sentences[index]
28
+ tts = gTTS(text=sentence, lang='en')
29
+ audio_path = f'sentence_{index + 1}.mp3'
30
+ tts.save(audio_path)
31
+ return audio_path
32
+ else:
33
+ return None
34
+
35
+ # Function to update dropdown choices based on text input
36
+ def update_dropdown(mytext):
37
+ choices = process_text(mytext)
38
+ return gr.update(choices=choices)
39
+
40
+ # Create a Gradio Blocks app
41
+ with gr.Blocks() as app:
42
+ with gr.Row():
43
+ textbox = gr.Textbox(label="Enter your text here")
44
+ with gr.Row():
45
+ submit_button = gr.Button("Submit")
46
+ with gr.Row():
47
+ dropdown = gr.Dropdown(choices=[], label="Select Sentence")
48
+ with gr.Row():
49
+ audio_output = gr.Audio(label="Audio of Selected Sentence")
50
+
51
+ submit_button.click(fn=update_dropdown, inputs=textbox, outputs=dropdown)
52
+ dropdown.change(fn=generate_audio, inputs=dropdown, outputs=audio_output)
53
+
54
+ app.launch()