Spaces:
Sleeping
Sleeping
Update vosk_handler.py
Browse files- vosk_handler.py +28 -28
vosk_handler.py
CHANGED
|
@@ -1,28 +1,28 @@
|
|
| 1 |
-
from vosk import Model, KaldiRecognizer
|
| 2 |
-
import wave
|
| 3 |
-
import json
|
| 4 |
-
|
| 5 |
-
class VoskTranscriber:
|
| 6 |
-
def __init__(self, model_path=
|
| 7 |
-
self.model = Model(model_path)
|
| 8 |
-
|
| 9 |
-
def transcribe_audio(self, audio_data):
|
| 10 |
-
try:
|
| 11 |
-
with wave.open(audio_data, "rb") as wf:
|
| 12 |
-
recognizer = KaldiRecognizer(self.model, wf.getframerate())
|
| 13 |
-
recognizer.SetWords(True)
|
| 14 |
-
|
| 15 |
-
text = ""
|
| 16 |
-
while data := wf.readframes(4000):
|
| 17 |
-
if recognizer.AcceptWaveform(data):
|
| 18 |
-
text += json.loads(recognizer.Result())["text"] + " "
|
| 19 |
-
|
| 20 |
-
text += json.loads(recognizer.FinalResult())["text"]
|
| 21 |
-
return {"success": True, "text": text.strip()}
|
| 22 |
-
except Exception as e:
|
| 23 |
-
return {"success": False, "error": str(e)}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if __name__ == "__main__":
|
| 27 |
-
transcriber = VoskTranscriber()
|
| 28 |
-
print(transcriber.transcribe_audio("output.wav"))
|
|
|
|
| 1 |
+
from vosk import Model, KaldiRecognizer
|
| 2 |
+
import wave
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
class VoskTranscriber:
|
| 6 |
+
def __init__(self, model_path="Vosk/vosk-model-small-en-us-0.15"):
|
| 7 |
+
self.model = Model(model_path)
|
| 8 |
+
|
| 9 |
+
def transcribe_audio(self, audio_data):
|
| 10 |
+
try:
|
| 11 |
+
with wave.open(audio_data, "rb") as wf:
|
| 12 |
+
recognizer = KaldiRecognizer(self.model, wf.getframerate())
|
| 13 |
+
recognizer.SetWords(True)
|
| 14 |
+
|
| 15 |
+
text = ""
|
| 16 |
+
while data := wf.readframes(4000):
|
| 17 |
+
if recognizer.AcceptWaveform(data):
|
| 18 |
+
text += json.loads(recognizer.Result())["text"] + " "
|
| 19 |
+
|
| 20 |
+
text += json.loads(recognizer.FinalResult())["text"]
|
| 21 |
+
return {"success": True, "text": text.strip()}
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return {"success": False, "error": str(e)}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
transcriber = VoskTranscriber()
|
| 28 |
+
print(transcriber.transcribe_audio("output.wav"))
|