Get Axios Error 403 when using openai API in Streamlit

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 the submitted documents. 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. I have asked on the Openai forum, and I was told that it was not a OpenAI API problem but a streamlit one.

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)

Hey @asaji1999,

Can you share a link to the deployed app? You mentioned you’re only seeing this error after uploading a file, right?

I am running it as a local host from VSCode, I am currently learning about AI, Machine Learning, so this was a small project by using ChatGPT to learn.

If this is running locally, it’s unlikely the 403 error is coming from Streamlit. What version of Streamlit are you running? Also, have you double-checked that the API key you’re using is valid?

Streamllit version is 1.25.0, and API key is valid

Hey @asaji1999, I ran your app locally with my own API key and I’m not seeing that error. What kind of file are you uploading?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.