Streamlit code

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.6 kB
13
Indexable
Never
import os
import time
 
import openai
import streamlit as st
 
 
 
# Change with your Assistant ID and OpenAI API Key
CHATBOT_ASSISTANT_ID = ""
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(
    CHATBOT_ASSISTANT_ID
)
 
# 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(
            messages=[
                {
                    "role": "user",
                    "content": user_question,
                    "file_ids": [file.id],
                }
            ]
        )
 
        # Create a run with the new thread
        run = openai_client.beta.threads.runs.create(
            thread_id=thread.id,
            assistant_id=assistant.id,
        )
 
        # Check periodically whether the run is done, and update the status
        while run.status != "completed":
            time.sleep(5)
            status_box.update(label=f"{run.status}...", state="running")
            run = openai_client.beta.threads.runs.retrieve(
                thread_id=thread.id, run_id=run.id
            )
 
        # 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(
            thread_id=thread.id
        )
        st.markdown(messages.data[0].content[0].text.value)
 
        # Delete the uploaded file from OpenAI
        openai_client.files.delete(file.id)
Leave a Comment