Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -133,14 +133,52 @@ def get_vectorstore(text_chunks):
|
|
| 133 |
def handle_userinput(question, pdf_vectorstore, dfs):
|
| 134 |
if pdf_vectorstore and st.session_state.conversation:
|
| 135 |
response = st.session_state.conversation({"question": question})
|
| 136 |
-
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
answer = generateResponse(question, dfs)
|
| 141 |
-
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
else:
|
| 145 |
st.write("Please upload and process your documents/data first.")
|
| 146 |
|
|
@@ -172,7 +210,17 @@ def main():
|
|
| 172 |
# Chat display
|
| 173 |
for message in st.session_state.chat_history:
|
| 174 |
with st.chat_message(message["role"]):
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
if user_question:
|
| 178 |
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|
|
|
|
| 133 |
def handle_userinput(question, pdf_vectorstore, dfs):
|
| 134 |
if pdf_vectorstore and st.session_state.conversation:
|
| 135 |
response = st.session_state.conversation({"question": question})
|
| 136 |
+
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 137 |
+
|
| 138 |
+
assistant_response = response['answer']
|
| 139 |
+
|
| 140 |
+
# Check if the response contains image or dataframe information (adapt as needed)
|
| 141 |
+
|
| 142 |
+
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 143 |
+
content_type = assistant_response.get('type') # Assuming your StreamLitResponse sets this
|
| 144 |
+
content_value = assistant_response['value']
|
| 145 |
+
|
| 146 |
+
if content_type == "dataframe":
|
| 147 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"}) # Store a placeholder
|
| 148 |
+
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value}) # Store the DataFrame separately
|
| 149 |
+
elif content_type == "plot": # Or whatever you name the plot type
|
| 150 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"}) # Placeholder
|
| 151 |
+
st.session_state.chat_history.append({"role": "assistant", "plot": content_value}) # Store the image/plot data
|
| 152 |
+
else:
|
| 153 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 154 |
+
|
| 155 |
+
else:
|
| 156 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 157 |
+
|
| 158 |
+
st.rerun()
|
| 159 |
+
|
| 160 |
+
elif dfs: # PandasAI case
|
| 161 |
answer = generateResponse(question, dfs)
|
| 162 |
+
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 163 |
+
|
| 164 |
+
if isinstance(answer, dict) and 'value' in answer:
|
| 165 |
+
content_type = answer.get('type')
|
| 166 |
+
content_value = answer['value']
|
| 167 |
+
|
| 168 |
+
if content_type == "dataframe":
|
| 169 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 170 |
+
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 171 |
+
elif content_type == "plot":
|
| 172 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 173 |
+
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 174 |
+
else:
|
| 175 |
+
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 176 |
+
|
| 177 |
+
else:
|
| 178 |
+
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 179 |
+
|
| 180 |
+
st.rerun()
|
| 181 |
+
|
| 182 |
else:
|
| 183 |
st.write("Please upload and process your documents/data first.")
|
| 184 |
|
|
|
|
| 210 |
# Chat display
|
| 211 |
for message in st.session_state.chat_history:
|
| 212 |
with st.chat_message(message["role"]):
|
| 213 |
+
if "dataframe" in message:
|
| 214 |
+
st.dataframe(message["dataframe"])
|
| 215 |
+
elif "plot" in message:
|
| 216 |
+
if isinstance(message["plot"], Image.Image):
|
| 217 |
+
st.image(message["plot"])
|
| 218 |
+
elif isinstance(message["plot"], go.Figure):
|
| 219 |
+
st.plotly_chart(message["plot"])
|
| 220 |
+
else:
|
| 221 |
+
st.write("Unsupported plot format")
|
| 222 |
+
else:
|
| 223 |
+
st.write(message["content"])
|
| 224 |
|
| 225 |
if user_question:
|
| 226 |
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|