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):
        response = openai.Completion.create(
        return response.choices[0].text.strip()
    except openai.error.OpenAIError as 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:
    # Use textract to extract text
    text = textract.process("tempfile").decode('utf-8')
    # Clean up and remove the temporary file
    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:")
    if st.button('Chat with OpenAI'):
        with st.spinner('Generating response...'):
            response = chat_with_openai(document_text)
            st.write("OpenAI's Response:")


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

hey i just had this problem too, just with image generation, where it was unable to load previous generated images, and i tried to open the image url outside of the app, and i found this:



<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:158bb942-901e-007e-6faa-6dd9ed000000 Time:2024-03-03T20:34:44.8685181Z</Message>

<AuthenticationErrorDetail>Signature not valid in the specified time frame: Start [Sat, 02 Mar 2024 23:37:50 GMT] - Expiry [Sun, 03 Mar 2024 01:37:50 GMT] - Current [Sun, 03 Mar 2024 20:34:44 GMT]</AuthenticationErrorDetail>


though im not entirely sure it is related