Skip to content

Weird interaction change with OpenAI Client and files #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
Lord-Haji opened this issue Dec 7, 2023 · 5 comments
Closed
1 task done

Weird interaction change with OpenAI Client and files #942

Lord-Haji opened this issue Dec 7, 2023 · 5 comments

Comments

@Lord-Haji
Copy link

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

I had a webapp running which was using the openai==0.27.7 I recently was working on revamping it and switched to the new openai client. I thought it'll be just plug and play with the new Interface like how GPT works. I tried changing my approach a few times but kept getting weird file format errors everytime. Not an issue on the file end since the same one works on the older api for the same.
Totally an issue on the new client

To Reproduce

` def transcribe(self):
# transcript_text = openai.Audio.transcribe("whisper-1", self.audio_file)["text"]
transcript_text = client.audio.transcriptions.create(
model = "whisper-1",
file = self.audio_file
)["text"]
self.transcript = transcript_text

    return transcript_text`

@app.post("/transcribe_audio") async def transcribe_audio(audio_file: UploadFile = File(...)): if audio_file.filename.endswith((".mp3", ".wav", "m4a", "mp4", "mpeg", "mpga", "oga", "ogg", "webm")): audio_query = AudioQuery(audio_file) transcript_text = audio_query.transcribe() return {"transcript": transcript_text} else: raise HTTPException(status_code=400, detail="Invalid file type. Please upload a file in a supported format")

Code snippets

@app.post("/transcribe_audio")
async def transcribe_audio(audio_file: UploadFile = File(...)):
    if audio_file.filename.endswith((".mp3", ".wav")):
        file_name = os.path.splitext(os.path.basename(audio_file.filename))[0]
        audio_query = AudioQuery(audio_file, file_name)
        
        transcript_text = audio_query.transcribe()
        return {"transcript": transcript_text}
    else:
        raise HTTPException(status_code=400, detail="Invalid file type. Please upload a .mp3 or .wav file")


    def transcribe(self):
        transcript_text = fetch_transcription(self.file_name)

        if transcript_text:
            self.transcript = transcript_text
        else:
            openai.api_key = API_KEY
            transcript_text = openai.Audio.transcribe("whisper-1", self.audio_file)["text"]
            cache_transcription(self.file_name, transcript_text)
            self.transcript = transcript_text

OS

Windows

Python version

Python v3.11.4

Library version

openai v1.0.1

@Lord-Haji Lord-Haji added the bug Something isn't working label Dec 7, 2023
@rattrayalex rattrayalex removed the bug Something isn't working label Dec 8, 2023
@rattrayalex
Copy link
Collaborator

rattrayalex commented Dec 8, 2023

Please see the migration guide for v1: #742

It looks like your code snippet is still written for the v0, which has a different API.

Note also the documentation on file uploads: https://github.com/openai/openai-python#file-uploads

@Lord-Haji
Copy link
Author

I'm quite confused wasnt this migration supposed to be plug and play?
I tried the openai migrate on a linux system on my old codebase
and still ended up with

RuntimeError: Expected entry at file to be bytes, an io.IOBase instance, PathLike or a tuple but received <class 'starlette.datastructures.UploadFile'> instead. See https://github.com/openai/openai-python/tree/main#file-uploads

@Lord-Haji
Copy link
Author

Makes me think did the old client support starlette.datastructures.UploadFile ? and new one just had a downgrade in this regard?

@rattrayalex
Copy link
Collaborator

cc @morgante, do you think we could auto-migrate this case?

@Lord-Haji can you share the code you have now?

@morgante
Copy link
Contributor

morgante commented Dec 8, 2023

@Lord-Haji Can you share a minimal reproduction / repo with a sample that worked before?

If so, I think we can set up an auto-migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants