-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2b_executor.py
More file actions
137 lines (112 loc) · 5 KB
/
Copy pathe2b_executor.py
File metadata and controls
137 lines (112 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
E2B sandbox execution functions for the Autonomous ML Agent.
This module contains all functions responsible for executing code in E2B sandboxes.
"""
import os
import streamlit as st
from typing import Optional, Tuple, Dict, Any
from e2b_code_interpreter import Sandbox
import pandas as pd
import io
def execute_in_e2b(script: str, csv_bytes: bytes) -> Tuple[Optional[bytes], Dict[str, Any]]:
"""
Execute a Python script in an E2B sandbox with the provided CSV data.
Args:
script (str): The Python script to execute
csv_bytes (bytes): The CSV file data as bytes
Returns:
tuple: (cleaned_csv_bytes, exec_info) or (None, exec_info) on error
"""
api_key = os.getenv("E2B_API_KEY")
if not api_key:
st.error("E2B_API_KEY is not set")
return None, {}
# Create a dictionary to store execution results
exec_info = {}
try:
# DOCUMENTATION: https://docs.e2b.dev/getting-started/introduction
# Create E2B sandbox instance - E2B manages lifecycle automatically
sandbox = Sandbox.create(api_key=api_key)
# Upload the CSV file to the sandbox
sandbox.files.write("/tmp/input.csv", csv_bytes)
# Execute the script directly using run_code
result = sandbox.run_code(script)
# Store execution results
exec_info["exit_code"] = getattr(result, "exit_code", 0)
exec_info["stdout"] = getattr(result, "stdout", "")
exec_info["stderr"] = getattr(result, "stderr", "")
# Debug: Check what files exist in /tmp
try:
list_result = sandbox.run_code("import os; print('Files in /tmp:'); print(os.listdir('/tmp'))")
exec_info["debug_files"] = getattr(list_result, "stdout", "")
print("Debug - Files in /tmp:", exec_info["debug_files"])
except Exception as e:
exec_info["debug_files"] = f"Error listing files: {str(e)}"
# Try to download the cleaned CSV file
try:
# The script should save the cleaned data to "/tmp/cleaned.csv"
cleaned_bytes = sandbox.files.read("/tmp/cleaned.csv")
return cleaned_bytes, exec_info
except Exception as e:
st.error(f"Error downloading cleaned file: {str(e)}")
st.error(f"Debug info: {exec_info.get('debug_files', 'No debug info')}")
return None, exec_info
except Exception as e:
st.error(f"Error executing script in E2B: {str(e)}")
return None, exec_info
finally:
# Only kill after we've retrieved all results
try:
sandbox.kill()
except:
pass # Ignore errors if sandbox is already dead
def execute_model_training_in_e2b(script: str, cleaned_csv_bytes: bytes) -> Tuple[Optional[pd.DataFrame], Dict[str, Any]]:
"""
Execute a model training script in an E2B sandbox with the cleaned CSV data.
Args:
script (str): The Python script to execute for model training
cleaned_csv_bytes (bytes): The cleaned CSV file data as bytes
Returns:
tuple: (model_results_df, exec_info) or (None, exec_info) on error
"""
api_key = os.getenv("E2B_API_KEY")
if not api_key:
st.error("E2B_API_KEY is not set")
return None, {}
# Create a dictionary to store execution results
exec_info = {}
try:
# Create E2B sandbox instance with timeout
sandbox = Sandbox.create(api_key=api_key, timeout=600)
# Upload the cleaned CSV file to the sandbox
sandbox.files.write("/tmp/cleaned.csv", cleaned_csv_bytes)
# Execute the model training script
result = sandbox.run_code(script)
# Store execution results
exec_info["exit_code"] = getattr(result, "exit_code", 0)
exec_info["stdout"] = getattr(result, "stdout", "")
exec_info["stderr"] = getattr(result, "stderr", "")
# Try to download model results if script executed successfully
if exec_info["exit_code"] == 0:
try:
model_results_bytes = sandbox.files.read("/tmp/model_results.csv")
if isinstance(model_results_bytes, str):
model_results_df = pd.read_csv(io.StringIO(model_results_bytes))
else:
model_results_df = pd.read_csv(io.BytesIO(model_results_bytes))
return model_results_df, exec_info
except Exception as e:
st.error(f"Could not download model_results.csv: {str(e)}")
return None, exec_info
else:
st.error(f"Model training script failed with exit code {exec_info['exit_code']}")
return None, exec_info
except Exception as e:
st.error(f"Error executing model training in E2B: {str(e)}")
return None, exec_info
finally:
# Clean up sandbox
try:
sandbox.kill()
except:
pass