Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,10 +46,21 @@ if not GOOGLE_API_KEY:
|
|
| 46 |
|
| 47 |
|
| 48 |
def generateResponse(prompt, dfs):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
# Processing pdfs
|
|
@@ -92,28 +103,27 @@ def handle_userinput(question, pdf_vectorstore, dfs):
|
|
| 92 |
|
| 93 |
st.rerun()
|
| 94 |
|
| 95 |
-
elif dfs: # PandasAI
|
| 96 |
-
|
|
|
|
| 97 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 98 |
|
| 99 |
-
if
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
else: # Text or other
|
| 116 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 117 |
|
| 118 |
st.rerun()
|
| 119 |
return # Exit early after PandasAI processing
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def generateResponse(prompt, dfs):
|
| 49 |
+
llm = GoogleGemini(api_key=GOOGLE_API_KEY)
|
| 50 |
+
response_parser = StreamLitResponse({})
|
| 51 |
+
pandas_agent = SmartDatalake(dfs, config={"llm": llm, "response_parser": response_parser})
|
| 52 |
+
try:
|
| 53 |
+
pandas_agent.chat(prompt)
|
| 54 |
+
results = response_parser.get_results()
|
| 55 |
+
if not results: # If results is empty
|
| 56 |
+
return {"type": "text", "value": "No results found for your query."} # Return a default message as a dictionary
|
| 57 |
+
elif len(results) == 1: # If results contains only one object, return the object directly
|
| 58 |
+
return results[0]
|
| 59 |
+
else:
|
| 60 |
+
return results # Return the list of results
|
| 61 |
+
except Exception as e: # Catch any exceptions during PandasAI processing
|
| 62 |
+
st.error(f"Error in PandasAI: {e}") # Display the error in Streamlit
|
| 63 |
+
return {"type": "text", "value": f"An error occurred: {e}"}
|
| 64 |
|
| 65 |
|
| 66 |
# Processing pdfs
|
|
|
|
| 103 |
|
| 104 |
st.rerun()
|
| 105 |
|
| 106 |
+
elif dfs: # PandasAI
|
| 107 |
+
assistant_response = generateResponse(question, dfs) # Get the single response
|
| 108 |
+
|
| 109 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 110 |
|
| 111 |
+
if assistant_response: # Check if assistant_response is not None or empty
|
| 112 |
+
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 113 |
+
content_type = assistant_response.get('type')
|
| 114 |
+
content_value = assistant_response.get('value')
|
| 115 |
+
|
| 116 |
+
if content_type == "dataframe":
|
| 117 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 118 |
+
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 119 |
+
elif content_type == "plot":
|
| 120 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 121 |
+
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 122 |
+
else: # Text or other
|
| 123 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 124 |
+
|
| 125 |
+
else: # Text or other (including None if that's what it is)
|
| 126 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
|
|
|
|
|
|
| 127 |
|
| 128 |
st.rerun()
|
| 129 |
return # Exit early after PandasAI processing
|