Spaces:
Running
Running
deploy: auto-deploy 17:59:57
Browse files
app/services/csv_analysis_service.py
CHANGED
|
@@ -105,6 +105,7 @@ with open(r"{blocks_path}", "r") as _f:
|
|
| 105 |
|
| 106 |
_results = {{"analyze": [], "visualization": []}}
|
| 107 |
_ns = {{"df": df, "pd": pd, "np": np, "plt": plt, "sns": sns}}
|
|
|
|
| 108 |
|
| 109 |
for _b in _data.get("analyze", []):
|
| 110 |
_code = (_b.get("python_code") or "").strip()
|
|
@@ -113,9 +114,25 @@ for _b in _data.get("analyze", []):
|
|
| 113 |
continue
|
| 114 |
_old = sys.stdout
|
| 115 |
sys.stdout = io.StringIO()
|
|
|
|
| 116 |
try:
|
| 117 |
exec(_code, _ns)
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
except Exception:
|
| 120 |
_results["analyze"].append({{"success": False, "output": sys.stdout.getvalue(), "error": traceback.format_exc()}})
|
| 121 |
finally:
|
|
|
|
| 105 |
|
| 106 |
_results = {{"analyze": [], "visualization": []}}
|
| 107 |
_ns = {{"df": df, "pd": pd, "np": np, "plt": plt, "sns": sns}}
|
| 108 |
+
_EXCLUDED = {{"pd", "np", "plt", "sns", "df", "json", "sys", "io", "base64", "traceback", "matplotlib", "seaborn"}}
|
| 109 |
|
| 110 |
for _b in _data.get("analyze", []):
|
| 111 |
_code = (_b.get("python_code") or "").strip()
|
|
|
|
| 114 |
continue
|
| 115 |
_old = sys.stdout
|
| 116 |
sys.stdout = io.StringIO()
|
| 117 |
+
_pre = {{k for k in _ns if not k.startswith("_")}} - _EXCLUDED
|
| 118 |
try:
|
| 119 |
exec(_code, _ns)
|
| 120 |
+
_output = sys.stdout.getvalue()
|
| 121 |
+
if not _output:
|
| 122 |
+
_post = {{k for k in _ns if not k.startswith("_")}} - _EXCLUDED
|
| 123 |
+
_new = sorted(_post - _pre)
|
| 124 |
+
if "final_result" in _ns:
|
| 125 |
+
_output += f"final_result = {{repr(_ns['final_result'])}}\\n"
|
| 126 |
+
for _k in _new:
|
| 127 |
+
if _k == "final_result":
|
| 128 |
+
continue
|
| 129 |
+
_v = _ns[_k]
|
| 130 |
+
try:
|
| 131 |
+
_s = repr(_v)
|
| 132 |
+
except Exception:
|
| 133 |
+
_s = "<unrepresentable>"
|
| 134 |
+
_output += f"{{_k}} = {{_s}}\\n"
|
| 135 |
+
_results["analyze"].append({{"success": True, "output": _output, "error": None}})
|
| 136 |
except Exception:
|
| 137 |
_results["analyze"].append({{"success": False, "output": sys.stdout.getvalue(), "error": traceback.format_exc()}})
|
| 138 |
finally:
|
app/services/prompts/csv_system_prompt.py
CHANGED
|
@@ -10,7 +10,7 @@ _JSON_EXAMPLE = (
|
|
| 10 |
' "analyze": [\n'
|
| 11 |
' {\n'
|
| 12 |
' "description": "Short explanation of the math",\n'
|
| 13 |
-
' "python_code": "# Clean data first\\ndf[\'col\'] = ...\\n\\n# Perform analysis\\
|
| 14 |
' }\n'
|
| 15 |
' ],\n'
|
| 16 |
' "visualization": [\n'
|
|
@@ -84,12 +84,13 @@ VISUALIZATION STANDARDS:
|
|
| 84 |
VARIABLE ASSIGNMENT RULES:
|
| 85 |
1. Every operation must store its result in a variable.
|
| 86 |
2. Variable names should be descriptive and snake_case.
|
| 87 |
-
3. For
|
| 88 |
-
4. For
|
| 89 |
-
5. For
|
| 90 |
-
6. For
|
| 91 |
-
7. For
|
| 92 |
-
8. For
|
|
|
|
| 93 |
|
| 94 |
EXAMPLES:
|
| 95 |
|
|
|
|
| 10 |
' "analyze": [\n'
|
| 11 |
' {\n'
|
| 12 |
' "description": "Short explanation of the math",\n'
|
| 13 |
+
' "python_code": "# Clean data first\\ndf[\'col\'] = ...\\n\\n# Perform analysis\\nfinal_result = df.groupby..."\n'
|
| 14 |
' }\n'
|
| 15 |
' ],\n'
|
| 16 |
' "visualization": [\n'
|
|
|
|
| 84 |
VARIABLE ASSIGNMENT RULES:
|
| 85 |
1. Every operation must store its result in a variable.
|
| 86 |
2. Variable names should be descriptive and snake_case.
|
| 87 |
+
3. For the final/primary result of an analysis block, ALWAYS use the variable name `final_result`.
|
| 88 |
+
4. For DataFrame operations: result_df = df.operation()
|
| 89 |
+
5. For statistical results: summary_stats = df.describe(include='all')
|
| 90 |
+
6. For filtered data: filtered_data = df[df['column'] > value]
|
| 91 |
+
7. For grouped analysis: revenue_by_region = df.groupby('region')['revenue'].sum().reset_index()
|
| 92 |
+
8. For correlation matrices: correlation_matrix = df.corr(numeric_only=True)
|
| 93 |
+
9. For visualizations: fig, ax = plt.subplots(...)
|
| 94 |
|
| 95 |
EXAMPLES:
|
| 96 |
|