Rajan Sharma commited on
Commit
19052ba
·
verified ·
1 Parent(s): c098a9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,8 +1,9 @@
1
  # app.py
2
  from __future__ import annotations
3
  import os
 
4
  import traceback
5
- import regex as re2
6
  from typing import List, Dict, Any
7
 
8
  import gradio as gr
@@ -11,8 +12,6 @@ from datetime import datetime
11
 
12
  # --- BACKEND IMPORTS ---
13
  from langchain_cohere import ChatCohere
14
- # --- THE FIXED IMPORT IS HERE ---
15
- from langchain_experimental.utilities.python import PythonREPL
16
 
17
  # --- LOCAL MODULE IMPORTS ---
18
  from settings import (
@@ -41,9 +40,8 @@ def _create_python_script(user_scenario: str, schema_context: str) -> str:
41
  """Uses an LLM to act as an "AI Coder", writing a complete Python script."""
42
  prompt_for_coder = f"""
43
  You are an expert Python data scientist. Your sole job is to write a single, complete, and executable Python script to answer the user's request.
44
- You have access to a list of pandas dataframes loaded into a variable named `dfs`. The first dataframe is `dfs[0]`, the second is `dfs[1]`, and so on.
45
 
46
- CRITICAL CONTEXT: Before writing any code, you MUST first understand the data you have been given. Here is the schema for each dataframe:
47
  --- DATA SCHEMA ---
48
  {schema_context}
49
  --- END SCHEMA ---
@@ -53,16 +51,16 @@ CRITICAL RULE: You MUST use the exact column names provided in the DATA SCHEMA.
53
  Based on the user's scenario below, write a single Python script that performs the entire analysis.
54
 
55
  RULES FOR YOUR SCRIPT:
56
- 1. **Use the DataFrames:** Your script MUST use the `dfs` list to access the data.
57
- 2. **Print Your Findings:** Use the `print()` function at each step of your analysis to output the results as a formatted report.
58
  3. **No Placeholders:** Do not use placeholder data.
59
- 4. **Self-Contained:** The script must be entirely self-contained.
60
 
61
  --- USER'S SCENARIO ---
62
  {user_scenario}
63
 
64
  --- PYTHON SCRIPT ---
65
- Now, write the complete Python script to be executed. The script should start with `import pandas as pd` and contain all the logic.
66
  ```python
67
  """
68
  generated_text = cohere_chat(prompt_for_coder)
@@ -81,7 +79,7 @@ def ping_cohere() -> str:
81
  cli = _co_client()
82
  if not cli: return "Cohere client not initialized."
83
  vecs = cohere_embed(["hello", "world"])
84
- return f"Cohere OK ✅ (model={COHERE_MODEL_PRIMARY}, timeout={COHERE_TIMEOUT_S}s)" if vecs else "Cohere reachable."
85
  except Exception as e:
86
  return f"Cohere ping failed: {e}"
87
 
@@ -112,15 +110,24 @@ def handle(user_msg: str, files: list) -> str:
112
  schema_context = "\n".join(schema_parts)
113
  analysis_script = _create_python_script(safe_in, schema_context)
114
 
115
- # Initialize the Python Executor
116
- python_repl = PythonREPL()
117
- # Pass the dataframes into the execution environment
118
- local_vars = {"dfs": dataframes}
 
 
 
 
119
 
120
  try:
121
- # Execute the AI-generated script
122
- res = python_repl.run(command=analysis_script, locals=local_vars)
123
- return _sanitize_text(res)
 
 
 
 
 
124
  except Exception as e:
125
  # If execution fails, return the error and the script for debugging
126
  return f"An error occurred executing the script: {e}\n\nGenerated Script:\n```python\n{analysis_script}\n```"
@@ -197,7 +204,7 @@ with gr.Blocks(theme="soft", css="style.css") as demo:
197
 
198
  def view_history(selection, history_state_list):
199
  if not selection or not history_state_list: return ""
200
- selected_id = selection.split(" - ")[0]
201
  selected_assessment = next((item for item in history_state_list if item["id"] == selected_id), None)
202
  if selected_assessment:
203
  file_list_md = "\n- ".join(selected_assessment['files'])
 
1
  # app.py
2
  from __future__ import annotations
3
  import os
4
+ import io
5
  import traceback
6
+ from contextlib import redirect_stdout
7
  from typing import List, Dict, Any
8
 
9
  import gradio as gr
 
12
 
13
  # --- BACKEND IMPORTS ---
14
  from langchain_cohere import ChatCohere
 
 
15
 
16
  # --- LOCAL MODULE IMPORTS ---
17
  from settings import (
 
40
  """Uses an LLM to act as an "AI Coder", writing a complete Python script."""
41
  prompt_for_coder = f"""
42
  You are an expert Python data scientist. Your sole job is to write a single, complete, and executable Python script to answer the user's request.
43
+ You have access to a list of pandas dataframes loaded into a variable named `dfs`.
44
 
 
45
  --- DATA SCHEMA ---
46
  {schema_context}
47
  --- END SCHEMA ---
 
51
  Based on the user's scenario below, write a single Python script that performs the entire analysis.
52
 
53
  RULES FOR YOUR SCRIPT:
54
+ 1. **Use the DataFrames:** Your script MUST use the `dfs` list and the exact column names from the schema.
55
+ 2. **Print Your Findings:** Use the `print()` function at each step to output the results as a formatted report.
56
  3. **No Placeholders:** Do not use placeholder data.
57
+ 4. **Self-Contained:** The script must be entirely self-contained, starting with `import pandas as pd`.
58
 
59
  --- USER'S SCENARIO ---
60
  {user_scenario}
61
 
62
  --- PYTHON SCRIPT ---
63
+ Now, write the complete Python script to be executed.
64
  ```python
65
  """
66
  generated_text = cohere_chat(prompt_for_coder)
 
79
  cli = _co_client()
80
  if not cli: return "Cohere client not initialized."
81
  vecs = cohere_embed(["hello", "world"])
82
+ return f"Cohere OK ✅ (model={COHERE_MODEL_PRIMARY})" if vecs else "Cohere reachable."
83
  except Exception as e:
84
  return f"Cohere ping failed: {e}"
85
 
 
110
  schema_context = "\n".join(schema_parts)
111
  analysis_script = _create_python_script(safe_in, schema_context)
112
 
113
+ # --- THE FINAL, ROBUST FIX IS HERE: Using standard Python 'exec' ---
114
+ # Create a safe environment for the script to run in
115
+ execution_namespace = {
116
+ "dfs": dataframes,
117
+ "pd": pd
118
+ }
119
+ # Create a string buffer to capture the script's `print` statements
120
+ output_buffer = io.StringIO()
121
 
122
  try:
123
+ # Use a context manager to redirect stdout to our buffer
124
+ with redirect_stdout(output_buffer):
125
+ # Execute the AI-generated script in the safe namespace
126
+ exec(analysis_script, execution_namespace)
127
+
128
+ # Get the captured output
129
+ result = output_buffer.getvalue()
130
+ return _sanitize_text(result or "(The analysis script ran but produced no output.)")
131
  except Exception as e:
132
  # If execution fails, return the error and the script for debugging
133
  return f"An error occurred executing the script: {e}\n\nGenerated Script:\n```python\n{analysis_script}\n```"
 
204
 
205
  def view_history(selection, history_state_list):
206
  if not selection or not history_state_list: return ""
207
+ selected_id = selection.split(" - ")
208
  selected_assessment = next((item for item in history_state_list if item["id"] == selected_id), None)
209
  if selected_assessment:
210
  file_list_md = "\n- ".join(selected_assessment['files'])