Spaces:
Running
Running
added csv-agent-default in next.js
Browse files- main.py +3 -16
- python_code_interpreter_service.py +40 -56
main.py
CHANGED
|
@@ -93,7 +93,7 @@ async def execute_code(
|
|
| 93 |
code_request: Dictionary containing 'code' key with the Python code to execute
|
| 94 |
|
| 95 |
Returns:
|
| 96 |
-
dict: Dictionary containing execution results with all plots, output, variables, any errors
|
| 97 |
"""
|
| 98 |
if not token_valid:
|
| 99 |
raise HTTPException(status_code=401, detail="Not authenticated")
|
|
@@ -118,9 +118,9 @@ async def execute_code(
|
|
| 118 |
response_content = {
|
| 119 |
"success": result['error'] is None,
|
| 120 |
"output": result['output'],
|
|
|
|
| 121 |
"plots": [],
|
| 122 |
-
"html_charts": result.get('html_charts', [])
|
| 123 |
-
"excel_files": []
|
| 124 |
}
|
| 125 |
|
| 126 |
# Add all plots if they exist
|
|
@@ -134,23 +134,10 @@ async def execute_code(
|
|
| 134 |
for index, plot_data in enumerate(result['plots'])
|
| 135 |
]
|
| 136 |
|
| 137 |
-
# Add Excel files if they exist
|
| 138 |
-
if result.get('excel_files'):
|
| 139 |
-
response_content["excel_files"] = [
|
| 140 |
-
{
|
| 141 |
-
"filename": file['filename'],
|
| 142 |
-
"content": file['content'],
|
| 143 |
-
"content_type": file['content_type']
|
| 144 |
-
}
|
| 145 |
-
for file in result['excel_files']
|
| 146 |
-
]
|
| 147 |
-
|
| 148 |
# Add error information if exists
|
| 149 |
if result['error']:
|
| 150 |
response_content["error"] = result['error']
|
| 151 |
|
| 152 |
-
logger.info(f"Response content: {response_content}")
|
| 153 |
-
|
| 154 |
return response_content
|
| 155 |
|
| 156 |
except Exception as e:
|
|
|
|
| 93 |
code_request: Dictionary containing 'code' key with the Python code to execute
|
| 94 |
|
| 95 |
Returns:
|
| 96 |
+
dict: Dictionary containing execution results with all plots, output, variables, and any errors
|
| 97 |
"""
|
| 98 |
if not token_valid:
|
| 99 |
raise HTTPException(status_code=401, detail="Not authenticated")
|
|
|
|
| 118 |
response_content = {
|
| 119 |
"success": result['error'] is None,
|
| 120 |
"output": result['output'],
|
| 121 |
+
# "variables": result['variables'],
|
| 122 |
"plots": [],
|
| 123 |
+
"html_charts": result.get('html_charts', [])
|
|
|
|
| 124 |
}
|
| 125 |
|
| 126 |
# Add all plots if they exist
|
|
|
|
| 134 |
for index, plot_data in enumerate(result['plots'])
|
| 135 |
]
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# Add error information if exists
|
| 138 |
if result['error']:
|
| 139 |
response_content["error"] = result['error']
|
| 140 |
|
|
|
|
|
|
|
| 141 |
return response_content
|
| 142 |
|
| 143 |
except Exception as e:
|
python_code_interpreter_service.py
CHANGED
|
@@ -6,7 +6,6 @@ import os
|
|
| 6 |
import base64
|
| 7 |
from pathlib import Path
|
| 8 |
import uuid
|
| 9 |
-
import time
|
| 10 |
import numpy as np
|
| 11 |
import pandas as pd
|
| 12 |
import matplotlib
|
|
@@ -26,8 +25,6 @@ import warnings
|
|
| 26 |
import plotly.express as px
|
| 27 |
import plotly.graph_objects as go
|
| 28 |
from plotly.io import to_html
|
| 29 |
-
import openpyxl
|
| 30 |
-
|
| 31 |
|
| 32 |
|
| 33 |
def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
@@ -54,7 +51,6 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 54 |
plot_base64 = []
|
| 55 |
variables = {}
|
| 56 |
html_charts = []
|
| 57 |
-
excel_files = []
|
| 58 |
|
| 59 |
# Monkey patch plt.show() to save figures
|
| 60 |
original_show = plt.show
|
|
@@ -62,6 +58,7 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 62 |
def custom_show():
|
| 63 |
for i, fig in enumerate(plt.get_fignums()):
|
| 64 |
figure = plt.figure(fig)
|
|
|
|
| 65 |
buf = io.BytesIO()
|
| 66 |
figure.savefig(buf, format='png', bbox_inches='tight')
|
| 67 |
buf.seek(0)
|
|
@@ -72,51 +69,27 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 72 |
original_plotly_show = go.Figure.show
|
| 73 |
|
| 74 |
def custom_plotly_show(fig, *args, **kwargs):
|
|
|
|
| 75 |
chart_id = str(uuid.uuid4())
|
| 76 |
filename = f"chart_{chart_id}.html"
|
| 77 |
filepath = charts_dir / filename
|
|
|
|
|
|
|
| 78 |
html = to_html(fig, include_plotlyjs='cdn')
|
| 79 |
with open(filepath, 'w', encoding='utf-8') as f:
|
| 80 |
f.write(html)
|
| 81 |
-
html_charts.append(filename)
|
| 82 |
-
fig._grid_ref = None
|
| 83 |
-
return None
|
| 84 |
-
|
| 85 |
-
# Monkey patch pd.ExcelWriter to capture Excel files
|
| 86 |
-
original_ExcelWriter = pd.ExcelWriter
|
| 87 |
-
|
| 88 |
-
def custom_ExcelWriter(*args, **kwargs):
|
| 89 |
-
# Force openpyxl engine if no engine specified
|
| 90 |
-
if 'engine' not in kwargs:
|
| 91 |
-
kwargs['engine'] = 'openpyxl'
|
| 92 |
-
# Create in-memory file
|
| 93 |
-
excel_buffer = io.BytesIO()
|
| 94 |
-
kwargs['path'] = excel_buffer
|
| 95 |
-
writer = original_ExcelWriter(*args, **kwargs)
|
| 96 |
|
| 97 |
-
# Add
|
| 98 |
-
|
| 99 |
-
writer.close()
|
| 100 |
-
excel_buffer.seek(0)
|
| 101 |
-
excel_content = base64.b64encode(excel_buffer.read()).decode('utf-8')
|
| 102 |
-
filename = args[0] if len(args) > 0 else kwargs.get('path', 'output.xlsx')
|
| 103 |
-
if isinstance(filename, Path):
|
| 104 |
-
filename = filename.name
|
| 105 |
-
excel_files.append({
|
| 106 |
-
'filename': filename,
|
| 107 |
-
'content': excel_content,
|
| 108 |
-
'content_type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
| 109 |
-
})
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
try:
|
| 115 |
-
#
|
| 116 |
-
pd.ExcelWriter = custom_ExcelWriter
|
| 117 |
-
|
| 118 |
-
# Create execution context
|
| 119 |
exec_globals = {
|
|
|
|
| 120 |
'np': np,
|
| 121 |
'pd': pd,
|
| 122 |
'plt': plt,
|
|
@@ -125,25 +98,33 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 125 |
'stats': stats,
|
| 126 |
'sklearn': sklearn,
|
| 127 |
'tabulate': tabulate,
|
|
|
|
|
|
|
| 128 |
'px': px,
|
| 129 |
'go': go,
|
|
|
|
|
|
|
| 130 |
'datetime': datetime,
|
| 131 |
'parser': parser,
|
| 132 |
'pytz': pytz,
|
|
|
|
|
|
|
| 133 |
'os': os,
|
| 134 |
'sys': sys,
|
| 135 |
'warnings': warnings,
|
| 136 |
'json': json,
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
'openpyxl': openpyxl,
|
| 140 |
'DATA_DIR': data_dir,
|
| 141 |
'CHARTS_DIR': charts_dir,
|
|
|
|
|
|
|
| 142 |
'df': df,
|
|
|
|
| 143 |
'__builtins__': __builtins__,
|
| 144 |
}
|
| 145 |
|
| 146 |
-
# Add sklearn components
|
| 147 |
from sklearn import (
|
| 148 |
datasets, preprocessing, model_selection,
|
| 149 |
linear_model, ensemble, metrics, svm,
|
|
@@ -162,15 +143,18 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 162 |
'feature_selection': feature_selection,
|
| 163 |
})
|
| 164 |
|
| 165 |
-
# Replace show
|
| 166 |
plt.show = custom_show
|
|
|
|
|
|
|
| 167 |
go.Figure.show = custom_plotly_show
|
| 168 |
|
| 169 |
-
# Execute code
|
| 170 |
with contextlib.redirect_stdout(stdout):
|
|
|
|
| 171 |
exec(code, exec_globals)
|
| 172 |
|
| 173 |
-
# Capture variables
|
| 174 |
for name, value in exec_globals.items():
|
| 175 |
if not name.startswith('_') and name not in [
|
| 176 |
'np', 'pd', 'plt', 'sns', 'sm', 'stats', 'sklearn',
|
|
@@ -178,8 +162,7 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 178 |
'os', 'sys', 'warnings', 'json', 'DATA_DIR', 'CHARTS_DIR',
|
| 179 |
'datasets', 'preprocessing', 'model_selection', 'linear_model',
|
| 180 |
'ensemble', 'metrics', 'svm', 'decomposition', 'cluster',
|
| 181 |
-
'feature_selection', 'df'
|
| 182 |
-
'time', 'openpyxl'
|
| 183 |
]:
|
| 184 |
variables[name] = value
|
| 185 |
|
|
@@ -191,12 +174,12 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 191 |
"traceback": traceback.format_exc()
|
| 192 |
}
|
| 193 |
finally:
|
| 194 |
-
# Restore original
|
| 195 |
plt.show = original_show
|
|
|
|
| 196 |
go.Figure.show = original_plotly_show
|
| 197 |
-
pd.ExcelWriter = original_ExcelWriter
|
| 198 |
|
| 199 |
-
# Convert
|
| 200 |
def convert_objects(obj):
|
| 201 |
if isinstance(obj, (np.ndarray, np.generic)):
|
| 202 |
return obj.tolist() if obj.size > 1 else obj.item()
|
|
@@ -227,15 +210,16 @@ def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
| 227 |
return f"<function {obj.__name__}>"
|
| 228 |
return obj
|
| 229 |
|
| 230 |
-
processed_vars = {
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
| 234 |
|
| 235 |
return {
|
| 236 |
'output': output,
|
| 237 |
'error': error,
|
| 238 |
'plots': plot_base64,
|
| 239 |
-
'html_charts': html_charts
|
| 240 |
-
'excel_files': excel_files,
|
| 241 |
}
|
|
|
|
| 6 |
import base64
|
| 7 |
from pathlib import Path
|
| 8 |
import uuid
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
import matplotlib
|
|
|
|
| 25 |
import plotly.express as px
|
| 26 |
import plotly.graph_objects as go
|
| 27 |
from plotly.io import to_html
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def execute_python_code(code: str, df: pd.DataFrame = None) -> Dict[str, Any]:
|
|
|
|
| 51 |
plot_base64 = []
|
| 52 |
variables = {}
|
| 53 |
html_charts = []
|
|
|
|
| 54 |
|
| 55 |
# Monkey patch plt.show() to save figures
|
| 56 |
original_show = plt.show
|
|
|
|
| 58 |
def custom_show():
|
| 59 |
for i, fig in enumerate(plt.get_fignums()):
|
| 60 |
figure = plt.figure(fig)
|
| 61 |
+
# Save plot to bytes buffer instead of file
|
| 62 |
buf = io.BytesIO()
|
| 63 |
figure.savefig(buf, format='png', bbox_inches='tight')
|
| 64 |
buf.seek(0)
|
|
|
|
| 69 |
original_plotly_show = go.Figure.show
|
| 70 |
|
| 71 |
def custom_plotly_show(fig, *args, **kwargs):
|
| 72 |
+
# Generate unique filename
|
| 73 |
chart_id = str(uuid.uuid4())
|
| 74 |
filename = f"chart_{chart_id}.html"
|
| 75 |
filepath = charts_dir / filename
|
| 76 |
+
|
| 77 |
+
# Save as HTML
|
| 78 |
html = to_html(fig, include_plotlyjs='cdn')
|
| 79 |
with open(filepath, 'w', encoding='utf-8') as f:
|
| 80 |
f.write(html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Add to html_charts list
|
| 83 |
+
html_charts.append(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# Close the figure to free memory
|
| 86 |
+
fig._grid_ref = None # Help with memory cleanup
|
| 87 |
+
return None
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
# Create a comprehensive execution context with all common data science libraries
|
|
|
|
|
|
|
|
|
|
| 91 |
exec_globals = {
|
| 92 |
+
# Core libraries
|
| 93 |
'np': np,
|
| 94 |
'pd': pd,
|
| 95 |
'plt': plt,
|
|
|
|
| 98 |
'stats': stats,
|
| 99 |
'sklearn': sklearn,
|
| 100 |
'tabulate': tabulate,
|
| 101 |
+
|
| 102 |
+
# Plotly libraries
|
| 103 |
'px': px,
|
| 104 |
'go': go,
|
| 105 |
+
|
| 106 |
+
# Date/time libraries
|
| 107 |
'datetime': datetime,
|
| 108 |
'parser': parser,
|
| 109 |
'pytz': pytz,
|
| 110 |
+
|
| 111 |
+
# Utility
|
| 112 |
'os': os,
|
| 113 |
'sys': sys,
|
| 114 |
'warnings': warnings,
|
| 115 |
'json': json,
|
| 116 |
+
|
| 117 |
+
# File paths
|
|
|
|
| 118 |
'DATA_DIR': data_dir,
|
| 119 |
'CHARTS_DIR': charts_dir,
|
| 120 |
+
|
| 121 |
+
# Provided DataFrame
|
| 122 |
'df': df,
|
| 123 |
+
|
| 124 |
'__builtins__': __builtins__,
|
| 125 |
}
|
| 126 |
|
| 127 |
+
# Add common sklearn components
|
| 128 |
from sklearn import (
|
| 129 |
datasets, preprocessing, model_selection,
|
| 130 |
linear_model, ensemble, metrics, svm,
|
|
|
|
| 143 |
'feature_selection': feature_selection,
|
| 144 |
})
|
| 145 |
|
| 146 |
+
# Replace plt.show with custom implementation
|
| 147 |
plt.show = custom_show
|
| 148 |
+
|
| 149 |
+
# Replace plotly figure's show method
|
| 150 |
go.Figure.show = custom_plotly_show
|
| 151 |
|
| 152 |
+
# Execute code and capture output
|
| 153 |
with contextlib.redirect_stdout(stdout):
|
| 154 |
+
# First execute to get variables
|
| 155 |
exec(code, exec_globals)
|
| 156 |
|
| 157 |
+
# Capture all variables that were created
|
| 158 |
for name, value in exec_globals.items():
|
| 159 |
if not name.startswith('_') and name not in [
|
| 160 |
'np', 'pd', 'plt', 'sns', 'sm', 'stats', 'sklearn',
|
|
|
|
| 162 |
'os', 'sys', 'warnings', 'json', 'DATA_DIR', 'CHARTS_DIR',
|
| 163 |
'datasets', 'preprocessing', 'model_selection', 'linear_model',
|
| 164 |
'ensemble', 'metrics', 'svm', 'decomposition', 'cluster',
|
| 165 |
+
'feature_selection', 'df' # Exclude our parameter from variables
|
|
|
|
| 166 |
]:
|
| 167 |
variables[name] = value
|
| 168 |
|
|
|
|
| 174 |
"traceback": traceback.format_exc()
|
| 175 |
}
|
| 176 |
finally:
|
| 177 |
+
# Restore original plt.show
|
| 178 |
plt.show = original_show
|
| 179 |
+
# Restore original plotly show
|
| 180 |
go.Figure.show = original_plotly_show
|
|
|
|
| 181 |
|
| 182 |
+
# Convert various objects to serializable formats
|
| 183 |
def convert_objects(obj):
|
| 184 |
if isinstance(obj, (np.ndarray, np.generic)):
|
| 185 |
return obj.tolist() if obj.size > 1 else obj.item()
|
|
|
|
| 210 |
return f"<function {obj.__name__}>"
|
| 211 |
return obj
|
| 212 |
|
| 213 |
+
processed_vars = {}
|
| 214 |
+
for k, v in variables.items():
|
| 215 |
+
try:
|
| 216 |
+
processed_vars[k] = convert_objects(v)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
processed_vars[k] = f"<Unable to serialize: {str(e)}>"
|
| 219 |
|
| 220 |
return {
|
| 221 |
'output': output,
|
| 222 |
'error': error,
|
| 223 |
'plots': plot_base64,
|
| 224 |
+
'html_charts': html_charts
|
|
|
|
| 225 |
}
|