CoolFace
Apppublic

piealamodewhitebread/SillyTavern-Extras1

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
whisper_module.py56 linesDownload Raw Back to speech_recognition
1"""2Speech-to-text module based on Whisper for SillyTavern Extras3    - Whisper github: https://github.com/openai/whisper4 5Authors:6    - Tony Ribeiro (https://github.com/Tony-sama)7 8Models are saved into user cache folder, example: C:/Users/toto/.cache/whisper9 10References:11    - Code adapted from:12        - whisper github: https://github.com/openai/whisper13        - oobabooga text-generation-webui github: https://github.com/oobabooga/text-generation-webui14"""15from flask import jsonify, abort, request16 17import whisper18 19DEBUG_PREFIX = "<stt whisper module>"20RECORDING_FILE_PATH = "stt_test.wav"21 22model = None23 24def load_model(file_path=None):25    """26    Load given vosk model from file or default to en-us model.27    Download model to user cache folder, example: C:/Users/toto/.cache/vosk28    """29 30    if file_path is None:31        return whisper.load_model("base.en")32    else:33        return whisper.load_model(file_path)34    35def process_audio():36    """37    Transcript request audio file to text using Whisper38    """39 40    if model is None:41        print(DEBUG_PREFIX,"Whisper model not initialized yet.")42        return ""43 44    try:    45        file = request.files.get('AudioFile')46        file.save(RECORDING_FILE_PATH)47          48        result = model.transcribe(RECORDING_FILE_PATH)49        transcript = result["text"]50        print(DEBUG_PREFIX, "Transcripted from audio file (whisper):", transcript)51 52        return jsonify({"transcript": transcript})53 54    except Exception as e: # No exception observed during test but we never know55        print(e)56        abort(500, DEBUG_PREFIX+" Exception occurs while processing audio")