sanjaystarc commited on
Commit
18b02aa
·
verified ·
1 Parent(s): a614a39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -42,8 +42,8 @@ SYSTEM_INSTRUCTION = (
42
  "1. **Reasoning:** Explain your plan, the steps taken, and the insights derived from the data. Format this in Markdown. "
43
  "2. **Code:** If the question requires calculation, aggregation, or visualization, you MUST generate Python code to execute against the 'df' DataFrame. "
44
  " - The DataFrame is already loaded as a variable named 'df'. Do NOT redefine it. "
45
- " - Use Streamlit functions for output: `st.dataframe(...)` for results, `st.bar_chart()`, `st.line_chart()`, or `st.pyplot()` for plots. "
46
- " - If you need custom plotting, you can assume `import matplotlib.pyplot as plt` is available and use `plt.figure()`, `plt.bar()`, etc., followed by `st.pyplot(plt)` to display the plot. "
47
  " - Ensure the code is self-contained and ready to execute."
48
  )
49
 
@@ -53,7 +53,7 @@ SYSTEM_INSTRUCTION = (
53
  def chat_with_gemini(prompt, context):
54
  """Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
55
 
56
- # FIX: Correctly prepend 'models/' to the model name in the URL path
57
  url = f"{GEMINI_BASE}/models/{CHAT_MODEL}:generateContent?key={GEMINI_API_KEY}"
58
 
59
  # Construct the full prompt including the data context
@@ -125,7 +125,6 @@ if uploaded:
125
  df = st.session_state.df # Local variable for code execution context
126
 
127
  # Summarize dataset for context sent to the LLM
128
- # FIX: Using to_string() instead of to_markdown() to avoid the 'tabulate' dependency error.
129
  context = f"Dataset Columns: {', '.join(df.columns.astype(str))}\n\nFirst 5 rows of data:\n{df.head(5).to_string(index=False)}"
130
 
131
  st.markdown("---")
@@ -166,6 +165,9 @@ if uploaded:
166
  # Executing the code within the local scope
167
  exec(code, globals(), local_scope)
168
 
 
 
 
169
  st.success("Code execution complete. Results are displayed above.")
170
 
171
  except Exception as e:
 
42
  "1. **Reasoning:** Explain your plan, the steps taken, and the insights derived from the data. Format this in Markdown. "
43
  "2. **Code:** If the question requires calculation, aggregation, or visualization, you MUST generate Python code to execute against the 'df' DataFrame. "
44
  " - The DataFrame is already loaded as a variable named 'df'. Do NOT redefine it. "
45
+ " - Use Streamlit functions for simple outputs: `st.dataframe(...)`, `st.bar_chart()`, `st.line_chart()`. "
46
+ " - For custom, complex plots, you MUST use Matplotlib: start with `plt.figure()`, then use `plt.` commands (like `plt.hist()`), and finish with `st.pyplot(plt)` to display the plot. "
47
  " - Ensure the code is self-contained and ready to execute."
48
  )
49
 
 
53
  def chat_with_gemini(prompt, context):
54
  """Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
55
 
56
+ # Correctly prepend 'models/' to the model name in the URL path
57
  url = f"{GEMINI_BASE}/models/{CHAT_MODEL}:generateContent?key={GEMINI_API_KEY}"
58
 
59
  # Construct the full prompt including the data context
 
125
  df = st.session_state.df # Local variable for code execution context
126
 
127
  # Summarize dataset for context sent to the LLM
 
128
  context = f"Dataset Columns: {', '.join(df.columns.astype(str))}\n\nFirst 5 rows of data:\n{df.head(5).to_string(index=False)}"
129
 
130
  st.markdown("---")
 
165
  # Executing the code within the local scope
166
  exec(code, globals(), local_scope)
167
 
168
+ # FIX: Explicitly close all Matplotlib figures to prevent cross-run contamination
169
+ plt.close('all')
170
+
171
  st.success("Code execution complete. Results are displayed above.")
172
 
173
  except Exception as e: