1

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
Xenia
  • 87
  • 6
  • Anywhere you can use a file-object you can usually also use a file-like object, like a `io.BytesIO` object. This allows you do do all the things you'd normally do with files without having to interact with the filesystem at all. – Paul M. Dec 25 '22 at 09:36
  • May I ask: It seems like first you're opening an audio file from your filesystem via `song = AudioSegment.from_file(file)`, and then writing a copy of that audio file to the filesystem via `song.export("audioexport.wav", format="wav")`. Then, you open the copy via `wavv = AudioSegment.from_wav("audioexport.wav")`. Is the original file not a WAVE file, or why do you make a copy first? – Paul M. Dec 25 '22 at 11:56
  • Is `file` a path to an audio file on your local file system? – Paul M. Dec 25 '22 at 11:58
  • Yes, you're right. I shouldn't copy it again. Before I was sending files that were not wave. – Xenia Dec 25 '22 at 12:45
  • I send the file from my device to Flask API that it's stored in hosting. – Xenia Dec 25 '22 at 12:46

1 Answers1

0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 19:10