AssertionError: Given audio file must be a filename string or a file-like object

What im trying to do is to implement the API speech to text in my application. As a front end, I'm using react-native and as a back-end, I'm using python flask. The recording function is in the react-native. It is sent to the backend, I receive it, and Im trying to convert it to text, but this error is shown: assert isinstance(filename_or_fileobject, (type(""), type(u""))) or hasattr(filename_or_fileobject, "read"), "Given audio file must be a filename string or a file-like object" AssertionError: Given audio file must be a filename string or a file-like object

flask: app.py

app = Flask(__name__)
CORS(app)


@app.route('/audio', methods=['POST'])
def speech_to_text():
    data = request.get_data()
    print('DATA:', data)
    transcript = ''
    if request.method == "POST":
        print("FORM DATA RECIVIVED")

        if data:
            recognizer = sr.Recognizer()
            audioFile = sr.AudioFile(data)
            with audioFile as source:
                newData = recognizer.record(source)
            transcript = recognizer.recognize_google(newData, key=None)
            print('Transcript: ', transcript)

    return json.dumps({"text": "Audio successfully trancripted",
                       "the words": transcript}), 200

react-native: app.js

export default function App() {
  const [recording, setRecording] = React.useState();
  const [text, setText] = React.useState("");

  async function startRecording() {
    try {
      await Audio.requestPermissionsAsync();
      await Audio.setAudioModeAsync({
        allowsRecordingIOS: true,
        playsInSilentModeIOS: true,
      });
      const { recording } = await Audio.Recording.createAsync({
        android: {
          extension: ".mp4",
          audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
          outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
        },
        ios: {
          extension: ".wav",
          sampleRate: 44100,
          numberOfChannels: 2,
          bitRate: 128000,
          audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH,
          outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_LINEARPCM,
        },
      });
      setRecording(recording);
    } catch (err) {
      console.error("Failed to start recording", err);
    }
  }

  async function stopRecording() {
    setRecording(undefined);
    await recording.stopAndUnloadAsync();
    const uri = recording.getURI();
    
   console.log('uri: ', uri)
       // uri:  
      file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Fflask-api- 
       f578cf76-6a22-4ac9-86ab-9d300382693d/Audio/recording-5c5100ce-e457-4af2-ab73-5f6f628c1a8b.mp4

    try {
      const response = await FileSystem.uploadAsync(
        FLASK_BACKEND,
        uri
      );
      const body = JSON.parse(response.body);
      setText(body.text);
    } catch (err) {
      console.error(err);
    }
  }

  return (
    <View style={styles.container}>
      <Button 
      title={recording ? "Stop Recording": "Start Recording"}
      onPress={recording ? stopRecording : startRecording}
      />
    </View>
  );
}

});

By far, I understand that the recognizer needs a file in order to convert it to text, but the URI that is sent to the back end is a file URL(correct me if I'm wrong)

What I want is to convert it to text when the user records his voice

How can I solve this error?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum