Spaces:
Running
on
Zero
Running
on
Zero
emirhanbilgic
commited on
Commit
•
347bb89
1
Parent(s):
00f7498
Update app.py
Browse files
app.py
CHANGED
@@ -93,45 +93,19 @@ def update_target_lang_options(source_lang):
|
|
93 |
# Function to process sentences for audio generation
|
94 |
def process_sentences_for_audio(sentences, description, tts_model, tts_tokenizer):
|
95 |
audio_files = []
|
96 |
-
outputs = []
|
97 |
|
98 |
for i, sentence in enumerate(sentences):
|
99 |
print(f"Generating audio for sentence {i+1}...")
|
100 |
output_file_prefix = f"sentence_{i+1}"
|
101 |
audio_file = generate_single_wav_from_text(sentence, description, output_file_prefix, tts_model, tts_tokenizer)
|
102 |
audio_files.append(audio_file)
|
103 |
-
outputs.append((sentence, audio_file))
|
104 |
|
105 |
-
|
106 |
-
gr.Markdown(f"**Sentence**: {sentence}")
|
107 |
-
gr.Audio(value=audio_file, label=sentence)
|
108 |
-
|
109 |
-
return outputs, audio_files
|
110 |
-
|
111 |
-
# Main Gradio function
|
112 |
-
def process_pdf(pdf_file, translate_checkbox, source_lang, target_lang, description, tts_model, tts_tokenizer):
|
113 |
-
print("Extracting text from PDF...")
|
114 |
-
text = pdf_to_text(pdf_file.name)
|
115 |
-
|
116 |
-
# Translate if translation checkbox is selected
|
117 |
-
if translate_checkbox:
|
118 |
-
print("Translating text...")
|
119 |
-
text = translate(text, source_lang, target_lang)
|
120 |
-
|
121 |
-
print("Splitting text into sentences...")
|
122 |
-
sentences = split_text_into_sentences(text)
|
123 |
-
|
124 |
-
# Process sentences for audio generation
|
125 |
-
outputs, audio_files = process_sentences_for_audio(sentences, description, tts_model, tts_tokenizer)
|
126 |
|
127 |
-
print("Combining all audio files...")
|
128 |
combined_output_file = "sentences_combined.wav"
|
129 |
combine_wav_files(combined_output_file, *audio_files)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
# Return the sentences with their corresponding audio files
|
134 |
-
return [(sentence, audio_file) for sentence, audio_file in outputs], combined_output_file
|
135 |
|
136 |
# Gradio interface
|
137 |
with gr.Blocks() as demo:
|
@@ -149,12 +123,20 @@ with gr.Blocks() as demo:
|
|
149 |
output_group = gr.Group()
|
150 |
|
151 |
def handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
def handle_translation_toggle(translate_checkbox):
|
160 |
if translate_checkbox:
|
|
|
93 |
# Function to process sentences for audio generation
|
94 |
def process_sentences_for_audio(sentences, description, tts_model, tts_tokenizer):
|
95 |
audio_files = []
|
|
|
96 |
|
97 |
for i, sentence in enumerate(sentences):
|
98 |
print(f"Generating audio for sentence {i+1}...")
|
99 |
output_file_prefix = f"sentence_{i+1}"
|
100 |
audio_file = generate_single_wav_from_text(sentence, description, output_file_prefix, tts_model, tts_tokenizer)
|
101 |
audio_files.append(audio_file)
|
|
|
102 |
|
103 |
+
yield sentence, audio_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
|
|
105 |
combined_output_file = "sentences_combined.wav"
|
106 |
combine_wav_files(combined_output_file, *audio_files)
|
107 |
|
108 |
+
yield None, combined_output_file
|
|
|
|
|
|
|
109 |
|
110 |
# Gradio interface
|
111 |
with gr.Blocks() as demo:
|
|
|
123 |
output_group = gr.Group()
|
124 |
|
125 |
def handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
|
126 |
+
text = pdf_to_text(pdf_input.name)
|
127 |
+
if translate_checkbox:
|
128 |
+
text = translate(text, source_lang, target_lang)
|
129 |
+
|
130 |
+
sentences = split_text_into_sentences(text)
|
131 |
+
for sentence, audio_file in process_sentences_for_audio(sentences, description, tts_model, tts_tokenizer):
|
132 |
+
if sentence:
|
133 |
+
with output_group:
|
134 |
+
gr.Markdown(f"**Sentence**: {sentence}")
|
135 |
+
gr.Audio(value=audio_file, label=sentence)
|
136 |
+
else:
|
137 |
+
with output_group:
|
138 |
+
gr.Markdown("### Combined Audio")
|
139 |
+
gr.Audio(value=audio_file, label="Combined Audio")
|
140 |
|
141 |
def handle_translation_toggle(translate_checkbox):
|
142 |
if translate_checkbox:
|