Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,8 @@ import numpy as np
|
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
-
import matplotlib.pyplot as plt
|
| 9 |
-
import seaborn as sns
|
| 10 |
|
| 11 |
# --- CONFIG ---
|
| 12 |
# Note: GEMINI_API_KEY is retrieved from environment variables/secrets.
|
|
@@ -44,13 +44,13 @@ SYSTEM_INSTRUCTION = (
|
|
| 44 |
"2. **Code:** If the question requires calculation, aggregation, or visualization, you MUST generate Python code to execute against the 'df' DataFrame. "
|
| 45 |
" - The DataFrame is already loaded as a variable named 'df'. Do NOT redefine it. "
|
| 46 |
" - Use Streamlit functions for simple outputs: `st.dataframe(...)`, `st.bar_chart()`, `st.line_chart()`. "
|
| 47 |
-
" - For **ALL** custom, complex plots, you MUST follow this strict Matplotlib sequence: **Start with `plt.figure()`, use `plt.` commands for plotting, and explicitly end with `st.pyplot(plt)`** to display the output. "
|
|
|
|
| 48 |
" - Ensure the code is self-contained and ready to execute."
|
| 49 |
)
|
| 50 |
|
| 51 |
# --- Helper Functions ---
|
| 52 |
|
| 53 |
-
# Function to chat with the Gemini API and enforce structured JSON output
|
| 54 |
def chat_with_gemini(prompt, context):
|
| 55 |
"""Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
|
| 56 |
|
|
@@ -76,10 +76,9 @@ def chat_with_gemini(prompt, context):
|
|
| 76 |
for attempt in range(max_retries):
|
| 77 |
try:
|
| 78 |
r = requests.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|
| 79 |
-
r.raise_for_status()
|
| 80 |
data = r.json()
|
| 81 |
|
| 82 |
-
# The JSON output is a string inside the 'text' part
|
| 83 |
json_str = data["candidates"][0]["content"]["parts"][0]["text"]
|
| 84 |
return json.loads(json_str)
|
| 85 |
|
|
@@ -161,7 +160,8 @@ if uploaded:
|
|
| 161 |
'st': st,
|
| 162 |
'pd': pd,
|
| 163 |
'np': np,
|
| 164 |
-
'plt': plt,
|
|
|
|
| 165 |
}
|
| 166 |
|
| 167 |
# Append a neutral statement to the code to prevent implicit Streamlit display of the last value
|
|
|
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import seaborn as sns
|
| 10 |
|
| 11 |
# --- CONFIG ---
|
| 12 |
# Note: GEMINI_API_KEY is retrieved from environment variables/secrets.
|
|
|
|
| 44 |
"2. **Code:** If the question requires calculation, aggregation, or visualization, you MUST generate Python code to execute against the 'df' DataFrame. "
|
| 45 |
" - The DataFrame is already loaded as a variable named 'df'. Do NOT redefine it. "
|
| 46 |
" - Use Streamlit functions for simple outputs: `st.dataframe(...)`, `st.bar_chart()`, `st.line_chart()`. "
|
| 47 |
+
" - For **ALL** custom, complex plots, you MUST follow this strict Matplotlib sequence: **Start with `plt.figure()`, use `plt.` or `sns.` commands for plotting, and explicitly end with `st.pyplot(plt)`** to display the output. "
|
| 48 |
+
" - **CRITICAL GUARDRAIL:** When generating code that uses logical conditions (e.g., in `if` statements or for complex filters) on Pandas Series or NumPy arrays, you **MUST** resolve ambiguity by using `.any()` or `.all()`. Do NOT compare a series directly to a single boolean value."
|
| 49 |
" - Ensure the code is self-contained and ready to execute."
|
| 50 |
)
|
| 51 |
|
| 52 |
# --- Helper Functions ---
|
| 53 |
|
|
|
|
| 54 |
def chat_with_gemini(prompt, context):
|
| 55 |
"""Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
|
| 56 |
|
|
|
|
| 76 |
for attempt in range(max_retries):
|
| 77 |
try:
|
| 78 |
r = requests.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|
| 79 |
+
r.raise_for_status()
|
| 80 |
data = r.json()
|
| 81 |
|
|
|
|
| 82 |
json_str = data["candidates"][0]["content"]["parts"][0]["text"]
|
| 83 |
return json.loads(json_str)
|
| 84 |
|
|
|
|
| 160 |
'st': st,
|
| 161 |
'pd': pd,
|
| 162 |
'np': np,
|
| 163 |
+
'plt': plt,
|
| 164 |
+
'sns': sns,
|
| 165 |
}
|
| 166 |
|
| 167 |
# Append a neutral statement to the code to prevent implicit Streamlit display of the last value
|