Streamlit code avatar
2 months ago
2.6 kB
import os
import time
import openai
import streamlit as st
# Change with your Assistant ID and OpenAI API Key
# Create an OpenAI client with your API key
openai_client = openai.Client(api_key=os.getenv("OPENAI_API_KEY", OPENAI_API_KEY))
# Retrieve the assistant you want to use
assistant = openai_client.beta.assistants.retrieve(
# Display an image of a robot assistant
st.image("robot_image.png", width=150)
# Create the title and subheader for the Streamlit page
st.title("Customer Care Assistant")
st.subheader("Upload a PDF and ask me anything about it!")
# Create a file input for the user to upload a PDF
uploaded_file = st.file_uploader(
    "Upload a PDF", type="pdf", label_visibility="collapsed"
# Allow users to input their question
user_question = st.text_input("Enter your question about the PDF:")
# If the user has uploaded a file and entered a question, start the assistant process...
if uploaded_file is not None and user_question:
    # Create a status indicator to show the user the assistant is working
    with st.status("Processing your request...", expanded=False) as status_box:
        # Upload the file to OpenAI
        file = openai_client.files.create(
            file=uploaded_file, purpose="assistants"
        # Create a new thread with a message that has the uploaded file's ID and the user's question
        thread = openai_client.beta.threads.create(
                    "role": "user",
                    "content": user_question,
                    "file_ids": [],
        # Create a run with the new thread
        run = openai_client.beta.threads.runs.create(
        # Check periodically whether the run is done, and update the status
        while run.status != "completed":
            status_box.update(label=f"{run.status}...", state="running")
            run = openai_client.beta.threads.runs.retrieve(
        # Once the run is complete, update the status box and show the content
        status_box.update(label="Complete", state="complete", expanded=True)
        messages = openai_client.beta.threads.messages.list(
        # Delete the uploaded file from OpenAI
Leave a Comment