Created
June 7, 2023 06:44
-
-
Save ehzawad/86dabe2c1d3c82d7e9d35783601fe9f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import gradio as gr | |
| import numpy as np | |
| from vosk import KaldiRecognizer, Model | |
| model = Model(model_path="/home/gns/vosk-api/model/") | |
| def reformat_freq(sr, y): | |
| if sr not in ( | |
| 48000, | |
| 16000, | |
| 8000 | |
| ): | |
| raise ValueError("Unsupported rate", sr) | |
| if sr == 48000: | |
| y = ( | |
| ((y / max(np.max(y), 1)) * 32767) | |
| .reshape((-1, 6)) | |
| .mean(axis=1) | |
| .astype("int16").tobytes() | |
| ) | |
| sr = 8000 | |
| elif sr == 16000: | |
| y = ( | |
| ((y / max(np.max(y), 1)) * 32767) | |
| .reshape((-1, 2)) | |
| .mean(axis=1) | |
| .astype("int16") | |
| ) | |
| sr = 8000 | |
| return sr, y | |
| def transcribe(data, state): | |
| sample_rate, audio_data = data | |
| sr, y = reformat_freq(sample_rate, audio_data) | |
| print('new sample rate: '+str(sr)) | |
| if state is None: | |
| rec = KaldiRecognizer(model, 8000) | |
| result = [] | |
| else: | |
| rec, result = state | |
| if rec.AcceptWaveform(y): | |
| text_result = json.loads(rec.Result())["text"] | |
| if text_result != "": | |
| result.append(text_result) | |
| partial_result = "" | |
| else: | |
| partial_result = json.loads(rec.PartialResult())["partial"] + " " | |
| print("\n".join(result) + "\n" + partial_result, (rec, result)) | |
| return "\n".join(result) + "\n" + partial_result, (rec, result) | |
| gr.Interface( | |
| fn=transcribe, | |
| inputs=[ | |
| gr.Audio(source="microphone", type="numpy", streaming=True), | |
| "state" | |
| ], | |
| outputs=[ | |
| "textbox", | |
| "state" | |
| ], | |
| live=True).launch(share=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment