Spaces:
Sleeping
Sleeping
# import streamlit as st | |
# from llama_index.core import ServiceContext, Document | |
# from llama_index.core import VectorStoreIndex, SimpleDirectoryReader | |
# from llama_index.llms.openai import OpenAI | |
# import openai | |
# import os | |
# import openai | |
# # Fetch the OpenAI API key from the environment variable | |
# openai.api_key = os.getenv("OPENAI_API_KEY") | |
# # Check if the key was correctly loaded (optional) | |
# if not openai.api_key: | |
# print("OpenAI API key not found in environment variables.") | |
# st.header("Chat with the Streamlit docs π¬ π") | |
# if "messages" not in st.session_state.keys(): # Initialize the chat message history | |
# st.session_state.messages = [ | |
# {"role": "assistant", "content": "Ask me a question about Python!"} | |
# ] | |
# @st.cache_resource(show_spinner=False) | |
# def load_data(): | |
# with st.spinner(text="Loading and indexing the Streamlit docs β hang tight! This should take 1-2 minutes."): | |
# reader = SimpleDirectoryReader(input_dir="./how-to-code-in-python.pdf", recursive=True) | |
# docs = reader.load_data() | |
# service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts β do not hallucinate features.")) | |
# index = VectorStoreIndex.from_documents(docs, service_context=service_context) | |
# return index | |
# index = load_data() | |
import streamlit as st | |
from llama_index.core import ServiceContext, Document | |
from llama_index.core import VectorStoreIndex | |
from llama_index.llms.openai import OpenAI | |
import openai | |
import PyPDF2 | |
openai_api_key = st.secrets["OPENAI_API_KEY"] | |
openai.api_key = openai_api_key | |
# Check if the key was correctly loaded (optional) | |
if not openai.api_key: | |
print("OpenAI API key not found in environment variables.") | |
st.header("Chat with the Streamlit docs ") | |
if "messages" not in st.session_state.keys(): # Initialize the chat message history | |
st.session_state.messages = [ | |
{"role": "assistant", "content": "Ask me a question about Python!"} | |
] | |
# Upload the PDF file | |
st.header("Upload the PDF file") | |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") | |
def load_data(): | |
if uploaded_file is not None: | |
with st.spinner(text="Loading and indexing the PDF file β hang tight! This should take 1-2 minutes."): | |
pdf_reader = PyPDF2.PdfFileReader(uploaded_file) | |
text = "" | |
for page in range(pdf_reader.numPages): | |
text += pdf_reader.getPage(page).extractText() | |
doc = Document(text) | |
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts β do not hallucinate features.")) | |
index = VectorStoreIndex.from_documents([doc], service_context=service_context) | |
return index | |
index = load_data() | |
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True) | |
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
for message in st.session_state.messages: # Display the prior chat messages | |
with st.chat_message(message["role"]): | |
st.write(message["content"]) | |
# If last message is not from assistant, generate a new response | |
if st.session_state.messages[-1]["role"] != "assistant": | |
with st.chat_message("assistant"): | |
with st.spinner("Thinking..."): | |
response = chat_engine.chat(prompt) | |
st.write(response.response) | |
message = {"role": "assistant", "content": response.response} | |
st.session_state.messages.append(message) # Add response to message history | |