emirhanbilgic commited on
Commit
53b808e
·
verified ·
1 Parent(s): e666162

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -7,7 +7,8 @@ from transformers import AutoTokenizer, AutoFeatureExtractor, set_seed
7
  from PyPDF2 import PdfReader
8
  import re
9
  import textwrap
10
- import soundfile as sf
 
11
 
12
  # Device configuration
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -91,7 +92,7 @@ with gr.Blocks() as demo:
91
  value="Old man voice. Monotone voice tune from an old man, with a very close recording that almost has no background noise.")
92
  run_button = gr.Button("Generate Audio", variant="primary")
93
  with gr.Column():
94
- audio_gallery = gr.Gallery(label="Generated Audios", item_type="audio")
95
  markdown_output = gr.Markdown()
96
 
97
  def handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
@@ -100,25 +101,24 @@ with gr.Blocks() as demo:
100
  text = translate(text, source_lang, target_lang)
101
 
102
  sentences = split_text_into_sentences(text)
103
- all_audio_paths = []
104
  all_text = ""
105
 
106
  for sentence in sentences:
107
  sample_rate, audio_arr = generate_single_wav_from_text(sentence, description)
108
- # Create temporary audio file
109
- with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmpfile:
110
- sf.write(tmpfile, audio_arr, sample_rate)
111
- all_audio_paths.append(tmpfile.name)
112
  all_text += f"**Sentence**: {sentence}\n\n"
113
- yield all_audio_paths, all_text
114
 
115
  def run_pipeline(pdf_input, translate_checkbox, source_lang, target_lang, description):
116
- for audio_data, markdown_text in handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
117
- yield audio_data, markdown_text
 
 
 
118
 
119
- translate_checkbox.change(fn=handle_translation_toggle, inputs=translate_checkbox, outputs=[source_lang, target_lang])
120
- source_lang.change(fn=lambda lang: gr.update(choices={"en": ["de", "fr", "tr"], "tr": ["en"], "de": ["en", "fr"], "fr": ["en", "de"]}.get(lang, [])), inputs=source_lang, outputs=target_lang)
121
- run_button.click(run_pipeline, inputs=[pdf_input, translate_checkbox, source_lang, target_lang, description], outputs=[audio_gallery, markdown_output])
122
 
123
  demo.queue()
124
  demo.launch(share=True)
 
7
  from PyPDF2 import PdfReader
8
  import re
9
  import textwrap
10
+ import soundfile as SF
11
+ import numpy as np
12
 
13
  # Device configuration
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
92
  value="Old man voice. Monotone voice tune from an old man, with a very close recording that almost has no background noise.")
93
  run_button = gr.Button("Generate Audio", variant="primary")
94
  with gr.Column():
95
+ audio_container = gr.Column()
96
  markdown_output = gr.Markdown()
97
 
98
  def handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
 
101
  text = translate(text, source_lang, target_lang)
102
 
103
  sentences = split_text_into_sentences(text)
104
+ all_audio_data = []
105
  all_text = ""
106
 
107
  for sentence in sentences:
108
  sample_rate, audio_arr = generate_single_wav_from_text(sentence, description)
109
+ audio_data = (sample_rate, audio_arr)
110
+ all_audio_data.append(audio_data)
 
 
111
  all_text += f"**Sentence**: {sentence}\n\n"
112
+ yield all_audio_data, all_text
113
 
114
  def run_pipeline(pdf_input, translate_checkbox, source_lang, target_lang, description):
115
+ audio_container.clear_components() # Clear previous components
116
+ for audio_data_list, markdown_text in handle_process(pdf_input, translate_checkbox, source_lang, target_lang, description):
117
+ for sample_rate, audio_arr in audio_data_list:
118
+ audio_container.append(gr.Audio(value=(np.array(audio_arr).astype(np.float32), sample_rate)))
119
+ yield None, markdown_text
120
 
121
+ run_button.click(run_pipeline, inputs=[pdf_input, translate_checkbox, source_lang, target_lang, description], outputs=[audio_container, markdown_output])
 
 
122
 
123
  demo.queue()
124
  demo.launch(share=True)