ajchri5 commited on
Commit
779e433
·
verified ·
1 Parent(s): 54c13ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,17 +1,9 @@
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.*")
@@ -27,6 +19,9 @@ translation_model = pipeline("translation", model="Helsinki-NLP/opus-mt-ROMANCE-
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
@@ -67,23 +62,42 @@ def translate_text_to_english(text, source_lang="fr"): # Translate 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:
79
- audio_input = gr.Audio(label="Record your voice", type="filepath") # Audio input
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)
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline, EncoderDecoderCache
3
  import torchaudio
4
  import warnings
5
  import fasttext # Import fastText for language detection
6
+ import pandas as pd
7
 
8
  # Suppress specific warnings related to PySoundFile fallback
9
  warnings.filterwarnings("ignore", category=UserWarning, message="PySoundFile failed.*")
 
19
  # Load the fastText language detection model
20
  lang_model = fasttext.load_model('lid.176.bin') # This is the pre-trained model for language detection
21
 
22
+ # Initialize history tracking
23
+ history_data = [] # List to track transcription, detected language, translation, and confidence score
24
+
25
  # Function to load audio and transcribe it
26
  def transcribe_audio(audio_file, sampling_rate=48000): # Set to 48 kHz
27
  # Load the audio file using torchaudio
 
62
  translation = translation_model(text, src_lang=source_lang, tgt_lang="en")
63
  return translation[0]['translation_text']
64
 
65
+ # Function to track history (save results to the history list)
66
+ def save_to_history(text, language, translation, confidence_score):
67
+ history_data.append([text, language, translation, confidence_score]) # Save to the history list
68
+
69
  # Main function to process audio, transcribe, detect language, and translate
70
  def process_audio(audio_file):
71
  transcription = transcribe_audio(audio_file, sampling_rate=48000) # Use 48 kHz initially
72
  language, score = detect_language(transcription) # Detect language of the transcription
73
  translated_text = translate_text_to_english(transcription, source_lang=language) # Translate to English
74
+ save_to_history(transcription, language, translated_text, score) # Save results to history
75
  return transcription, language, score, translated_text
76
 
77
+ # Update visibility of the history table in Gradio
78
+ def update_vis(radio_value):
79
+ if radio_value == 'show':
80
+ return gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=True)
81
+ else:
82
+ return gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=False)
83
+
84
  # Gradio interface
85
  with gr.Blocks() as demo:
86
+ with gr.Row():
87
+ with gr.Column():
88
+ audio_input = gr.Audio(label="Record your voice", type="filepath") # Audio input
89
+ transcription_output = gr.Textbox(label="Transcription") # Transcription output
90
+ language_output = gr.Textbox(label="Detected Language") # Detected language output
91
+ score_output = gr.Textbox(label="Confidence Score") # Confidence score output
92
+ translated_output = gr.Textbox(label="Translated Text to English") # Translated text output
93
+ process_button = gr.Button("Process Audio") # Button to process the audio
94
+
95
+ with gr.Column():
96
+ history = gr.Radio(['show', 'hide'], label="App usage history") # Show or hide history
97
+ dataframe = gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=False)
98
+
99
+ # Button click will process the audio and display the output
100
+ process_button.click(fn=process_audio, inputs=[audio_input], outputs=[transcription_output, language_output, score_output, translated_output])
101
+ history.change(fn=update_vis, inputs=history, outputs=dataframe)
102
 
103
  demo.launch(debug=True)