Get Axios Error 403 when using OpenAI API in Streamlit app

Hi, I am trying to make a streamlit app that will take input from users in the form of documents which will then use openai api to chat with. After submitting a document I get the error " AxiosError: Request failed with status code 403 ". I have good amount of credit in my account and no rate limit.

import streamlit as st
import openai
import textract
import os

# Authenticate with OpenAI API
openai.api_key = '■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■GevsBmE0'

def chat_with_openai(document):
    try:
        response = openai.Completion.create(
          engine="davinci", 
          prompt=document,
          max_tokens=150
        )
        return response.choices[0].text.strip()
    except openai.error.OpenAIError as e:
        print(e)
        return "Error occurred while processing the request."

def extract_text_from_file(uploaded_file):
    # Save the uploaded file temporarily
    with open("tempfile", "wb") as f:
        f.write(uploaded_file.read())
    
    # Use textract to extract text
    text = textract.process("tempfile").decode('utf-8')
    
    # Clean up and remove the temporary file
    os.remove("tempfile")
    
    return text

st.title('Chat with OpenAI using Document Submission')

uploaded_file = st.file_uploader("Upload your document:", type=['txt', 'pdf', 'docx', 'ppt'])

if uploaded_file:
    document_text = extract_text_from_file(uploaded_file)
    st.write("Document content:")
    st.write(document_text)
    
    if st.button('Chat with OpenAI'):
        with st.spinner('Generating response...'):
            response = chat_with_openai(document_text)
            st.write("OpenAI's Response:")
            st.write(response)

Hi,

Error 403 is Forbidden, that does not seem like a typical OpenAI error, so long as you have a valid API key. Have you contacted streamlit about this?

1 Like

No I havent, So this isnt a openai api problem?

On the face of it no, if your API key works at all then this is a streamlit issue.

Ok Thank You , I try in the streamlit forums

If you get a solution over there, a link back to it from here would be helpful to others that might have the same issue.

1 Like