Spaces:
Sleeping
Sleeping
Sync from GitHub
Browse files- .gitattributes +1 -0
- .gitignore +216 -0
- LICENSE +21 -0
- Pipfile +36 -0
- Pipfile.lock +0 -0
- README.md +214 -9
- app.py +11 -0
- gradio_dashboard.jpg +3 -0
- requirements.txt +4 -0
- ruff.toml +9 -0
- setup.cfg +22 -0
- src/__init__.py +18 -0
- src/__main__.py +50 -0
- src/compiler.py +62 -0
- src/examples.py +76 -0
- src/interface.py +85 -0
- src/optimizer.py +50 -0
- src/prompts.py +61 -0
- src/tester.py +46 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
gradio_dashboard.jpg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[codz]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py.cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# UV
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
#uv.lock
|
| 102 |
+
|
| 103 |
+
# poetry
|
| 104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 106 |
+
# commonly ignored for libraries.
|
| 107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 108 |
+
#poetry.lock
|
| 109 |
+
#poetry.toml
|
| 110 |
+
|
| 111 |
+
# pdm
|
| 112 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 113 |
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
| 114 |
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
| 115 |
+
#pdm.lock
|
| 116 |
+
#pdm.toml
|
| 117 |
+
.pdm-python
|
| 118 |
+
.pdm-build/
|
| 119 |
+
|
| 120 |
+
# pixi
|
| 121 |
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
| 122 |
+
#pixi.lock
|
| 123 |
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
| 124 |
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
| 125 |
+
.pixi
|
| 126 |
+
|
| 127 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 128 |
+
__pypackages__/
|
| 129 |
+
|
| 130 |
+
# Celery stuff
|
| 131 |
+
celerybeat-schedule
|
| 132 |
+
celerybeat.pid
|
| 133 |
+
|
| 134 |
+
# SageMath parsed files
|
| 135 |
+
*.sage.py
|
| 136 |
+
|
| 137 |
+
# Environments
|
| 138 |
+
.env
|
| 139 |
+
.envrc
|
| 140 |
+
.venv
|
| 141 |
+
env/
|
| 142 |
+
venv/
|
| 143 |
+
ENV/
|
| 144 |
+
env.bak/
|
| 145 |
+
venv.bak/
|
| 146 |
+
|
| 147 |
+
# Spyder project settings
|
| 148 |
+
.spyderproject
|
| 149 |
+
.spyproject
|
| 150 |
+
|
| 151 |
+
# Rope project settings
|
| 152 |
+
.ropeproject
|
| 153 |
+
|
| 154 |
+
# mkdocs documentation
|
| 155 |
+
/site
|
| 156 |
+
|
| 157 |
+
# mypy
|
| 158 |
+
.mypy_cache/
|
| 159 |
+
.dmypy.json
|
| 160 |
+
dmypy.json
|
| 161 |
+
|
| 162 |
+
# Pyre type checker
|
| 163 |
+
.pyre/
|
| 164 |
+
|
| 165 |
+
# pytype static type analyzer
|
| 166 |
+
.pytype/
|
| 167 |
+
|
| 168 |
+
# Cython debug symbols
|
| 169 |
+
cython_debug/
|
| 170 |
+
|
| 171 |
+
# PyCharm
|
| 172 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 173 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 174 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 175 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 176 |
+
#.idea/
|
| 177 |
+
|
| 178 |
+
# Abstra
|
| 179 |
+
# Abstra is an AI-powered process automation framework.
|
| 180 |
+
# Ignore directories containing user credentials, local state, and settings.
|
| 181 |
+
# Learn more at https://abstra.io/docs
|
| 182 |
+
.abstra/
|
| 183 |
+
|
| 184 |
+
# Visual Studio Code
|
| 185 |
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
| 186 |
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
| 187 |
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
| 188 |
+
# you could uncomment the following to ignore the entire vscode folder
|
| 189 |
+
# .vscode/
|
| 190 |
+
|
| 191 |
+
# Ruff stuff:
|
| 192 |
+
.ruff_cache/
|
| 193 |
+
|
| 194 |
+
# PyPI configuration file
|
| 195 |
+
.pypirc
|
| 196 |
+
|
| 197 |
+
# Cursor
|
| 198 |
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
| 199 |
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
| 200 |
+
# refer to https://docs.cursor.com/context/ignore-files
|
| 201 |
+
.cursorignore
|
| 202 |
+
.cursorindexingignore
|
| 203 |
+
|
| 204 |
+
# Marimo
|
| 205 |
+
marimo/_static/
|
| 206 |
+
marimo/_lsp/
|
| 207 |
+
__marimo__/
|
| 208 |
+
|
| 209 |
+
# Generated Scripts
|
| 210 |
+
**activate-*-env.*
|
| 211 |
+
|
| 212 |
+
# Extension generation and testing files
|
| 213 |
+
*.c
|
| 214 |
+
*.pyd
|
| 215 |
+
setup.py
|
| 216 |
+
usage_example.py
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026 Carlos Bazaga
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
Pipfile
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[[source]]
|
| 2 |
+
name = "pypi"
|
| 3 |
+
verify_ssl = true
|
| 4 |
+
url = "https://pypi.org/simple"
|
| 5 |
+
|
| 6 |
+
[packages]
|
| 7 |
+
# Environment.
|
| 8 |
+
python-dotenv = "*"
|
| 9 |
+
# Networking.
|
| 10 |
+
requests = "*"
|
| 11 |
+
# Models SDK.
|
| 12 |
+
openai = "*"
|
| 13 |
+
# UI Framework.
|
| 14 |
+
gradio = "*"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
[dev-packages]
|
| 18 |
+
# Formatters.
|
| 19 |
+
isort = "*"
|
| 20 |
+
# Linters.
|
| 21 |
+
mypy = "*"
|
| 22 |
+
mccabe = "*"
|
| 23 |
+
pyflakes = "*"
|
| 24 |
+
pydocstyle = "*"
|
| 25 |
+
pycodestyle = "*"
|
| 26 |
+
# Testing.
|
| 27 |
+
pytest = "*"
|
| 28 |
+
coverage = "*"
|
| 29 |
+
# Interactive Notebook.
|
| 30 |
+
ipykernel = "*"
|
| 31 |
+
ipywidgets = "*"
|
| 32 |
+
# Build Tools.
|
| 33 |
+
setuptools = "*"
|
| 34 |
+
|
| 35 |
+
[requires]
|
| 36 |
+
python_version = "3.13"
|
Pipfile.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
README.md
CHANGED
|
@@ -1,15 +1,220 @@
|
|
| 1 |
---
|
| 2 |
-
title: Python C
|
| 3 |
-
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.14.0
|
| 8 |
-
python_version: '3.13'
|
| 9 |
-
app_file: app.py
|
| 10 |
-
pinned: false
|
| 11 |
license: mit
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Python C Extension Generator
|
| 3 |
+
app_file: app.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 6.14.0
|
|
|
|
|
|
|
|
|
|
| 6 |
license: mit
|
| 7 |
+
emoji: 🐉
|
| 8 |
+
colorFrom: blue
|
| 9 |
+
colorTo: green
|
| 10 |
+
short_description: High-performance Python C Extension generator from Python.
|
| 11 |
+
pinned: true
|
| 12 |
+
thumbnail: >-
|
| 13 |
+
https://cdn-uploads.huggingface.co/production/uploads/67caf50af30e4fe450042ac4/GBtJw_6f8kKFbPaAi6dXk.png
|
| 14 |
---
|
| 15 |
|
| 16 |
+
## Python C Extension code generator
|
| 17 |
+
|
| 18 |
+
A Gradio app that provides an interactive interface for users to input Python code and
|
| 19 |
+
generate C extension code
|
| 20 |
+
|
| 21 |
+
Optionally, a compile and eval stage can be activated for local deployments to compare
|
| 22 |
+
its performance against the original Python code.
|
| 23 |
+
|
| 24 |
+
> [!CAUTION]
|
| 25 |
+
>
|
| 26 |
+
> **Always review the generated codes before running them, as they will be executed in
|
| 27 |
+
> your local environment and may contain code that could be harmful or unwanted.**
|
| 28 |
+
>
|
| 29 |
+
> AI-generated code may contain errors or unsafe practices, so it's crucial to
|
| 30 |
+
> thoroughly review and test on a sandboxed environment any code before using it in a
|
| 31 |
+
> production environment.
|
| 32 |
+
>
|
| 33 |
+
> Never run code generated by AI models without understanding its implications and
|
| 34 |
+
> ensuring it adheres to your security and safety standards.
|
| 35 |
+
|
| 36 |
+
> [!IMPORTANT]
|
| 37 |
+
>
|
| 38 |
+
> **Disclaimer:** This App and Notebook are provided for educational purposes only.
|
| 39 |
+
> Use it at your own risk.
|
| 40 |
+
|
| 41 |
+
### Installation
|
| 42 |
+
|
| 43 |
+
* Install the required Python dependencies:
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
pip install -r requirements.txt
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
* Or, if you prefer Pipenv:
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
pipenv install
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
### Configuration
|
| 56 |
+
|
| 57 |
+
The app reads configuration from environment variables and from a `.env` file if present.
|
| 58 |
+
|
| 59 |
+
* `MODELS`: Colon-separated list of models to expose in the dropdown.\
|
| 60 |
+
Example:
|
| 61 |
+
|
| 62 |
+
```bash
|
| 63 |
+
export MODELS="gpt-5.1-codex-mini:gpt-5.4-mini"
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
```powershell
|
| 67 |
+
$env:MODELS = "gpt-5.1-codex-mini:gpt-5.4-mini"
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
If not set, the app defaults to `gpt-5.1-codex-mini` and `gpt-5.4-mini`.
|
| 71 |
+
|
| 72 |
+
* `COMPILE_STAGE`: Set to `true`, `1`, or `yes` to enable the compile and test stage.\
|
| 73 |
+
Example:
|
| 74 |
+
|
| 75 |
+
```bash
|
| 76 |
+
export COMPILE_STAGE=true
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
```powershell
|
| 80 |
+
$env:COMPILE_STAGE = "true"
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Running locally
|
| 84 |
+
|
| 85 |
+
* Run the app locally with:
|
| 86 |
+
|
| 87 |
+
```bash
|
| 88 |
+
python app.py
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
* For autoreload during development, use the Gradio CLI:
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
gradio app.py
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
### Gradio app overview
|
| 98 |
+
|
| 99 |
+
In this image, you can see the Gradio app dashboard whose main sections are
|
| 100 |
+
described below.
|
| 101 |
+
|
| 102 |
+
\
|
| 103 |
+
*Image: Gradio app dashboard with default example `hello world` code loaded.*
|
| 104 |
+
*(compile output redacted for privacy)*
|
| 105 |
+
|
| 106 |
+
Sections:
|
| 107 |
+
|
| 108 |
+
* **Dropdown selectors and input fields**:
|
| 109 |
+
* **Module name input**:
|
| 110 |
+
A text input field where users can specify the name of the C extension module to be
|
| 111 |
+
generated.
|
| 112 |
+
|
| 113 |
+
That name will be used to create the C extension file `<module_name>.c` and
|
| 114 |
+
the `setup.py` file required to compile the extension.
|
| 115 |
+
|
| 116 |
+
That name will also be used to import the compiled module as usual in Python:
|
| 117 |
+
|
| 118 |
+
```python
|
| 119 |
+
import <module_name>
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
Or
|
| 123 |
+
|
| 124 |
+
```python
|
| 125 |
+
from <module_name> import <function_name>
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
* **Model selector**:
|
| 129 |
+
A dropdown menu to select the model used for code generation.
|
| 130 |
+
|
| 131 |
+
The available options are taken from the `MODELS` environment variable if set.
|
| 132 |
+
Otherwise the app defaults to `gpt-5.1-codex-mini` and `gpt-5.4-mini`.
|
| 133 |
+
|
| 134 |
+
* **Platform selector**:
|
| 135 |
+
A dropdown menu to select the target platform for the generated C extension.
|
| 136 |
+
|
| 137 |
+
This affects how the app frames the prompt for the model and ensures the
|
| 138 |
+
generated code targets the selected platform (`Windows` or `Linux`).
|
| 139 |
+
|
| 140 |
+
* **Examples selector**:
|
| 141 |
+
A list of ready-made Python examples to load into the input field.
|
| 142 |
+
|
| 143 |
+
Built-in examples include `Hello world`, `Sum array`, `Fibonacci`, `Leibniz pi`,
|
| 144 |
+
and `Max subarray sum`.
|
| 145 |
+
|
| 146 |
+
* **Text input areas**:
|
| 147 |
+
|
| 148 |
+
These areas are all editable, including those filled with generated code by the model.
|
| 149 |
+
This allows users to modify and experiment with the code as needed.
|
| 150 |
+
|
| 151 |
+
* **Python code**:
|
| 152 |
+
A text area where users can input their Python code.
|
| 153 |
+
|
| 154 |
+
> [!NOTE]
|
| 155 |
+
>
|
| 156 |
+
> We are creating an importable module not an executable program so the code to be
|
| 157 |
+
> optimized must contain only declarations such as DEF or CLASS.
|
| 158 |
+
|
| 159 |
+
* **C extension code**:
|
| 160 |
+
A text area that displays the generated C extension code.
|
| 161 |
+
* **Compilation code**:
|
| 162 |
+
A text area that shows the generated `setup.py` code.
|
| 163 |
+
This file is required to compile the C extension.
|
| 164 |
+
* **Test compare code**:
|
| 165 |
+
A text area that provides example code to run the compiled C extension.
|
| 166 |
+
|
| 167 |
+
* **Output areas**:
|
| 168 |
+
|
| 169 |
+
These are non-editable areas that display the results of various operations.
|
| 170 |
+
|
| 171 |
+
* **C Extension result**:
|
| 172 |
+
A text area that displays the output of the C extension code build.
|
| 173 |
+
|
| 174 |
+
Beware that this area can contain a large amount of text including warnings during
|
| 175 |
+
the compilation process and sensible information about the local environment,
|
| 176 |
+
like: paths, Python version, etc may be included.
|
| 177 |
+
|
| 178 |
+
Redact that information if you plan to share the output.
|
| 179 |
+
|
| 180 |
+
* **Test result**:
|
| 181 |
+
A text area that displays the output of the test code run.
|
| 182 |
+
|
| 183 |
+
* **Buttons**:
|
| 184 |
+
* **Generate extension code**:
|
| 185 |
+
A button that triggers the generation of the C extension code from the provided
|
| 186 |
+
Python code.
|
| 187 |
+
|
| 188 |
+
It will call the model to generate the C code, the setup.py file and the test code,
|
| 189 |
+
filling the corresponding text areas automatically.
|
| 190 |
+
|
| 191 |
+
* **Compile extension**:
|
| 192 |
+
A button that compiles the generated C extension using the provided `setup.py` file.
|
| 193 |
+
It will create the extension C file, `<module_name>.c`, and the `setup.py` file in
|
| 194 |
+
the local folder, then it will run the compilation command and build the C extension,
|
| 195 |
+
`<module_name>.<arch_info>.pyd`.\
|
| 196 |
+
*`arch_info` represents architecture info the extension has been compiled for:*
|
| 197 |
+
|
| 198 |
+
* `hello_world.cp313-win_amd64.pyd`: CPython 3.13 + Windows + Amd64 Architecture.
|
| 199 |
+
|
| 200 |
+
> [!CAUTION]
|
| 201 |
+
>
|
| 202 |
+
> **Always review the `setup.py` code before running it, as it will be executed in
|
| 203 |
+
> your local environment and may contain code that could be harmful or unwanted.**
|
| 204 |
+
>
|
| 205 |
+
> **Also review the generated C code, as it will be compiled and executed in your
|
| 206 |
+
> local environment and may contain code that could be harmful or unwanted.**
|
| 207 |
+
|
| 208 |
+
It will display the compilation output in the "C Extension result" area.
|
| 209 |
+
|
| 210 |
+
* **Test code**:
|
| 211 |
+
A button that executes the test code to compare the performance of the original
|
| 212 |
+
Python code and the generated C extension.
|
| 213 |
+
|
| 214 |
+
> [!CAUTION]
|
| 215 |
+
>
|
| 216 |
+
> **Always review the test code before running it, as it will be executed in
|
| 217 |
+
> your local environment and may contain code that could be harmful or unwanted.**
|
| 218 |
+
|
| 219 |
+
Will save the test code provided in the "Test compare code" into the
|
| 220 |
+
`usage_example.py` file and execute it, showing the output in the "Test result" area.
|
app.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Entrypoint wrapper for Hugging Face Spaces. Delegates to src/__main__.py."""
|
| 2 |
+
|
| 3 |
+
from src.__main__ import main
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# HBD: We assign the 'app' to 'demo' to allow watch autoreload:
|
| 7 |
+
# 'demo' is required to be defined at the global scope for reload to work in Gradio.
|
| 8 |
+
# Initial warnings may appear at launch with 'gradio app.py' because 'demo' will not
|
| 9 |
+
# exists until the app service ends inside main, but it will do when reload requires it,
|
| 10 |
+
# once the service has been stopped.
|
| 11 |
+
demo = main()
|
gradio_dashboard.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
requests
|
| 3 |
+
gradio
|
| 4 |
+
openai
|
ruff.toml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
line-length = 89
|
| 2 |
+
|
| 3 |
+
[lint]
|
| 4 |
+
select = ["E", "D", "W"]
|
| 5 |
+
ignore = ["D203", "D204", "D208", "D213"]
|
| 6 |
+
|
| 7 |
+
[lint.per-file-ignores]
|
| 8 |
+
"*.ipynb" = ["ALL"]
|
| 9 |
+
"*.interactive" = ["ALL"]
|
setup.cfg
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[isort]
|
| 2 |
+
line_length = 89
|
| 3 |
+
multi_line_output = 9
|
| 4 |
+
lines_after_imports = 2
|
| 5 |
+
known_first_party = src
|
| 6 |
+
|
| 7 |
+
[mypy]
|
| 8 |
+
implicit_optional = True
|
| 9 |
+
ignore_missing_imports = True
|
| 10 |
+
explicit_package_bases = True
|
| 11 |
+
disable_error_code = import-untyped
|
| 12 |
+
|
| 13 |
+
[pycodestyle]
|
| 14 |
+
max-line-length = 89
|
| 15 |
+
ignore = E133, W503
|
| 16 |
+
|
| 17 |
+
[pydocstyle]
|
| 18 |
+
ignore = D203, D204, D208, D213
|
| 19 |
+
|
| 20 |
+
[pylint.MESSAGES CONTROL]
|
| 21 |
+
disable = W1203, W0718
|
| 22 |
+
# disable = logging-fstring-interpolation, broad-exception-caught
|
src/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Initialization module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
from logging import basicConfig, getLogger
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Environment initialization.
|
| 9 |
+
load_dotenv(override=True)
|
| 10 |
+
|
| 11 |
+
# Setup the global logger.
|
| 12 |
+
LOG_STYLE = '{'
|
| 13 |
+
LOG_LEVEL = 'INFO'
|
| 14 |
+
LOG_FORMAT = ('{asctime} {levelname:<8} {processName}({process}) '
|
| 15 |
+
'{threadName} {name} {lineno} "{message}"')
|
| 16 |
+
basicConfig(level=LOG_LEVEL, style='{', format=LOG_FORMAT)
|
| 17 |
+
|
| 18 |
+
getLogger(__name__).info('INITIALIZED AI PYTHON C EXTENSIONS GENERATOR')
|
src/__main__.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Main entrypoint module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
from logging import getLogger
|
| 4 |
+
from os import getenv
|
| 5 |
+
from sys import platform
|
| 6 |
+
|
| 7 |
+
from .compiler import compile_extension
|
| 8 |
+
from .interface import get_interface
|
| 9 |
+
from .optimizer import optimize_gpt as optimize_function
|
| 10 |
+
from .tester import test_extension
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
_logger = getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
# Default models list
|
| 16 |
+
DEFAULT_MODELS = ["gpt-5.1-codex", "gpt-5.4"] # Expensive.
|
| 17 |
+
DEFAULT_MODELS = ["gpt-5.1-codex-mini", "gpt-5.4-mini"] # Cheap.
|
| 18 |
+
|
| 19 |
+
# Read available models from environment variable or use defaults
|
| 20 |
+
models = models_env.split(":") if (models_env := getenv("MODELS")) else DEFAULT_MODELS
|
| 21 |
+
|
| 22 |
+
# Detect system platform for default selection.
|
| 23 |
+
if platform == "win32":
|
| 24 |
+
default_platform = "Windows"
|
| 25 |
+
else:
|
| 26 |
+
default_platform = "Linux"
|
| 27 |
+
|
| 28 |
+
# Read compile stage flag from environment variable, defaulting to False if not set.
|
| 29 |
+
compile_stage = getenv("COMPILE_STAGE", "False").lower() in ("true", "1", "yes")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
"""Launch the AI Python C Extensions Generator application."""
|
| 34 |
+
_logger.info('STARTING AI PYTHON C EXTENSIONS GENERATOR...')
|
| 35 |
+
_logger.info(f'AVAILABLE MODELS: {models}')
|
| 36 |
+
# Prepare the interface configuration based.
|
| 37 |
+
interface_config = {"models": models, "compile_stage": compile_stage,
|
| 38 |
+
"optimize_function": optimize_function}
|
| 39 |
+
if compile_stage:
|
| 40 |
+
_logger.info('COMPILE STAGE ENABLED')
|
| 41 |
+
interface_config.update({"compile_extension": compile_extension,
|
| 42 |
+
"test_extension": test_extension})
|
| 43 |
+
app = get_interface(**interface_config)
|
| 44 |
+
app.launch(footer_links=[])
|
| 45 |
+
# We return the app instance for potential use in autoreload scenarios.
|
| 46 |
+
return app
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
main()
|
src/compiler.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Compilation and tests module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import subprocess
|
| 5 |
+
import sys
|
| 6 |
+
from logging import getLogger
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
_logger = getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Define a function to write outputs to a file with a given filename.
|
| 13 |
+
def write_file(data, filename):
|
| 14 |
+
"""Write data to a file with the specified filename."""
|
| 15 |
+
with open(filename, "w") as file:
|
| 16 |
+
file.write(data)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Extension compilation function.
|
| 20 |
+
def build_extension():
|
| 21 |
+
"""Compile the C extension using 'setup.py' and return the compilation output."""
|
| 22 |
+
# Set default COMSPEC to cmd.exe on Windows to avoid issues with some C compilers.
|
| 23 |
+
if sys.platform == "win32":
|
| 24 |
+
_logger.info('PATCHING COMSPEC FOR WINDOWS COMPILATION COMPATIBILITY...')
|
| 25 |
+
preset_comspec = os.environ.get("COMSPEC")
|
| 26 |
+
os.environ["COMSPEC"] = "C:\\Windows\\System32\\cmd.exe"
|
| 27 |
+
try:
|
| 28 |
+
_logger.info('STARTING COMPILATION PROCESS...')
|
| 29 |
+
compile_cmd = ["python", "setup.py", "build_ext", "--inplace"]
|
| 30 |
+
compile_result = subprocess.run(compile_cmd, env=os.environ,
|
| 31 |
+
check=True, text=True, capture_output=True)
|
| 32 |
+
except subprocess.CalledProcessError as ex:
|
| 33 |
+
_logger.error(f"COMPILATION FAILED WITH ERROR:\n{ex.stdout}\n{ex.stderr}")
|
| 34 |
+
raise Exception(f"An error occurred while building:\n{ex.stdout}\n{ex.stderr}")
|
| 35 |
+
finally:
|
| 36 |
+
# Restore original COMSPEC on Windows.
|
| 37 |
+
if sys.platform == "win32":
|
| 38 |
+
_logger.info('RESTORING ORIGINAL COMSPEC...')
|
| 39 |
+
os.environ["COMSPEC"] = preset_comspec
|
| 40 |
+
_logger.info('COMPILATION COMPLETED SUCCESSFULLY.')
|
| 41 |
+
return compile_result.stdout
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Extension compilation function.
|
| 45 |
+
def compile_extension(c_code, setup_code, module_name):
|
| 46 |
+
"""Build the C extension from the provided codes."""
|
| 47 |
+
try: # Write the provided codes to their respective files.
|
| 48 |
+
_logger.info('WRITING GENERATED C CODE TO FILE...')
|
| 49 |
+
write_file(c_code, f"{module_name}.c")
|
| 50 |
+
_logger.info('WRITING GENERATED SETUP CODE TO FILE...')
|
| 51 |
+
write_file(setup_code, "setup.py")
|
| 52 |
+
except Exception as ex:
|
| 53 |
+
_logger.error(f"ERROR WHILE WRITING FILES:\n{ex}")
|
| 54 |
+
return f"An error occurred while writing files:\n{ex}"
|
| 55 |
+
try: # Build the extension and capture the output.
|
| 56 |
+
_logger.info('BUILDING THE EXTENSION...')
|
| 57 |
+
build_output = build_extension()
|
| 58 |
+
except Exception as ex: # If build fails, return the error message.
|
| 59 |
+
_logger.error(f"ERROR WHILE BUILDING EXTENSION:\n{ex}")
|
| 60 |
+
return str(ex)
|
| 61 |
+
_logger.info('EXTENSION BUILT SUCCESSFULLY.')
|
| 62 |
+
return build_output
|
src/examples.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Example codes for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
# Prints 'Hello, World!' from a Python function.
|
| 4 |
+
example_hello_world = """
|
| 5 |
+
def hello_world():
|
| 6 |
+
print("Hello, World!")
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
# Iterates over an array and returns the sum of all its elements.
|
| 10 |
+
example_sum = """
|
| 11 |
+
def array_sum(arr):
|
| 12 |
+
total = 0
|
| 13 |
+
for x in arr:
|
| 14 |
+
total += x
|
| 15 |
+
return total
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# Returns the nth Fibonacci number using an iterative approach.
|
| 19 |
+
example_fibonacci = """
|
| 20 |
+
def fibonacci(n):
|
| 21 |
+
a, b = 0, 1
|
| 22 |
+
for _ in range(n):
|
| 23 |
+
a, b = b, a + b
|
| 24 |
+
return a
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Approximates π using the Leibniz alternating series formula.
|
| 28 |
+
example_leibniz_pi = """
|
| 29 |
+
def leibniz_pi(iterations):
|
| 30 |
+
result = 1.0
|
| 31 |
+
for i in range(1, iterations + 1):
|
| 32 |
+
j = i * 4 - 1
|
| 33 |
+
result -= (1 / j)
|
| 34 |
+
j = i * 4 + 1
|
| 35 |
+
result += (1 / j)
|
| 36 |
+
return result * 4
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# Finds the total maximum subarray sum over 20 runs using LCG RNG.
|
| 40 |
+
# * <https://en.wikipedia.org/wiki/Maximum_subarray_problem>
|
| 41 |
+
example_subarray = """
|
| 42 |
+
def lcg(seed, a=1664525, c=1013904223, m=2**32):
|
| 43 |
+
value = seed
|
| 44 |
+
while True:
|
| 45 |
+
value = (a * value + c) % m
|
| 46 |
+
yield value
|
| 47 |
+
|
| 48 |
+
def max_subarray_sum(n, seed, min_val, max_val):
|
| 49 |
+
lcg_gen = lcg(seed)
|
| 50 |
+
random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val
|
| 51 |
+
for _ in range(n)]
|
| 52 |
+
max_sum = float('-inf')
|
| 53 |
+
for i in range(n):
|
| 54 |
+
current_sum = 0
|
| 55 |
+
for j in range(i, n):
|
| 56 |
+
current_sum += random_numbers[j]
|
| 57 |
+
if current_sum > max_sum:
|
| 58 |
+
max_sum = current_sum
|
| 59 |
+
return max_sum
|
| 60 |
+
|
| 61 |
+
def total_max_subarray_sum(n, initial_seed, min_val, max_val):
|
| 62 |
+
total_sum = 0
|
| 63 |
+
lcg_gen = lcg(initial_seed)
|
| 64 |
+
for _ in range(20):
|
| 65 |
+
seed = next(lcg_gen)
|
| 66 |
+
total_sum += max_subarray_sum(n, seed, min_val, max_val)
|
| 67 |
+
return total_sum
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
# Dictionary of examples for easy access in the interface.
|
| 71 |
+
example_dict = {
|
| 72 |
+
"Hello world": [example_hello_world, "hello_world"],
|
| 73 |
+
"Sum array": [example_sum, "array_sum"],
|
| 74 |
+
"Fibonacci": [example_fibonacci, "fibonacci"],
|
| 75 |
+
"Leibniz pi": [example_leibniz_pi, "calculate_pi"],
|
| 76 |
+
"Max subarray sum": [example_subarray, "max_subarray_sum"]}
|
src/interface.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Interface module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
from gradio import Blocks, Button, Dropdown, Examples, Markdown, Row, TextArea, Textbox
|
| 4 |
+
|
| 5 |
+
from .examples import example_dict
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
css = """
|
| 9 |
+
.c_ext {background-color: #050;}
|
| 10 |
+
.python {background-color: #306998;}
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _compile_extension():
|
| 15 |
+
return "COMPILE_EXTENSION PLACEHOLDER"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _test_extension():
|
| 19 |
+
return "TEST_EXTENSION PLACEHOLDER"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_interface(optimize_function, compile_stage=False,
|
| 23 |
+
compile_extension=_compile_extension,
|
| 24 |
+
test_extension=_test_extension,
|
| 25 |
+
default_platform="Windows",
|
| 26 |
+
models=["gpt-5.1-codex-mini"]):
|
| 27 |
+
"""Get the Gradio Blocks interface for the AI Python C Extensions Generator."""
|
| 28 |
+
with Blocks(css=css, title="AI Python C Extensions Generator") as ui:
|
| 29 |
+
Markdown("## Convert code from Python to C Extension")
|
| 30 |
+
|
| 31 |
+
with Row():
|
| 32 |
+
module_name = Textbox(label="Module name:", lines=1, value="sample_module")
|
| 33 |
+
model = Dropdown(label="Select model", choices=models, value=models[0])
|
| 34 |
+
platform = Dropdown(label="Select platform", choices=["Windows", "Linux"],
|
| 35 |
+
value=default_platform)
|
| 36 |
+
|
| 37 |
+
with Row():
|
| 38 |
+
python = Textbox(label="Python code:", lines=30,
|
| 39 |
+
buttons=["copy"], elem_classes=["python"],
|
| 40 |
+
value=example_dict["Hello world"][0])
|
| 41 |
+
c_code = Textbox(label="C Extension code:", lines=30,
|
| 42 |
+
buttons=["copy"], elem_classes=["c_ext"])
|
| 43 |
+
|
| 44 |
+
with Row():
|
| 45 |
+
Examples(label="Python examples: (From simple to complex)",
|
| 46 |
+
inputs=[python, module_name],
|
| 47 |
+
examples=list(example_dict.values()),
|
| 48 |
+
example_labels=list(example_dict.keys()))
|
| 49 |
+
|
| 50 |
+
with Row():
|
| 51 |
+
get_extension = Button("Generate extension code")
|
| 52 |
+
|
| 53 |
+
with Row():
|
| 54 |
+
setup_code = Textbox(label="Compilation code:", lines=10,
|
| 55 |
+
buttons=["copy"], elem_classes=["python"])
|
| 56 |
+
usage_code = Textbox(label="Test compare code:", lines=10,
|
| 57 |
+
buttons=["copy"], elem_classes=["python"])
|
| 58 |
+
|
| 59 |
+
get_extension.click(optimize_function,
|
| 60 |
+
inputs=[python, module_name, platform, model],
|
| 61 |
+
outputs=[c_code, setup_code, usage_code])
|
| 62 |
+
|
| 63 |
+
# ######### BUILD AND TEST SECTIONS ######### #
|
| 64 |
+
if compile_stage:
|
| 65 |
+
with Row():
|
| 66 |
+
compile_ext = Button("Compile extension")
|
| 67 |
+
with Row():
|
| 68 |
+
c_ext_out = TextArea(label="C Extension result:", buttons=["copy"],
|
| 69 |
+
elem_classes=["c_ext"])
|
| 70 |
+
|
| 71 |
+
with Row():
|
| 72 |
+
test_run = Button("Test code")
|
| 73 |
+
with Row():
|
| 74 |
+
test_out = TextArea(label="Test result:", buttons=["copy"],
|
| 75 |
+
elem_classes=["python"])
|
| 76 |
+
|
| 77 |
+
compile_ext.click(compile_extension,
|
| 78 |
+
inputs=[c_code, setup_code, module_name],
|
| 79 |
+
outputs=[c_ext_out])
|
| 80 |
+
|
| 81 |
+
test_run.click(test_extension,
|
| 82 |
+
inputs=[usage_code],
|
| 83 |
+
outputs=[test_out])
|
| 84 |
+
|
| 85 |
+
return ui
|
src/optimizer.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Optimizer module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
from logging import getLogger
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
|
| 9 |
+
from .prompts import messages_for
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
_logger = getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Environment initialization.
|
| 16 |
+
load_dotenv(override=True)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Initialize client and set the default LLM model to use.
|
| 20 |
+
openai = OpenAI()
|
| 21 |
+
OPENAI_MODEL = "gpt-5"
|
| 22 |
+
_logger.info(f'INITIALIZED OPTIMIZER MODULE')
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Define Pydantic model class for GPT response parsing.
|
| 26 |
+
class _extension_codes(BaseModel):
|
| 27 |
+
c_code: str
|
| 28 |
+
setup: str
|
| 29 |
+
usage: str
|
| 30 |
+
|
| 31 |
+
def __str__(self):
|
| 32 |
+
"""Return a string representation of the optimization codes."""
|
| 33 |
+
return (f"C CODE:\n{self.c_code}\n"
|
| 34 |
+
f"---------------------------\n"
|
| 35 |
+
f"setup.py:\n{self.setup}\n"
|
| 36 |
+
f"---------------------------\n"
|
| 37 |
+
f"USAGE:\n{self.usage}")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Define optimization function using OpenAI's GPT model.
|
| 41 |
+
def optimize_gpt(python_code, module_name, platform, model=OPENAI_MODEL):
|
| 42 |
+
"""Generate an optimized C extension for Python."""
|
| 43 |
+
schema = _extension_codes.model_json_schema()
|
| 44 |
+
_logger.info('SENDING OPTIMIZATION REQUEST TO OPENAI... '
|
| 45 |
+
f'(MODEL: {model}, PLATFORM: {platform})')
|
| 46 |
+
response = openai.responses.parse(
|
| 47 |
+
model=model, text_format=_extension_codes,
|
| 48 |
+
input=messages_for(python_code, module_name, schema, platform)).output_parsed
|
| 49 |
+
_logger.info('RECEIVED OPTIMIZATION RESPONSE FROM OPENAI')
|
| 50 |
+
return response.c_code, response.setup, response.usage
|
src/prompts.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Prompts for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
# Define system message with instructions for generating the C extension code.
|
| 4 |
+
system_message = """
|
| 5 |
+
You are an assistant that reimplements Python code in high performance C extensions
|
| 6 |
+
for Python.
|
| 7 |
+
Your responses must always be a JSON with the following schema:
|
| 8 |
+
|
| 9 |
+
{schema}
|
| 10 |
+
|
| 11 |
+
Use comments sparingly and do not provide any explanation other than occasional comments.
|
| 12 |
+
|
| 13 |
+
The C extension for Python needs to produce an identical output in the fastest possible
|
| 14 |
+
time.
|
| 15 |
+
|
| 16 |
+
Make sure the C extension for Python code is correct and can be compiled with
|
| 17 |
+
'python setup.py build_ext' and used in Python.
|
| 18 |
+
|
| 19 |
+
The usage example must include a time measurement and a comparison with the original
|
| 20 |
+
Python code.
|
| 21 |
+
|
| 22 |
+
Do not include any additional text or explanation outside the JSON structure.
|
| 23 |
+
|
| 24 |
+
Make sure the JSON is correctly formatted.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Define user prompt template and function to fill it.
|
| 29 |
+
user_prompt = """
|
| 30 |
+
Reimplement this Python code as a C extension for Python with the fastest possible
|
| 31 |
+
implementation that produces identical output in the least time.
|
| 32 |
+
|
| 33 |
+
Respond only with C extension for Python code, do not explain your work other than
|
| 34 |
+
a few code comments.
|
| 35 |
+
|
| 36 |
+
The module name, used to import, must be "{module_name}", the generated C file will
|
| 37 |
+
be named "{module_name}.c".
|
| 38 |
+
|
| 39 |
+
Pay attention to number types to ensure no int overflows.
|
| 40 |
+
Remember to #include all necessary C packages such as iomanip or <python.h>
|
| 41 |
+
|
| 42 |
+
The target architecture is {platform}, take that in mind while generating the C
|
| 43 |
+
code, specially when choosing types to use, and use the appropriate compiler flags.
|
| 44 |
+
|
| 45 |
+
Make sure to use the Python C API correctly and manage memory properly to avoid
|
| 46 |
+
leaks or crashes.
|
| 47 |
+
|
| 48 |
+
Here is the Python code to reimplement:
|
| 49 |
+
|
| 50 |
+
{python_code}
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Define function to create the messages for the LLM.
|
| 55 |
+
def messages_for(python_code, module_name, schema, platform):
|
| 56 |
+
"""Create the messages given the Python code, module name, and platform."""
|
| 57 |
+
return [
|
| 58 |
+
{"role": "system", "content": system_message.format(schema=schema)},
|
| 59 |
+
{"role": "user", "content": user_prompt.format(python_code=python_code,
|
| 60 |
+
module_name=module_name,
|
| 61 |
+
platform=platform)}]
|
src/tester.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Testing module for the AI Python C Extensions Generator application."""
|
| 2 |
+
|
| 3 |
+
import io
|
| 4 |
+
import sys
|
| 5 |
+
from logging import getLogger
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
_logger = getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Define a function to write outputs to a file with a given filename.
|
| 12 |
+
def write_file(data, filename):
|
| 13 |
+
"""Write data to a file with the specified filename."""
|
| 14 |
+
with open(filename, "w") as file:
|
| 15 |
+
file.write(data)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Define a function to execute Python code and capture its output.
|
| 19 |
+
def execute_python(code):
|
| 20 |
+
"""Execute the given Python code and capture its output."""
|
| 21 |
+
try:
|
| 22 |
+
_logger.info('EXECUTING TEST CODE...')
|
| 23 |
+
# Redirect stdout to capture the output of the executed code.
|
| 24 |
+
output = io.StringIO()
|
| 25 |
+
sys.stdout = output
|
| 26 |
+
# Execute the provided code in a clean global namespace.
|
| 27 |
+
exec(code, {})
|
| 28 |
+
finally:
|
| 29 |
+
# Restore original stdout.
|
| 30 |
+
sys.stdout = sys.__stdout__
|
| 31 |
+
return output.getvalue()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Extension testing function.
|
| 35 |
+
def test_extension(usage_code):
|
| 36 |
+
"""Test the C extension executing the code and capture its output."""
|
| 37 |
+
try: # Write the code to file.
|
| 38 |
+
write_file(usage_code, "usage_example.py")
|
| 39 |
+
except Exception as ex:
|
| 40 |
+
return f"An error occurred while writing test file:\n{ex}"
|
| 41 |
+
try:
|
| 42 |
+
# Execute the code and capture the output.
|
| 43 |
+
output = execute_python(usage_code)
|
| 44 |
+
except Exception as ex:
|
| 45 |
+
return f"An error occurred while testing the extension:\n{ex}"
|
| 46 |
+
return output
|