Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ import gradio as gr
|
|
| 10 |
import pandas as pd
|
| 11 |
from datetime import datetime
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# --- BACKEND IMPORTS ---
|
| 14 |
from langchain_cohere import ChatCohere
|
| 15 |
|
|
@@ -34,6 +37,7 @@ def load_markdown_text(filepath: str) -> str:
|
|
| 34 |
|
| 35 |
def _sanitize_text(s: str) -> str:
|
| 36 |
if not isinstance(s, str): return s
|
|
|
|
| 37 |
return re2.sub(r'[\p{C}--[\n\t]]+', '', s)
|
| 38 |
|
| 39 |
def _create_python_script(user_scenario: str, schema_context: str) -> str:
|
|
@@ -110,26 +114,16 @@ def handle(user_msg: str, files: list) -> str:
|
|
| 110 |
schema_context = "\n".join(schema_parts)
|
| 111 |
analysis_script = _create_python_script(safe_in, schema_context)
|
| 112 |
|
| 113 |
-
|
| 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```"
|
| 134 |
else:
|
| 135 |
prompt = f"{GENERAL_CONVERSATION_PROMPT}\n\nUser: {safe_in}\nAssistant:"
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
from datetime import datetime
|
| 12 |
|
| 13 |
+
# --- THE FINAL FIX IS HERE: Re-introducing the missing import ---
|
| 14 |
+
import regex as re2
|
| 15 |
+
|
| 16 |
# --- BACKEND IMPORTS ---
|
| 17 |
from langchain_cohere import ChatCohere
|
| 18 |
|
|
|
|
| 37 |
|
| 38 |
def _sanitize_text(s: str) -> str:
|
| 39 |
if not isinstance(s, str): return s
|
| 40 |
+
# This now works because 're2' is defined from the import above
|
| 41 |
return re2.sub(r'[\p{C}--[\n\t]]+', '', s)
|
| 42 |
|
| 43 |
def _create_python_script(user_scenario: str, schema_context: str) -> str:
|
|
|
|
| 114 |
schema_context = "\n".join(schema_parts)
|
| 115 |
analysis_script = _create_python_script(safe_in, schema_context)
|
| 116 |
|
| 117 |
+
execution_namespace = {"dfs": dataframes, "pd": pd}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
output_buffer = io.StringIO()
|
| 119 |
|
| 120 |
try:
|
|
|
|
| 121 |
with redirect_stdout(output_buffer):
|
|
|
|
| 122 |
exec(analysis_script, execution_namespace)
|
| 123 |
|
|
|
|
| 124 |
result = output_buffer.getvalue()
|
| 125 |
return _sanitize_text(result or "(The analysis script ran but produced no output.)")
|
| 126 |
except Exception as e:
|
|
|
|
| 127 |
return f"An error occurred executing the script: {e}\n\nGenerated Script:\n```python\n{analysis_script}\n```"
|
| 128 |
else:
|
| 129 |
prompt = f"{GENERAL_CONVERSATION_PROMPT}\n\nUser: {safe_in}\nAssistant:"
|