Skip to content

About

AI-powered Python-to-C++ transpiler with Pybind11 generation, self-healing compilation, and automated performance benchmarking.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PolyCode AI πŸš€

Autonomous Python-to-C++ Transpiler, Pybind11 Extension Generator & Self-Healing Benchmark Pipeline

PolyCode AI is an end-to-end AI engineering tool that transpiles bottleneck Python functions into optimized C++17 code and native Pybind11 C extensions. It automatically compiles binaries via subprocesses, self-heals compilation errors using terminal stderr feedback, and benchmarks execution speedup side-by-side.


πŸ“‹ Table of Contents


πŸ’‘ Overview

Python is the leading language for rapid prototyping and AI development, but its interpreted nature and Global Interpreter Lock (GIL) make it slow for CPU-heavy mathematical computations. PolyCode AI bridges this gap by automatically converting Python bottlenecks into compiled, bare-metal C++ executables and native Pybind11 Python C-extensions (.pyd / .so).

The 3 Unified Engines

PolyCode AI combines three core software capabilities into a single seamless pipeline:

  1. Multi-Language LLM Polyglot Transpiler: Prompts frontier models via LiteLLM to convert Python algorithms into C++17 and Pybind11 module formats.
  2. Native Python Extension Generator: Invokes local C++ compilers (g++) via subprocesses to produce compiled shared libraries on the fly.
  3. Self-Healing LLM Code Optimization Agent: Captures compiler output (stderr) upon compilation failures, automatically sending stack traces back to the LLM to patch and recompile code without human intervention.

✨ Key Features

  • Multi-Target Transpilation: Supports C++17 standalone executables and Pybind11 dynamic modules.
  • Autonomous Error Recovery: Captures raw g++ compilation errors and iterates with reflection loops until valid binaries are built.
  • Automated Toolchain Integration: Resolves Pybind11 include headers, Python C API libraries, and g++ flags automatically.
  • Side-by-Side Micro-Benchmarking: Executes and measures execution time for Pure Python, Compiled C++ Executables, and Native Pybind11 Extensions to compute exact speedup factors.
  • Interactive Dashboard: Built with Gradio for clean side-by-side code visualizers, execution time metrics, and real-time build logs.

πŸ—οΈ Architecture & Workflow

                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚          User Input (Python)            β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. LLM Transpilation Engine                                                      β”‚
β”‚ β€’ Converts Python code to C++17 & Pybind11 native modules via LiteLLM            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 2. Native Extension Compiler                                                     β”‚
β”‚ β€’ Spawns g++ via subprocess                                                      β”‚
β”‚ β€’ Links Pybind11 header files and produces dynamic library (.pyd / .so)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                                    Compilation?
                                    β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
                                  FAILED      PASSED
                                    β”‚           β”‚
                                    β–Ό           β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚ 3. Self-Healing Agent                  β”‚      β”‚
β”‚ β€’ Captures g++ stderr compiler error   β”‚      β”‚
β”‚ β€’ Refines prompt with error context    β”‚      β”‚
β”‚ β€’ Re-prompts LLM for corrected code    β”‚      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
                   └───────────────────┐        β”‚
                                       β–Ό        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 4. Execution & Benchmarking Suite                                                β”‚
β”‚ β€’ Runs Pure Python, C++ Executable, and Pybind11 Extension                       β”‚
β”‚ β€’ Measures execution timing and calculates performance speedup                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


🎬 Demo

This project demonstrates an end-to-end code optimization workflow:

  • Converts Python code into C++17 and Pybind11 targets using LLMs
  • Compiles generated code with g++
  • Uses compiler stderr to drive an automated self-healing loop
  • Benchmarks Python, C++, and Pybind11 execution
  • Presents code, build logs, and performance results in a Gradio dashboard

βš™οΈ System Requirements

  • Operating System: Windows 11 / Linux / macOS
  • Python Environment: Python 3.12+ (managed via uv)
  • C++ Compiler: g++ with C++17 support (MinGW-w64 on Windows, GCC, or Clang)
  • API Credentials: API key for OpenAI, Gemini, Groq, or OpenRouter (managed via LiteLLM)

πŸš€ Installation & Quickstart

1. Clone Repository & Navigate to Folder

# Clone the repository
git clone https://github.com/FMangiwa/polycode-ai.git
cd polycode-ai

2. Set Up Virtual Environment

Create and activate a isolated environment using uv:

# Create virtual environment
uv venv

# Install dependencies from requirements.txt
uv pip install -r requirements.txt

3. Configure API Credentials

Create a .env file in 4\_polycode\_ai/:

OPENAI\_API\_KEY=your\_openai\_api\_key
GROQ\_API\_KEY=your\_groq\_api\_key
# Additional keys supported by LiteLLM (GEMINI\_API\_KEY, OPENROUTER\_API\_KEY, etc.)

4. Run Application

uv run python app.py

Open http://127.0.0.1:7860 in your web browser.


πŸ“ Project Directory Structure

4\_polycode\_ai/
β”œβ”€β”€ .env                 # Environment variables & API credentials (API keys)
β”œβ”€β”€ app.py               # Main Gradio application launcher & entrypoint
β”œβ”€β”€ config.py            # Global constants, paths, and build environment settings
β”œβ”€β”€ pytest.ini           # Pytest configuration and warning suppression rules
β”œβ”€β”€ Readme.md            # Project documentation & architecture overview
β”œβ”€β”€ requirements.txt     # Python project dependencies
β”œβ”€β”€ engine/
β”‚   β”œβ”€β”€ \_\_init\_\_.py          # Engine package initializer
β”‚   β”œβ”€β”€ compiler.py          # Low-level g++ compiler & subprocess management
β”‚   β”œβ”€β”€ llm.py               # LiteLLM client wrapper & C++/Pybind11 system prompts
β”‚   β”œβ”€β”€ orchestrator.py      # Core pipeline coordinator & benchmark runner
β”‚   β”œβ”€β”€ pybind\_loader.py     # Native C++ extension builder & module importer
β”‚   └── self\_healing.py      # Iterative LLM reflection & error-correction loops
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ test\_app.py          # Gradio API integration tests
β”‚   β”œβ”€β”€ test\_config.py       # Configuration assertion tests
β”‚   └── test\_engine.py       # Code block extraction & LLM parser tests
└── ui/
    β”œβ”€β”€ \_\_init\_\_.py          # UI package initializer
    └── dashboard.py         # Gradio dashboard interface layout & event handlers


πŸ“– Usage Guide

  1. Paste Code: Enter a CPU-bound Python function into the PolyCode AI dashboard.
  2. Run Pipeline: Click Transpile & Benchmark.
  3. Monitor Build: View compilation terminal output in real time. If g++ yields an error, watch the self-healing loop patch the code automatically.
  4. Select Target: Choose between C++ Standalone Executable or Pybind11 Native Extension.
  5. Analyze Speedup: Compare execution runtime and performance gain metrics side-by-side.

πŸ§ͺ Running Tests

PolyCode AI uses pytest for unit and integration testing. Run tests via uv:

uv run pytest -v

Expected Test Output

tests/test\_app.py::test\_gradio\_client\_predict\_integration PASSED        [ 25%]
tests/test\_config.py::test\_config\_constants PASSED                      [ 50%]
tests/test\_engine.py::test\_engine\_response PASSED                       [ 75%]
tests/test\_engine.py::test\_extract\_code\_block PASSED                    [100%]

============================== 4 passed in 9.27s ==============================


πŸ› οΈ Troubleshooting & Configuration

Compiler Path Issues

If g++ is not recognized, ensure MinGW-w64 or GCC is added to your system PATH:

# Verify g++ availability
g++ --version

Pybind11 Include Warnings

When compiling Pybind11 modules manually, ensure header locations match your active environment:

python -c "import pybind11; print(pybind11.get\_include())"

License

See LICENSE.


About

AI-powered Python-to-C++ transpiler with Pybind11 generation, self-healing compilation, and automated performance benchmarking.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages