Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,6 +46,10 @@ def read_docx(file):
|
|
46 |
|
47 |
@st.cache_resource(show_spinner=False)
|
48 |
def load_data(uploaded_files):
|
|
|
|
|
|
|
|
|
49 |
with st.spinner("Loading and indexing the documents β hang tight! This should take 1-2 minutes."):
|
50 |
docs = []
|
51 |
for uploaded_file in uploaded_files:
|
@@ -56,7 +60,9 @@ def load_data(uploaded_files):
|
|
56 |
text = read_docx(uploaded_file)
|
57 |
docs.append(Document(text=text))
|
58 |
|
|
|
59 |
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
|
|
|
60 |
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.")
|
61 |
|
62 |
index = VectorStoreIndex.from_documents(docs, settings=Settings.llm)
|
@@ -90,43 +96,44 @@ uploaded_files = st.file_uploader("Upload PDF or DOCX files", type=["pdf", "docx
|
|
90 |
|
91 |
if uploaded_files and st.session_state.openai_api_key:
|
92 |
index = load_data(uploaded_files)
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
st.
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
st.
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
with st.
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
if st.
|
113 |
-
st.session_state.
|
114 |
-
|
115 |
-
|
116 |
-
st.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
st.
|
126 |
-
|
127 |
-
|
128 |
-
st.
|
129 |
-
|
|
|
130 |
|
131 |
else:
|
132 |
st.sidebar.warning("Please enter your OpenAI API key and upload PDF or DOCX files to proceed.")
|
@@ -162,4 +169,4 @@ if st.session_state.show_conversations:
|
|
162 |
else:
|
163 |
st.sidebar.write("No previous conversations found.")
|
164 |
else:
|
165 |
-
st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
|
|
|
46 |
|
47 |
@st.cache_resource(show_spinner=False)
|
48 |
def load_data(uploaded_files):
|
49 |
+
if not st.session_state.openai_api_key:
|
50 |
+
st.error("No OpenAI API key provided. Please enter a valid API key.")
|
51 |
+
return None
|
52 |
+
|
53 |
with st.spinner("Loading and indexing the documents β hang tight! This should take 1-2 minutes."):
|
54 |
docs = []
|
55 |
for uploaded_file in uploaded_files:
|
|
|
60 |
text = read_docx(uploaded_file)
|
61 |
docs.append(Document(text=text))
|
62 |
|
63 |
+
# Set the OpenAI API key
|
64 |
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
|
65 |
+
api_key=st.session_state.openai_api_key,
|
66 |
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.")
|
67 |
|
68 |
index = VectorStoreIndex.from_documents(docs, settings=Settings.llm)
|
|
|
96 |
|
97 |
if uploaded_files and st.session_state.openai_api_key:
|
98 |
index = load_data(uploaded_files)
|
99 |
+
if index: # Ensure index is not None
|
100 |
+
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
|
101 |
+
|
102 |
+
# User input for questions
|
103 |
+
if prompt := st.chat_input("Your question"):
|
104 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
105 |
+
|
106 |
+
for message in st.session_state.messages:
|
107 |
+
with st.chat_message(message["role"]):
|
108 |
+
st.write(message["content"])
|
109 |
+
|
110 |
+
if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] != "assistant":
|
111 |
+
with st.chat_message("assistant"):
|
112 |
+
with st.spinner("Thinking..."):
|
113 |
+
response = chat_engine.chat(prompt)
|
114 |
+
st.write(response.response)
|
115 |
+
message = {"role": "assistant", "content": response.response}
|
116 |
+
st.session_state.messages.append(message)
|
117 |
+
|
118 |
+
if st.button("Save Conversation"):
|
119 |
+
if st.session_state.messages:
|
120 |
+
st.session_state.confirm_save = True
|
121 |
+
|
122 |
+
if st.session_state.get('confirm_save', False):
|
123 |
+
st.warning("Do you want to save the conversation?")
|
124 |
+
col1, col2 = st.columns(2)
|
125 |
+
with col1:
|
126 |
+
if st.button("Yes"):
|
127 |
+
save_conversation()
|
128 |
+
st.success("Conversation saved!")
|
129 |
+
st.session_state.confirm_save = False
|
130 |
+
with col2:
|
131 |
+
if st.button("No"):
|
132 |
+
st.session_state.confirm_save = False
|
133 |
+
|
134 |
+
if st.button("End Conversation"):
|
135 |
+
st.session_state.messages = []
|
136 |
+
st.success("Conversation ended. You can start a new one!")
|
137 |
|
138 |
else:
|
139 |
st.sidebar.warning("Please enter your OpenAI API key and upload PDF or DOCX files to proceed.")
|
|
|
169 |
else:
|
170 |
st.sidebar.write("No previous conversations found.")
|
171 |
else:
|
172 |
+
st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
|