SuperAPI AI 工具集

python-executor

通用与其它

查看项目

更新于 10-15 08:33

技能介绍

Python Executor 是一个在安全沙箱环境中执行 Python 代码的工具技能,通过 inference.sh 平台运行,需配合 belt CLI 使用。

用途

Python Executor 用于在隔离的沙箱环境中运行 Python 代码,适合需要执行脚本但又希望避免本地环境依赖或安全风险的任务,例如数据处理、文件生成、网络请求和自动化脚本等。

主要能力

该工具提供 Python 3.10 运行环境,仅使用 CPU。默认配置为 8GB 内存,另提供 @high_memory 变体,内存提升至 16GB,适用于大型数据集。执行超时时间可设置为 1 至 300 秒,默认 30 秒。

环境预装了 100 多个库,按用途分为:

  • 网页抓取与 HTTP:requests、httpx、aiohttp(HTTP 客户端),beautifulsoup4、lxml(HTML/XML 解析),selenium、playwright(浏览器自动化),scrapy(爬虫框架)
  • 数据处理:numpy、pandas、scipy(数值计算),matplotlib、seaborn、plotly(可视化)
  • 图像处理:pillow、opencv-python-headless(图像操作),scikit-image、imageio(图像算法)
  • 视频与音频:moviepy(视频编辑),av (PyAV)、ffmpeg-python(视频处理),pydub(音频处理)
  • 3D 处理:trimesh、open3d(3D 网格处理),numpy-stl、meshio、pyvista(3D 文件格式)
  • 文档与图形:svgwrite、cairosvg(SVG 创建),reportlab、pypdf2(PDF 生成)

使用方式

首先安装 belt CLI 技能:

npx skills add belt-sh/cli

然后登录:

belt login

通过命令行提交代码,输入为 JSON 格式。输入字段包括 code(要执行的代码)、timeout(超时秒数)、capture_output(是否捕获输出)和 working_dir(工作目录,默认为 null)。基础命令格式如下:

belt app run infsh/python-executor --input '{
"code": "import pandas as pd\nprint(pd.__version__)"
}'

示例:

网页抓取:

belt app run infsh/python-executor --input '{
  "code": "import requests\nfrom bs4 import BeautifulSoup\n\nresponse = requests.get(\"https://example.com\")\nsoup = BeautifulSoup(response.content, \"html.parser\")\nprint(soup.find(\"title\").text)"
}'

数据分析与可视化:

belt app run infsh/python-executor --input '{
  "code": "import pandas as pd\nimport matplotlib.pyplot as plt\n\ndata = {\"name\": [\"Alice\", \"Bob\"], \"sales\": [100, 150]}\ndf = pd.DataFrame(data)\n\nplt.bar(df[\"name\"], df[\"sales\"])\nplt.savefig(\"outputs/chart.png\")\nprint(\"Chart saved!\")"
}'

图像处理:

belt app run infsh/python-executor --input '{
  "code": "from PIL import Image\nimport numpy as np\n\n# Create gradient image\narr = np.linspace(0, 255, 256*256, dtype=np.uint8).reshape(256, 256)\nimg = Image.fromarray(arr, mode=\"L\")\nimg.save(\"outputs/gradient.png\")\nprint(\"Image created!\")"
}'

视频创建:

belt app run infsh/python-executor --input '{
  "code": "from moviepy.editor import ColorClip, TextClip, CompositeVideoClip\n\nclip = ColorClip(size=(640, 480), color=(0, 100, 200), duration=3)\ntxt = TextClip(\"Hello!\", fontsize=70, color=\"white\").set_position(\"center\").set_duration(3)\nvideo = CompositeVideoClip([clip, txt])\nvideo.write_videofile(\"outputs/hello.mp4\", fps=24)\nprint(\"Video created!\")",
  "timeout": 120
}'

3D 模型处理:

belt app run infsh/python-executor --input '{
  "code": "import trimesh\n\nsphere = trimesh.creation.icosphere(subdivisions=3, radius=1.0)\nsphere.export(\"outputs/sphere.stl\")\nprint(f\"Created sphere with {len(sphere.vertices)} vertices\")"
}'

API 调用:

belt app run infsh/python-executor --input '{
  "code": "import requests\nimport json\n\nresponse = requests.get(\"https://api.github.com/users/octocat\")\ndata = response.json()\nprint(json.dumps(data, indent=2))"
}'

保存到 outputs/ 目录的文件会被自动检测并返回,例如 plt.savefig('outputs/chart.png')、df.to_csv('outputs/data.csv')、video.write_videofile('outputs/video.mp4')、mesh.export('outputs/model.stl')。

高内存变体使用方式:

belt app run infsh/python-executor@high_memory --input input.json

适用场景

  • 网页抓取:从网站提取数据
  • 数据分析:处理和可视化数据集
  • 图像操作:调整大小、裁剪、合成图像
  • 视频创建:生成带文字叠加的视频
  • 3D 处理:加载、变换、导出 3D 模型
  • API 集成:调用外部 API
  • PDF 生成:创建报告和文档
  • 自动化:运行任意 Python 脚本

限制

  • 仅使用 CPU,不提供 GPU/ML 库(如需机器学习相关功能,建议使用专门的 AI 应用)
  • 在隔离子进程中运行,确保安全
  • 非交互式执行,需使用 plt.savefig() 而非 plt.show()
  • 输出文件会被自动检测并返回

相关技能

如需 AI 图像生成、AI 视频生成或 LLM 文本生成,可分别安装以下技能:

npx skills add inference-sh/skills@ai-image-generation
npx skills add inference-sh/skills@ai-video-generation
npx skills add inference-sh/skills@llm-models