Pranaym12/2-LiveASR
0
1import gradio as gr2import torch3import time4import librosa5import soundfile6import nemo.collections.asr as nemo_asr7import tempfile8import os9import uuid10 11from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration12import torch13 14# PersistDataset -----15import os16import csv17import gradio as gr18from gradio import inputs, outputs19import huggingface_hub20from huggingface_hub import Repository, hf_hub_download, upload_file21from datetime import datetime22 23# ---------------------------------------------24# Dataset and Token links - change awacke1 to your own HF id, and add a HF_TOKEN copy to your repo for write permissions25# This should allow you to save your results to your own Dataset hosted on HF. 26 27DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/ASRLive.csv"28DATASET_REPO_ID = "awacke1/ASRLive.csv"29DATA_FILENAME = "ASRLive.csv"30DATA_FILE = os.path.join("data", DATA_FILENAME)31HF_TOKEN = os.environ.get("HF_TOKEN")32 33PersistToDataset = False34#PersistToDataset = True # uncomment to save inference output to ASRLive.csv dataset35 36if PersistToDataset:37 try:38 hf_hub_download(39 repo_id=DATASET_REPO_ID,40 filename=DATA_FILENAME,41 cache_dir=DATA_DIRNAME,42 force_filename=DATA_FILENAME43 )44 except:45 print("file not found")46 repo = Repository(47 local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN48 )49 50def store_message(name: str, message: str):51 if name and message:52 with open(DATA_FILE, "a") as csvfile:53 writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])54 writer.writerow(55 {"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}56 )57 # uncomment line below to begin saving - 58 commit_url = repo.push_to_hub()59 ret = ""60 with open(DATA_FILE, "r") as csvfile:61 reader = csv.DictReader(csvfile)62 63 for row in reader:64 ret += row65 ret += "\r\n"66 return ret 67 68# main -------------------------69mname = "facebook/blenderbot-400M-distill"70model = BlenderbotForConditionalGeneration.from_pretrained(mname)71tokenizer = BlenderbotTokenizer.from_pretrained(mname)72 73def take_last_tokens(inputs, note_history, history):74 filterTokenCount = 128 # filter last 128 tokens75 if inputs['input_ids'].shape[1] > filterTokenCount:76 inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-filterTokenCount:].tolist()])77 inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-filterTokenCount:].tolist()])78 note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]79 history = history[1:]80 return inputs, note_history, history81 82def add_note_to_history(note, note_history):83 note_history.append(note)84 note_history = '</s> <s>'.join(note_history)85 return [note_history]86 87 88 89SAMPLE_RATE = 1600090model = nemo_asr.models.EncDecRNNTBPEModel.from_pretrained("nvidia/stt_en_conformer_transducer_xlarge")91model.change_decoding_strategy(None)92model.eval()93 94def process_audio_file(file):95 data, sr = librosa.load(file)96 if sr != SAMPLE_RATE:97 data = librosa.resample(data, orig_sr=sr, target_sr=SAMPLE_RATE)98 data = librosa.to_mono(data)99 return data100 101 102def transcribe(audio, state = ""): 103 if state is None:104 state = ""105 audio_data = process_audio_file(audio)106 with tempfile.TemporaryDirectory() as tmpdir:107 audio_path = os.path.join(tmpdir, f'audio_{uuid.uuid4()}.wav')108 soundfile.write(audio_path, audio_data, SAMPLE_RATE)109 transcriptions = model.transcribe([audio_path])110 if type(transcriptions) == tuple and len(transcriptions) == 2:111 transcriptions = transcriptions[0]112 transcriptions = transcriptions[0]113 114 if PersistToDataset:115 ret = store_message(transcriptions, state) # Save to dataset - uncomment to store into a dataset - hint you will need your HF_TOKEN116 state = state + transcriptions + " " + ret117 else:118 state = state + transcriptions119 return state, state120 121gr.Interface(122 fn=transcribe,123 inputs=[124 gr.Audio(source="microphone", type='filepath', streaming=True),125 "state",126 ],127 outputs=[128 "textbox",129 "state"130 ],131 layout="horizontal",132 theme="huggingface",133 title="🗣️ASR-Gradio-Live🧠💾",134 description=f"Live Automatic Speech Recognition (ASR).",135 allow_flagging='never',136 live=True, 137 article=f"Result💾 Dataset: [{DATASET_REPO_URL}]({DATASET_REPO_URL})"138).launch(debug=True)139 