Mohssinibra commited on
Commit
1a38424
·
verified ·
1 Parent(s): d27a60f
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -12,7 +12,10 @@ translation_model = MBartForConditionalGeneration.from_pretrained("facebook/mbar
12
  translation_tokenizer = MBart50Tokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="ar_AR")
13
 
14
  def transcribe_audio(audio):
15
- # Load the audio file from Gradio interface
 
 
 
16
  audio_array, sr = librosa.load(audio, sr=16000)
17
 
18
  # Tokenize the audio file
@@ -44,16 +47,16 @@ def translate_text(text):
44
 
45
  return translated_text
46
 
47
- # Create a Gradio interface for uploading audio or recording from the browser
48
- demo = gr.Interface(fn=transcribe_audio,
49
- inputs=gr.Audio(type="filepath"), # Corrected input component
50
- outputs=["text", "text"], # Both transcription and translation outputs
51
- live=False, # Disable live to prevent automatic execution
52
- title="Speech-to-Text and Translation",
53
- description="Upload an audio file to transcribe and translate it from Darija to English.")
54
-
55
- # Add a submit button
56
- demo.add_button("Submit")
 
57
 
58
  demo.launch()
59
- demo.launch(api=True, share=True)
 
12
  translation_tokenizer = MBart50Tokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="ar_AR")
13
 
14
  def transcribe_audio(audio):
15
+ if not audio:
16
+ return "No audio file provided", "No translation available"
17
+
18
+ # Load the audio file
19
  audio_array, sr = librosa.load(audio, sr=16000)
20
 
21
  # Tokenize the audio file
 
47
 
48
  return translated_text
49
 
50
+ # Create Gradio Blocks for better UI
51
+ with gr.Blocks() as demo:
52
+ gr.Markdown("# Speech-to-Text and Translation")
53
+ gr.Markdown("Upload an audio file to transcribe and translate it from Darija to English.")
54
+
55
+ audio_input = gr.Audio(type="filepath", label="Upload or Record Audio")
56
+ submit_button = gr.Button("Submit")
57
+ transcription_output = gr.Textbox(label="Transcription (Darija)")
58
+ translation_output = gr.Textbox(label="Translation (English)")
59
+
60
+ submit_button.click(fn=transcribe_audio, inputs=audio_input, outputs=[transcription_output, translation_output])
61
 
62
  demo.launch()