How to manipulate audio without saving it in Flask with pydub

339 views Asked by At

I'm creating my first Flask API. I send audio file from React Native to Flask. Then I want to process it with vosk module (convert audio to text). Is it possible to manage it without saving the file on server? This is the code that works but saves the audio file:

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


app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])

def process_audio():
    file = request.files['111'] 
    song = AudioSegment.from_file(file)
    song.export("audioexport.wav", format="wav")

    SetLogLevel(0)
    FRAME_RATE = 16000
    CHANNELS=1

    model = Model("model")
    rec = KaldiRecognizer(model, FRAME_RATE)
    rec.SetWords(True)
 
    wavv = AudioSegment.from_wav("audioexport.wav")
    wavv = wavv.set_channels(CHANNELS)
    wavv = wavv.set_frame_rate(FRAME_RATE)
    

    rec.AcceptWaveform(wavv.raw_data)
    result = rec.Result()
    text = json.loads(result)["text"]

    return text
1

There are 1 answers

1
MT ERROR On

Sorry, it is not possible to convert without saving the file on the server.After the process you can delete the file