Server error with iOS

Hi,
I am using openai for audio transcription. and created an app at https://local-data.anvil.app/
It works fine on my laptop (Win10, Chrome browser), but I get an error when using my iPhone (iOS 17.3.1).

The full error text is “FileNotFoundError: [Errno 2] No such file or directory: ‘temp.wav’”. It seems strange to me as the code runs on the server side, right?

A clone link for the app is here:

Can anyone comment on this behaviour?

I can’t answer “why it works” on your PC, but it does throw the same error when I open the published app on my Chrome / Windows 10 machine.

Files in the server module usually need to be written to the /tmp/ directory.

Try using the anvil built in types for media objects and temporary files like this:

@anvil.server.callable
def audio(url):  
  response = requests.get(url)
 
  # Überprüfe, ob der Download erfolgreich war (Statuscode 200)
    if response.status_code == 200:
        # Speichere die heruntergeladene Datei lokal
        # hoist the openai code into a readbinary block
        # using a media object
        media_object = anvil.BlobMedia('audio/wav', response.content)
        with anvil.media.TempFile(media_object) as file_name:
            with open(file_name, "rb") as audio_file:
                transcript = client.audio.transcriptions.create(
                                            model="whisper-1",
                                            file=audio_file,
                                            response_format="text"
                                                )  
        return transcript
        
    else:
        print("Fehler beim Herunterladen der Datei. Statuscode:", 
                                                            response.status_code
                                                               )
    

This will also stop collisions from multiple users at the same time writing to the same “temp.wav”.

Thanks for the quick response. I tried your code (and added import anvil.media), but then I got a

BadRequestError: Error code: 400 - {‘error’: {‘message’: “Unrecognized file format. Supported formats: [‘flac’, ‘m4a’, ‘mp3’, ‘mp4’, ‘mpeg’, ‘mpga’, ‘oga’, ‘ogg’, ‘wav’, ‘webm’]”, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

Try

media_object = anvil.BlobMedia('audio/wav', response.content, "temp.wav")

Are you using requests because you expect users to submit urls in the future? If not, it would not be necessary to take a media object from a data table, from the client, get the url, then pass it back to the server module and download it with the requests module.

See:

Hi Ian and thanks for following up.
I still get a BadRequestError: Error code: 400

Regarding your question, I would like to use multiple wav files in the table and select them by name. The detour with writing “temp.wav” worked as OpenAI API requires a file. Before I tried to use Google Drive, but Anvil found a bug (so far not solved).

Do you know of an example or tutorial for Files in Server Modules? And where is the file in the original code stored?

I did some more troubleshooting. When running the app in the Anvil environment, everything works fine. Publishing the app and opening with a browser, this leads to response.status_code == 400, which the server cannot or will not process the request due to something that is perceived to be a client error.

What is the difference between “run” and “publish” then?