Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline
|
| 3 |
-
import torchaudio
|
| 4 |
-
import fasttext
|
| 5 |
-
import os
|
| 6 |
-
import wget
|
| 7 |
-
|
| 8 |
-
# Path to the fastText language detection model
|
| 9 |
-
model_path = "models/lid.176.bin"
|
| 10 |
-
model_url = "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin"
|
| 11 |
-
|
| 12 |
-
# Check if the fastText model exists, and download it if it doesn't
|
| 13 |
-
if not os.path.exists(model_path):
|
| 14 |
-
print(f"Downloading fastText model from {model_url}...")
|
| 15 |
-
wget.download(model_url, model_path)
|
| 16 |
-
|
| 17 |
-
# Load the fastText model
|
| 18 |
-
lang_model = fasttext.load_model(model_path)
|
| 19 |
-
|
| 20 |
-
# Load Whisper model for transcription
|
| 21 |
-
whisper_model_name = "openai/whisper-large"
|
| 22 |
-
processor = WhisperProcessor.from_pretrained(whisper_model_name)
|
| 23 |
-
whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
|
| 24 |
-
|
| 25 |
-
# Load translation model (e.g., from Helsinki-NLP)
|
| 26 |
-
translation_model = pipeline("translation", model="Helsinki-NLP/opus-mt-ROMANCE-en")
|
| 27 |
-
|
| 28 |
-
# App usage history
|
| 29 |
-
history_data = []
|
| 30 |
-
|
| 31 |
-
# Function to save data to CSV
|
| 32 |
-
def saveData(text, language, translated_text, confidence_score):
|
| 33 |
-
file_path = '/content/drive/MyDrive/IT164/a2prompt.csv'
|
| 34 |
-
file_exists = os.path.isfile(file_path)
|
| 35 |
-
|
| 36 |
-
with open(file_path, 'a', newline='', encoding='utf-8') as f:
|
| 37 |
-
w = csv.writer(f)
|
| 38 |
-
if not file_exists:
|
| 39 |
-
w.writerow(['Text', 'Language', 'Translation', 'Confidence Score'])
|
| 40 |
-
w.writerow([text, language, translated_text, confidence_score])
|
| 41 |
-
|
| 42 |
-
# Function to transcribe audio using Whisper model
|
| 43 |
-
def transcribe_audio(audio_file, sampling_rate=48000):
|
| 44 |
-
waveform, sr = torchaudio.load(audio_file, normalize=True)
|
| 45 |
-
if sr != 16000:
|
| 46 |
-
transform = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000)
|
| 47 |
-
waveform = transform(waveform)
|
| 48 |
-
sr = 16000
|
| 49 |
-
|
| 50 |
-
inputs = processor(waveform.squeeze(0).numpy(), return_tensors="pt", sampling_rate=sr)
|
| 51 |
-
past_key_values = None
|
| 52 |
-
generated_ids = whisper_model.generate(inputs["input_features"], past_key_values=past_key_values)
|
| 53 |
-
|
| 54 |
-
if past_key_values is not None:
|
| 55 |
-
past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values)
|
| 56 |
-
|
| 57 |
-
return processor.decode(generated_ids[0], skip_special_tokens=True)
|
| 58 |
-
|
| 59 |
-
# Function to detect language using fastText
|
| 60 |
-
def detect_language(text):
|
| 61 |
-
result = lang_model.predict(text)
|
| 62 |
-
language = result[0][0].replace('__label__', '')
|
| 63 |
-
score = result[1][0]
|
| 64 |
-
return language, score
|
| 65 |
-
|
| 66 |
-
# Function to translate text to English
|
| 67 |
-
def translate_text_to_english(text, source_lang="fr"):
|
| 68 |
-
translation = translation_model(text, src_lang=source_lang, tgt_lang="en")
|
| 69 |
-
return translation[0]['translation_text']
|
| 70 |
-
|
| 71 |
-
# Function to track history and save results
|
| 72 |
-
def save_to_history(text, language, translation, confidence_score):
|
| 73 |
-
history_data.append([text, language, translation, confidence_score])
|
| 74 |
-
saveData(text, language, translation, confidence_score)
|
| 75 |
-
|
| 76 |
-
# Main processing function: transcribe, detect language, translate, save history
|
| 77 |
-
def process_audio(audio_file):
|
| 78 |
-
transcription = transcribe_audio(audio_file, sampling_rate=48000)
|
| 79 |
-
language, score = detect_language(transcription)
|
| 80 |
-
translated_text = translate_text_to_english(transcription, source_lang=language)
|
| 81 |
-
save_to_history(transcription, language, translated_text, score)
|
| 82 |
-
return transcription, language, score, translated_text
|
| 83 |
-
|
| 84 |
-
# Update visibility of the history table in Gradio
|
| 85 |
-
def update_vis(radio_value):
|
| 86 |
-
if radio_value == 'show':
|
| 87 |
-
return gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=True)
|
| 88 |
-
else:
|
| 89 |
-
return gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=False)
|
| 90 |
-
|
| 91 |
-
# Gradio interface
|
| 92 |
-
with gr.Blocks() as demo:
|
| 93 |
-
with gr.Row():
|
| 94 |
-
with gr.Column():
|
| 95 |
-
audio_input = gr.Audio(label="Record your voice", type="filepath")
|
| 96 |
-
transcription_output = gr.Textbox(label="Transcription")
|
| 97 |
-
language_output = gr.Textbox(label="Detected Language")
|
| 98 |
-
score_output = gr.Textbox(label="Confidence Score")
|
| 99 |
-
translated_output = gr.Textbox(label="Translated Text to English")
|
| 100 |
-
process_button = gr.Button("Process Audio")
|
| 101 |
-
|
| 102 |
-
with gr.Column():
|
| 103 |
-
history = gr.Radio(['show', 'hide'], label="App usage history")
|
| 104 |
-
dataframe = gr.DataFrame(pd.DataFrame(history_data, columns=["Text", "Language", "Translation", "Confidence Score"]), visible=False)
|
| 105 |
-
|
| 106 |
-
# Button click to process audio and display results
|
| 107 |
-
process_button.click(fn=process_audio, inputs=[audio_input], outputs=[transcription_output, language_output, score_output, translated_output])
|
| 108 |
-
history.change(fn=update_vis, inputs=history, outputs=dataframe)
|
| 109 |
-
|
| 110 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|