Skip to content

Commit 6a9168e

Browse files
committed
Updates
1 parent b144539 commit 6a9168e

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

.github/workflows/publish-docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212
workflow_dispatch:
1313

1414
jobs:
15-
build-agixt:
15+
build-safeexecute:
1616
uses: josh-xt/AGiXT/.github/workflows/operation-docker-build-publish.yml@main
1717
with:
1818
registry-dockerhub-enable: ${{ github.event_name != 'pull_request' }}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
This module provides a safe way to execute Python code in a container. It is intended to be used with language models to enable them to execute code in a safe environment separate from the host machine (your computer or server).
66

7+
The container comes preloaded with the following packages:
8+
9+
```bash
10+
numpy matplotlib seaborn scikit-learn yfinance scipy statsmodels sympy bokeh plotly dash networkx pyvis pandas
11+
```
12+
713
## Installation
814

915
```bash

safeexecute/__init__.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,32 @@
55
import docker
66
from docker.errors import ImageNotFound
77

8+
IMAGE_NAME = "joshxt/safeexecute:latest"
9+
10+
11+
def install_docker_image():
12+
client = docker.from_env()
13+
try:
14+
client.images.get(IMAGE_NAME)
15+
logging.info(f"Image '{IMAGE_NAME}' found locally")
16+
except ImageNotFound:
17+
logging.info(f"Installing docker image '{IMAGE_NAME}' from Docker Hub")
18+
low_level_client = docker.APIClient()
19+
for line in low_level_client.pull(IMAGE_NAME, stream=True, decode=True):
20+
status = line.get("status")
21+
progress = line.get("progress")
22+
if status and progress:
23+
logging.info(f"{status}: {progress}")
24+
elif status:
25+
logging.info(status)
26+
logging.info(f"Image '{IMAGE_NAME}' installed")
27+
return client
28+
829

930
async def execute_python_code(self, code: str, working_directory: str) -> str:
31+
# Create working directory if it doesn't exist
32+
if not os.path.exists(working_directory):
33+
os.makedirs(working_directory)
1034
# Check if there are any package requirements in the code to install
1135
package_requirements = re.findall(r"pip install (.*)", code)
1236
if package_requirements:
@@ -22,25 +46,9 @@ async def execute_python_code(self, code: str, working_directory: str) -> str:
2246
with open(temp_file, "w") as f:
2347
f.write(code)
2448
try:
25-
client = docker.from_env()
26-
image_name = "joshxt/safeexecute:latest"
27-
try:
28-
client.images.get(image_name)
29-
logging.info(f"Image '{image_name}' found locally")
30-
except ImageNotFound:
31-
logging.info(
32-
f"Image '{image_name}' not found locally, pulling from Docker Hub"
33-
)
34-
low_level_client = docker.APIClient()
35-
for line in low_level_client.pull(image_name, stream=True, decode=True):
36-
status = line.get("status")
37-
progress = line.get("progress")
38-
if status and progress:
39-
logging.info(f"{status}: {progress}")
40-
elif status:
41-
logging.info(status)
49+
client = install_docker_image()
4250
container = client.containers.run(
43-
image_name,
51+
IMAGE_NAME,
4452
f"python {temp_file}",
4553
volumes={
4654
os.path.abspath(working_directory): {
@@ -60,3 +68,7 @@ async def execute_python_code(self, code: str, working_directory: str) -> str:
6068
return logs
6169
except Exception as e:
6270
return f"Error: {str(e)}"
71+
72+
73+
if __name__ == "__main__":
74+
install_docker_image()

0 commit comments

Comments
 (0)