Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import gradio as grimport nemo.collections.asr as nemo_asrfrom pydub import AudioSegmentimport osfrom huggingface_hub import login# Fetch the token from an environment variableHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:raise ValueError("HF_TOKEN environment variable not set. Please provide a valid Hugging Face token.")# Authenticate with Hugging Facelogin(HF_TOKEN)# Load the private NeMo ASR modeltry:asr_model = nemo_asr.models.EncDecHybridRNNTCTCBPEModel.from_pretrained(model_name="faimlab/stt_fa_fastconformer_hybrid_large_dataset_v30")except Exception as e:raise RuntimeError(f"Failed to load model: {str(e)}")# Function to convert audio to 16kHz mono WAVdef convert_to_wav(audio_path, output_path="temp.wav"):audio = AudioSegment.from_file(audio_path)audio = audio.set_channels(1).set_frame_rate(16000)audio.export(output_path, format="wav")return output_path# Transcription functiondef transcribe_audio(audio):if audio is None:return "Please upload an audio file."wav_path = convert_to_wav(audio)output = asr_model.transcribe([wav_path])if os.path.exists(wav_path):os.remove(wav_path)return output[0].text# Create Gradio interfaceinterface = gr.Interface(fn=transcribe_audio,inputs=gr.Audio(type="filepath", label="Upload Audio File"),outputs=gr.Textbox(label="Transcription"),title="ASR Transcription with NeMo",description="Upload an audio file to transcribe it using a private NeMo ASR model.")# Launch the appinterface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154import gradio as grimport nemo.collections.asr as nemo_asrfrom pydub import AudioSegmentimport osimport yt_dlp as youtube_dlfrom huggingface_hub import loginfrom hazm import Normalizerimport numpy as npimport reimport time# Fetch the token from an environment variableHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:raise ValueError("HF_TOKEN environment variable not set. Please provide a valid Hugging Face token.")# Authenticate with Hugging Facelogin(HF_TOKEN)# Load the private NeMo ASR modeltry:asr_model = nemo_asr.models.EncDecHybridRNNTCTCBPEModel.from_pretrained(model_name="faimlab/stt_fa_fastconformer_hybrid_large_dataset_v30")except Exception as e:raise RuntimeError(f"Failed to load model: {str(e)}")normalizer = Normalizer()def load_audio(audio_path):audio = AudioSegment.from_file(audio_path)audio = audio.set_channels(1).set_frame_rate(16000)audio_samples = np.array(audio.get_array_of_samples(), dtype=np.float32)audio_samples /= np.max(np.abs(audio_samples))return audio_samples, audio.frame_ratedef transcribe_chunk(audio_chunk, model):transcription = model.transcribe([audio_chunk], batch_size=1, verbose=False)return transcription[0].textdef transcribe_audio(file_path, model, chunk_size=30*16000):waveform, _ = load_audio(file_path)transcriptions = []for start in range(0, len(waveform), chunk_size):end = min(len(waveform), start + chunk_size)transcription = transcribe_chunk(waveform[start:end], model)transcriptions.append(transcription)transcriptions = ' '.join(transcriptions)transcriptions = re.sub(' +', ' ', transcriptions)transcriptions = normalizer.normalize(transcriptions)return transcriptions# YouTube audio download functionYT_LENGTH_LIMIT_S = 3600def download_yt_audio(yt_url, filename, cookie_file="cookies.txt"):info_loader = youtube_dl.YoutubeDL()try:info = info_loader.extract_info(yt_url, download=False)except youtube_dl.utils.DownloadError as err:raise gr.Error(str(err))file_length = info["duration_string"]file_h_m_s = file_length.split(":")file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]if len(file_h_m_s) == 1:file_h_m_s.insert(0, 0)if len(file_h_m_s) == 2:file_h_m_s.insert(0, 0)file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]if file_length_s > YT_LENGTH_LIMIT_S:yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "cookies": cookie_file}with youtube_dl.YoutubeDL(ydl_opts) as ydl:try:ydl.download([yt_url])except youtube_dl.utils.ExtractorError as err:raise gr.Error(str(err))# Gradio Interfacedef transcribe(audio):if audio is None:return "Please upload an audio file."transcription = transcribe_audio(audio, asr_model)return transcriptiondef transcribe_yt(yt_url):temp_filename = "/tmp/yt_audio.mp4" # Temporary filename for the downloaded videodownload_yt_audio(yt_url, temp_filename)transcription = transcribe_audio(temp_filename, asr_model)return transcriptionmf_transcribe = gr.Interface(fn=transcribe,inputs=gr.Microphone(type="filepath"),outputs=gr.Textbox(label="Transcription"),theme="huggingface",title="Persian ASR Transcription with NeMo Fast Conformer",description=("Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the NeMo's Fast Conformer Hybrid Large.\n\n""Trained on ~800 hours of Persian speech dataset (Common Voice 17 (~300 hours), YouTube (~400 hours), NasleMana (~90 hours), In-house dataset (~70 hours)).\n\n""For commercial applications, contact us via email: <saeedzou2012@gmail.com>.\n\n""Credit FAIM Group, Sharif University of Technology.\n\n"),allow_flagging="never",)# File upload tabfile_transcribe = gr.Interface(fn=transcribe,inputs=gr.Audio(type="filepath", label="Audio file"),outputs=gr.Textbox(label="Transcription"),theme="huggingface",title="Persian ASR Transcription with NeMo Fast Conformer",description=("Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the NeMo's Fast Conformer Hybrid Large.\n\n""Trained on ~800 hours of Persian speech dataset (Common Voice 17 (~300 hours), YouTube (~400 hours), NasleMana (~90 hours), In-house dataset (~70 hours)).\n\n""For commercial applications, contact us via email: <saeedzou2012@gmail.com>.\n\n""Credit FAIM Group, Sharif University of Technology.\n\n"),allow_flagging="never",)# YouTube tabyt_transcribe = gr.Interface(fn=transcribe_yt,inputs=gr.Textbox(label="YouTube URL", placeholder="Enter the YouTube URL here"),outputs=gr.Textbox(label="Transcription"),theme="huggingface",title="Transcribe YouTube Video",description="Transcribe audio from a YouTube video by providing its URL. Currently YouTube is blocking the requests. So you will see the app showing error",allow_flagging="never",)# Gradio Interfacedemo = gr.Blocks()with demo:# Create the tabs with the list of interfacesgr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])demo.launch()
app.py+154-53