[Python] Error when calling recasepunc.py with subprocess in Flask app: Error in recasepunc:...

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I am working on a Flask application that uses the Vosk speech recognition library to transcribe audio into text, then sends that text to the recasepunc.py script for punctuation restoration. The issue I’m encountering is that I get the following error:

    Error in recasepunc: None


    The error occurs when I try to pass the recognized text from Vosk to the recasepunc.py script via subprocess. The command I am trying to run is:

    processed_text = subprocess.check_output(
    'python recasepunc/recasepunc.py predict recasepunc/checkpoint', shell=True, text=True, input=recognized_text)


    Where recognized_text is the result from the Vosk recognition, like "hello how are you".

    Here’s the full context of the application code:

    from vosk import Model, KaldiRecognizer, SetLogLevel
    from pydub import AudioSegment
    from flask import Flask, request, jsonify
    import subprocess
    import json

    app = Flask(__name__)

    @app.route('/', methods=['POST'])
    def process_audio():
    file = request.files.get('111')

    if file:
    try:
    song = AudioSegment.from_file(file)
    SetLogLevel(0)
    FRAME_RATE = 16000
    CHANNELS = 1
    model = Model("model")
    rec = KaldiRecognizer(model, FRAME_RATE)
    rec.SetWords(True)

    wavv = song.set_channels(CHANNELS)
    wavv = song.set_frame_rate(FRAME_RATE)
    rec.AcceptWaveform(wavv.raw_data)
    result = rec.Result()

    recognized_text = json.loads(result)["text"]

    try:
    processed_text = subprocess.check_output(
    'python recasepunc/recasepunc.py predict recasepunc/checkpoint', shell=True, text=True, input=recognized_text)
    except subprocess.CalledProcessError as e:
    processed_text = f"Error in recasepunc: {e.stderr}"

    return jsonify({
    "recognized_text": recognized_text,
    "processed_text": processed_text
    })

    except Exception as e:
    return jsonify({"error": str(e)}), 500

    else:
    return jsonify({"error": "No audio file provided"}), 400

    if __name__ == "__main__":
    app.debug = True
    app.run(host="0.0.0.0", port=5000)

    What I’ve tried:


    1. I have confirmed that the command works directly in the terminal:

      echo "hello how are you" | python recasepunc/recasepunc.py predict recasepunc/checkpoint


      It returns the expected result: "Hello, How are you?".


    2. I tried using subprocess.check_output() as shown, but the output is "Error in recasepunc: None", which doesn’t provide any useful information for troubleshooting.
    My Environment:

    • Python 3.10
    • vosk 0.3.45
    • torch 2.2.2
    • transformers 4.39.3

    Could someone help me understand why this error occurs and how I can resolve it?

    Continue reading...

Compartilhe esta Página