Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -135,35 +135,38 @@ def handle_userinput(question, pdf_vectorstore, dfs):
|
|
| 135 |
if pdf_vectorstore and st.session_state.conversation:
|
| 136 |
response = st.session_state.conversation({"question": question})
|
| 137 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 138 |
-
assistant_response = response.get('answer')
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
results = generateResponse(question, dfs)
|
| 142 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 143 |
-
# PandasAI's results are already a list, no need to wrap
|
| 144 |
-
|
| 145 |
-
# Process each result from PandasAI (or Langchain) in a consistent way:
|
| 146 |
-
for assistant_response in results: # Changed to loop through results
|
| 147 |
-
# Assistant Response could be text, dataframe or plot
|
| 148 |
-
if assistant_response: # Check if assistant_response is not None or empty
|
| 149 |
-
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 150 |
-
content_type = assistant_response.get('type')
|
| 151 |
-
content_value = assistant_response.get('value')
|
| 152 |
-
|
| 153 |
-
if content_type == "dataframe":
|
| 154 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 155 |
-
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 156 |
-
elif content_type == "plot":
|
| 157 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 158 |
-
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 159 |
-
else: # Text or other
|
| 160 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
st.rerun()
|
| 166 |
-
return
|
| 167 |
|
| 168 |
else:
|
| 169 |
st.write("Please upload and process your documents/data first.")
|
|
|
|
| 135 |
if pdf_vectorstore and st.session_state.conversation:
|
| 136 |
response = st.session_state.conversation({"question": question})
|
| 137 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 138 |
+
assistant_response = response.get('answer')
|
| 139 |
|
| 140 |
+
if assistant_response: # Check if assistant_response is not None or empty
|
| 141 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response}) # Directly add string
|
| 142 |
+
|
| 143 |
+
st.rerun()
|
| 144 |
+
|
| 145 |
+
elif dfs: # PandasAI branch (handling DataFrames and plots)
|
| 146 |
results = generateResponse(question, dfs)
|
| 147 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
if results is not None: # Check if results is NOT None
|
| 150 |
+
for assistant_response in results:
|
| 151 |
+
if assistant_response: # Check if not None or empty
|
| 152 |
+
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 153 |
+
content_type = assistant_response.get('type')
|
| 154 |
+
content_value = assistant_response.get('value')
|
| 155 |
+
|
| 156 |
+
if content_type == "dataframe":
|
| 157 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 158 |
+
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 159 |
+
elif content_type == "plot":
|
| 160 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 161 |
+
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 162 |
+
else: # Text or other
|
| 163 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 164 |
+
|
| 165 |
+
else: # Text or other
|
| 166 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 167 |
|
| 168 |
st.rerun()
|
| 169 |
+
return # Exit early after PandasAI processing
|
| 170 |
|
| 171 |
else:
|
| 172 |
st.write("Please upload and process your documents/data first.")
|