CoolFace
Apppublic

Add-Vishnu/Pywhisper_CPP_ASR

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py54 linesDownload Raw Back to root
1import gradio as gr2import soundfile as sf3import tempfile4import shutil5import os6import librosa7import time8import numpy as np9import subprocess 10from pywhispercpp.model import Model11 12# model = Model('base.en', n_threads=6,models_dir="./Models") # Only english13# model = Model('base', n_threads=6,models_dir="./Models",language="hindi",translate=False)  # Multilingual14model = Model('medium', n_threads=6,models_dir="./Models",language="hindi",translate=False)  # Multilingual15 16def resample_to_16k(audio, orig_sr):17    y_resampled = librosa.resample(y=audio, orig_sr=orig_sr, target_sr = 16000)18    return y_resampled19 20def transcribe(audio):21    print(type(audio))22    sr,y = audio23    y = y.astype(np.float32)24    y /= np.max(np.abs(y))25    y_resampled = resample_to_16k(y, sr)26    27    28    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:29        temp_audio_path = temp_audio.name30        sf.write(temp_audio_path, y_resampled, 16000)31 32    start_time_py = time.time()33    py_result = model.transcribe(f'{temp_audio_path}', n_threads=6)34    end_time_py = time.time()35    print("Py_result : ",py_result)36    print("--------------------------")37    print(f"Execution time using py: {end_time_py - start_time_py} seconds")38    output_text = ""39    for segment in py_result:40        output_text+=segment.text41    return output_text, (end_time_py - start_time_py)42 43 44 45demo = gr.Interface(46    transcribe,47    inputs = "microphone",48    # gr.Audio(sources=["microphone"]),49    outputs=[gr.Textbox(label="Py_Transcription"),gr.Textbox(label="Time taken for Transcription")],50    # examples=["./Samples/Hindi_1.mp3","./Samples/Hindi_2.mp3","./Samples/Tamil_1.mp3","./Samples/Tamil_2.mp3","./Samples/Marathi_1.mp3","./Samples/Marathi_2.mp3","./Samples/Nepal_1.mp3","./Samples/Nepal_2.mp3","./Samples/Telugu_1.wav","./Samples/Telugu_2.wav","./Samples/Malayalam_1.wav","./Samples/Malayalam_2.wav","./Samples/Gujarati_1.wav","./Samples/Gujarati_2.wav","./Samples/Bengali_1.wav","./Samples/Bengali_2.wav"]51    examples=["./Samples/Hindi_1.mp3","./Samples/Hindi_2.mp3","./Samples/Hindi_3.mp3","./Samples/Hindi_4.mp3","./Samples/Hindi_5.mp3"] # only hindi   # ,"./Samples/Tamil_1.mp3","./Samples/Tamil_2.mp3","./Samples/Marathi_1.mp3","./Samples/Marathi_2.mp3","./Samples/Nepal_1.mp3","./Samples/Nepal_2.mp3","./Samples/Telugu_1.wav","./Samples/Telugu_2.wav","./Samples/Malayalam_1.wav","./Samples/Malayalam_2.wav","./Samples/Gujarati_1.wav","./Samples/Gujarati_2.wav","./Samples/Bengali_1.wav","./Samples/Bengali_2.wav"]52)53 54demo.launch()