{"id":14959643,"url":"https://github.com/typper-io/ai-code-sandbox","last_synced_at":"2025-05-02T12:31:46.380Z","repository":{"id":253595554,"uuid":"843976912","full_name":"typper-io/ai-code-sandbox","owner":"typper-io","description":"Secure Python sandbox for AI/ML code execution using Docker. Run LLM outputs safely.","archived":false,"fork":false,"pushed_at":"2025-04-29T16:40:34.000Z","size":10,"stargazers_count":41,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-29T17:42:12.391Z","etag":null,"topics":["ai","chatgpt","claude","code-generation","code-interpreter","llm","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/ai-code-sandbox","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typper-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-18T02:06:18.000Z","updated_at":"2025-04-29T16:40:38.000Z","dependencies_parsed_at":"2025-04-29T17:31:51.314Z","dependency_job_id":"ee6c3b7a-3c7f-4d01-bd58-e3dcb7b85b60","html_url":"https://github.com/typper-io/ai-code-sandbox","commit_stats":null,"previous_names":["typper-io/ai-code-sandbox"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typper-io%2Fai-code-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typper-io%2Fai-code-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typper-io%2Fai-code-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typper-io%2Fai-code-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typper-io","download_url":"https://codeload.github.com/typper-io/ai-code-sandbox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252038198,"owners_count":21684647,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","chatgpt","claude","code-generation","code-interpreter","llm","python"],"created_at":"2024-09-24T13:20:18.694Z","updated_at":"2025-05-02T12:31:46.372Z","avatar_url":"https://github.com/typper-io.png","language":"Python","funding_links":[],"categories":["python","ai"],"sub_categories":[],"readme":"# AICodeSandbox\n\nAICodeSandbox is a Python library designed to provide a secure and isolated environment for executing AI and machine learning code, particularly for Language Models (LLMs). It leverages Docker containers to create sandboxes, enabling safe execution of potentially untrusted AI-generated code.\n\n## Features\n\n- Create isolated Python environments using Docker containers\n- Securely run AI-generated code or LLM outputs\n- Install custom Python packages in the sandbox\n- Execute Python code safely within the sandbox\n- Read and write files within the sandbox environment\n- Automatically clean up resources after use\n\n## Key Advantages\n\n- **Security**: Isolates AI-generated code execution, protecting your system from potentially harmful operations.\n- **Speed**: Optimized container creation and management for quick sandbox setup and execution.\n- **Customization**: Easily add specific Python packages or use custom Docker images to suit your AI and ML needs.\n- **Resource Control**: Limit CPU and memory usage to prevent resource abuse.\n- **Flexibility**: Run various types of AI models and code snippets without worrying about system integrity.\n- **Easy Clean-up**: Automatic resource management ensures no leftover containers or images.\n\n## Requirements\n\nTo run AICodeSandbox, you need:\n\n- Python 3.7+\n- Docker installed and running on your system\n- `docker` Python package\n- Sufficient permissions to create and manage Docker containers\n- Internet connection (for initial package downloads)\n\n## Installation\n\n1. Clone this repository:\n   ```\n   git clone https://github.com/typper-io/ai-code-sandbox.git\n   cd ai-code-sandbox\n   ```\n\n2. Install the required Python packages:\n   ```\n   pip install -r requirements.txt\n   ```\n\n## Usage\n\nHere's a basic example of how to use AICodeSandbox:\n\n```python\nfrom ai_code_sandbox import AICodeSandbox\n\n# Create a sandbox with common AI/ML packages\nsandbox = AICodeSandbox(packages=[\"numpy\", \"pandas\", \"scikit-learn\", \"tensorflow\"])\n\ntry:\n    # Run some AI-generated code in the sandbox\n    code = \"\"\"\n    import numpy as np\n    import pandas as pd\n    from sklearn.model_selection import train_test_split\n    from tensorflow.keras.models import Sequential\n    from tensorflow.keras.layers import Dense\n\n    # Generate some dummy data\n    X = np.random.rand(1000, 10)\n    y = np.random.randint(0, 2, 1000)\n\n    # Split the data\n    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n\n    # Create a simple neural network\n    model = Sequential([\n        Dense(64, activation='relu', input_shape=(10,)),\n        Dense(32, activation='relu'),\n        Dense(1, activation='sigmoid')\n    ])\n\n    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n    # Train the model\n    history = model.fit(X_train, y_train, epochs=10, validation_split=0.2, verbose=0)\n\n    # Evaluate the model\n    loss, accuracy = model.evaluate(X_test, y_test, verbose=0)\n    print(f\"Test accuracy: {accuracy:.4f}\")\n    \"\"\"\n    result = sandbox.run_code(code)\n    print(result)\n\nfinally:\n    # Always close the sandbox to clean up resources\n    sandbox.close()\n```\n\n## Running in Docker\n\nYou can also run AICodeSandbox inside a Docker container. This setup uses Docker-in-Docker (DinD) to allow the AICodeSandbox to create and manage Docker containers from within a Docker container.\n\nExample `Dockerfile`:\n\n```dockerfile\nFROM docker:dind\n\nRUN apk add --no-cache python3 py3-pip\n\nWORKDIR /app\n\nCOPY requirements.txt .\nCOPY README.md .\nCOPY ai_code_sandbox/ ./ai_code_sandbox/\nCOPY setup.py .\n\nRUN python3 -m venv /app/venv\nENV PATH=\"/app/venv/bin:$PATH\"\n\nRUN pip3 install --upgrade pip\nRUN pip3 install -r requirements.txt\nRUN pip3 install -e .\n\nCOPY example.py .\n\nCMD [\"python3\", \"example.py\"]\n```\n\nThe docker compose `docker-compose.yml`:\n\n```yml\nversion: '3.8'\n\nservices:\n  ai_sandbox:\n    build: .\n    volumes:\n      - /var/run/docker.sock:/var/run/docker.sock\n    privileged: true\n    environment:\n      - DOCKER_TLS_CERTDIR=\"\"\n```\n\n## API Reference\n\n### `AICodeSandbox(custom_image=None, packages=None)`\n\nCreate a new sandbox environment.\n\n- `custom_image` (optional): Name of a custom Docker image to use.\n- `packages` (optional): List of Python packages to install in the sandbox.\n- `network_mode` (optional): Network mode to use for the sandbox. Defaults to \"none\".\n- `mem_limit` (optional): Memory limit for the sandbox. Defaults to \"100m\".\n- `cpu_period` (optional): CPU period for the sandbox. Defaults to 100000.\n- `cpu_quota` (optional): CPU quota for the sandbox. Defaults to 50000.\n\n### `sandbox.run_code(code, env_vars=None)`\n\nExecute Python code in the sandbox.\n\n- `code`: String containing Python code to execute.\n- `env_vars` (optional): Dictionary of environment variables to set for the execution.\n\n### `sandbox.write_file(filename, content)`\n\nWrite content to a file in the sandbox.\n\n- `filename`: Name of the file to create or overwrite.\n- `content`: String content to write to the file.\n\n### `sandbox.read_file(filename)`\n\nRead content from a file in the sandbox.\n\n- `filename`: Name of the file to read.\n\n### `sandbox.close()`\n\nRemove all resources created by the sandbox.\n\n## Security Considerations\n\nWhile AICodeSandbox provides a secure environment for running AI-generated code, it's important to note that no sandbox solution is completely foolproof. Users should still exercise caution and implement additional security measures when dealing with potentially malicious or untrusted AI-generated code.\n\n## Contributing\n\nContributions to AICodeSandbox are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypper-io%2Fai-code-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypper-io%2Fai-code-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypper-io%2Fai-code-sandbox/lists"}