ajchri5 commited on
Commit
0795d9e
·
verified ·
1 Parent(s): 7cbb27b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -1,7 +1,17 @@
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline, EncoderDecoderCache
3
  import torchaudio
4
  import warnings
 
5
 
6
  # Suppress specific warnings related to PySoundFile fallback
7
  warnings.filterwarnings("ignore", category=UserWarning, message="PySoundFile failed.*")
@@ -10,16 +20,26 @@ warnings.filterwarnings("ignore", category=UserWarning, message="PySoundFile fai
10
  whisper_model_name = "openai/whisper-large"
11
  processor = WhisperProcessor.from_pretrained(whisper_model_name)
12
  whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
13
- lang_detect_model = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
 
 
 
 
 
14
 
15
  # Function to load audio and transcribe it
16
- def transcribe_audio(audio_file):
17
  # Load the audio file using torchaudio
18
  waveform, sr = torchaudio.load(audio_file, normalize=True)
 
 
19
  if sr != 16000:
20
- transform = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000)
21
- waveform = transform(waveform) # Resample to 16kHz
22
- inputs = processor(waveform.squeeze(0).numpy(), return_tensors="pt", sampling_rate=16000) # Explicitly set sr=16000
 
 
 
23
 
24
  # Generate transcription and handle past_key_values deprecation
25
  past_key_values = None
@@ -34,16 +54,25 @@ def transcribe_audio(audio_file):
34
 
35
  return processor.decode(generated_ids[0], skip_special_tokens=True)
36
 
37
- # Function to detect language
38
  def detect_language(text):
39
- result = lang_detect_model(text, candidate_labels=["en", "fr", "es", "de", "it", "pt", "zh", "ja", "ar", "hi"])
40
- return result['labels'][0], result['scores'][0] # Return detected language and score
 
 
41
 
42
- # Main function to process audio, transcribe and detect language
43
- def process_audio(audio_file):
44
- transcription = transcribe_audio(audio_file)
45
- language, score = detect_language(transcription)
46
- return transcription, language, score
 
 
 
 
 
 
 
47
 
48
  # Gradio interface
49
  with gr.Blocks() as demo:
@@ -51,8 +80,10 @@ with gr.Blocks() as demo:
51
  transcription_output = gr.Textbox(label="Transcription") # Transcription output
52
  language_output = gr.Textbox(label="Detected Language") # Detected language output
53
  score_output = gr.Textbox(label="Confidence Score") # Confidence score output
 
 
54
  process_button = gr.Button("Process Audio") # Button to process the audio
55
 
56
- process_button.click(fn=process_audio, inputs=[audio_input], outputs=[transcription_output, language_output, score_output])
57
 
58
- demo.launch(debug=True)
 
1
+ # Install required packages in Colab
2
+ !pip install gradio
3
+ !pip install transformers
4
+ !pip install torchaudio
5
+ !pip install fasttext
6
+
7
+ # Download fastText language detection model
8
+ !wget https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin
9
+
10
  import gradio as gr
11
  from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline, EncoderDecoderCache
12
  import torchaudio
13
  import warnings
14
+ import fasttext # Import fastText for language detection
15
 
16
  # Suppress specific warnings related to PySoundFile fallback
17
  warnings.filterwarnings("ignore", category=UserWarning, message="PySoundFile failed.*")
 
20
  whisper_model_name = "openai/whisper-large"
21
  processor = WhisperProcessor.from_pretrained(whisper_model_name)
22
  whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
23
+
24
+ # Load Helsinki-NLP's opus-100 translation model
25
+ translation_model = pipeline("translation", model="Helsinki-NLP/opus-mt-ROMANCE-en") # A multilingual model from Opus-100
26
+
27
+ # Load the fastText language detection model
28
+ lang_model = fasttext.load_model('lid.176.bin') # This is the pre-trained model for language detection
29
 
30
  # Function to load audio and transcribe it
31
+ def transcribe_audio(audio_file, sampling_rate=48000): # Set to 48 kHz
32
  # Load the audio file using torchaudio
33
  waveform, sr = torchaudio.load(audio_file, normalize=True)
34
+
35
+ # If the audio is not 16 kHz, resample it
36
  if sr != 16000:
37
+ transform = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000) # Resample to 16 kHz
38
+ waveform = transform(waveform)
39
+ sr = 16000 # Update the sample rate to 16 kHz
40
+
41
+ # Process with WhisperProcessor
42
+ inputs = processor(waveform.squeeze(0).numpy(), return_tensors="pt", sampling_rate=sr) # Explicitly set sr to 16000
43
 
44
  # Generate transcription and handle past_key_values deprecation
45
  past_key_values = None
 
54
 
55
  return processor.decode(generated_ids[0], skip_special_tokens=True)
56
 
57
+ # Function to detect language using fastText
58
  def detect_language(text):
59
+ result = lang_model.predict(text) # Predict language with fastText
60
+ language = result[0][0].replace('__label__', '') # Extract the predicted language label
61
+ score = result[1][0] # Confidence score
62
+ return language, score
63
 
64
+ # Function to translate text (to English)
65
+ def translate_text_to_english(text, source_lang="fr"): # Translate to English
66
+ # Translate from the detected language to English
67
+ translation = translation_model(text, src_lang=source_lang, tgt_lang="en")
68
+ return translation[0]['translation_text']
69
+
70
+ # Main function to process audio, transcribe, detect language, and translate
71
+ def process_audio(audio_file):
72
+ transcription = transcribe_audio(audio_file, sampling_rate=48000) # Use 48 kHz initially
73
+ language, score = detect_language(transcription) # Detect language of the transcription
74
+ translated_text = translate_text_to_english(transcription, source_lang=language) # Translate to English
75
+ return transcription, language, score, translated_text
76
 
77
  # Gradio interface
78
  with gr.Blocks() as demo:
 
80
  transcription_output = gr.Textbox(label="Transcription") # Transcription output
81
  language_output = gr.Textbox(label="Detected Language") # Detected language output
82
  score_output = gr.Textbox(label="Confidence Score") # Confidence score output
83
+ translated_output = gr.Textbox(label="Translated Text to English") # Translated text output
84
+
85
  process_button = gr.Button("Process Audio") # Button to process the audio
86
 
87
+ process_button.click(fn=process_audio, inputs=[audio_input], outputs=[transcription_output, language_output, score_output, translated_output])
88
 
89
+ demo.launch(debug=True)