Futuresony commited on
Commit
421def4
·
verified ·
1 Parent(s): 0f1e860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -1,23 +1,28 @@
1
  import gradio as gr
2
  import librosa
3
  from asr import transcribe, ASR_EXAMPLES, ASR_NOTE
4
- from lid import identify # Import language identification model
5
 
6
- # Function to detect language and transcribe automatically
7
- def auto_detect_and_transcribe(audio):
8
- detected_lang = identify(audio) # Identify language from audio
9
- if detected_lang in ["swh", "eng"]: # Ensure it's either Swahili or English
10
- return f"[Detected Language: {detected_lang.upper()}]\n\n" + transcribe(audio)
11
- return "Error: Unsupported language detected."
 
 
 
 
 
12
 
13
  # Speech-to-Text Interface with Auto Language Detection
14
  mms_transcribe = gr.Interface(
15
- fn=auto_detect_and_transcribe,
16
  inputs=gr.Audio(),
17
  outputs="text",
18
  examples=ASR_EXAMPLES,
19
- title="Speech-to-Text (Automatic Language Detection)",
20
- description="Upload or record audio, and the model will detect if it is Swahili or English before transcribing.",
21
  article=ASR_NOTE,
22
  allow_flagging="never",
23
  )
@@ -25,11 +30,10 @@ mms_transcribe = gr.Interface(
25
  # Main Gradio App
26
  with gr.Blocks() as demo:
27
  gr.Markdown("<p align='center' style='font-size: 20px;'>MMS Speech-to-Text</p>")
28
- gr.HTML("<center>Convert speech to text while automatically detecting Swahili or English.</center>")
29
 
30
  mms_transcribe.render()
31
 
32
  if __name__ == "__main__":
33
  demo.queue()
34
  demo.launch()
35
-
 
1
  import gradio as gr
2
  import librosa
3
  from asr import transcribe, ASR_EXAMPLES, ASR_NOTE
4
+ from lid import identify # Import Language Identification model
5
 
6
+ # Function to detect language and transcribe speech
7
+ def auto_transcribe(audio):
8
+ # Detect language (returns language code like "eng" or "swh")
9
+ detected_lang = identify(audio)
10
+
11
+ # Ensure the detected language is Swahili or English
12
+ if detected_lang not in ["eng", "swh"]:
13
+ return "Error: Only English and Swahili are supported."
14
+
15
+ # Transcribe using detected language
16
+ return transcribe(audio, lang=detected_lang)
17
 
18
  # Speech-to-Text Interface with Auto Language Detection
19
  mms_transcribe = gr.Interface(
20
+ fn=auto_transcribe,
21
  inputs=gr.Audio(),
22
  outputs="text",
23
  examples=ASR_EXAMPLES,
24
+ title="Speech-to-Text (Auto Language Detection)",
25
+ description="Automatically detects whether speech is in Swahili or English and transcribes it.",
26
  article=ASR_NOTE,
27
  allow_flagging="never",
28
  )
 
30
  # Main Gradio App
31
  with gr.Blocks() as demo:
32
  gr.Markdown("<p align='center' style='font-size: 20px;'>MMS Speech-to-Text</p>")
33
+ gr.HTML("<center>Automatically detects and transcribes Swahili or English speech.</center>")
34
 
35
  mms_transcribe.render()
36
 
37
  if __name__ == "__main__":
38
  demo.queue()
39
  demo.launch()