Convert Python → high-performance C++ or add PEP-257 docstrings/comments to Python, with a clean streaming UI built in Gradio and the OpenAI API.
⚠️ Generated code is unaudited. Don’t run untrusted code.
-
Two modes
- Convertor: re-implements Python in optimized C++17+ (code block only)
- Documentation: injects docstrings + concise comments into Python without changing behavior
-
Streaming UX: tokens appear live in the app while the model writes
-
Reliable parsing: robust fenced-code extraction (
cpp /python / plain) -
Artifacts: saves outputs to
optimized.cpporDocumented.py -
(Optional) Native run: one-click Compile & Run (C++) for local benchmarking
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # then put your key insideEdit .env:
OPENAI_API_KEY=sk-your-keypython app.pyOpen the printed local URL (e.g., http://127.0.0.1:7860).
-
Paste Python code in the text box.
-
Choose Mode:
- Convertor → “Python → C++”
- Documentation → “Docstrings + comments”
-
Click Run. Output streams into the right panel.
-
When generation ends, the app writes one of:
optimized.cpp(Convertor)Documented.py(Documentation)
-
(Optional) Click Compile & Run (C++) to build & run
optimized.cpplocally.
The compile button uses
clang++on macOS andg++on Linux:-O3 -std=c++17 -march=native -o optimized optimized.cpp
-
Prompts (
prompts.py)system_message_for("Convertor")asks for pure C++ code in a ```cpp fenced block, with correct headers and attention to integer width/overflow.system_message_for("Documentation")asks for pure Python code in a ```python fenced block, adding PEP-257 docstrings + minimal inline comments, no behavior changes.
-
Streaming (
app.py)- Uses
client.chat.completions.create(..., stream=True, temperature=0)for deterministic generation. - Buffers fragments and streams to the UI; writes the final cleaned code to disk.
- Uses
-
Fence parsing (
utils.py)extract_code_block()finds the first matching fenced block, preferring a requested language when present.write_output()writes the cleaned code tooptimized.cpporDocumented.py.compile_and_run_cpp()compiles + runs locally and returns stdout.
Input (Python):
print("hello")Output (C++, saved to optimized.cpp):
#include <iostream>
int main(){ std::cout << "hello\n"; }Input:
def add(a,b):
return a+bOutput (saved to Documented.py):
def add(a: int, b: int) -> int:
"""Return the sum of two integers.
Args:
a (int): First addend.
b (int): Second addend.
Returns:
int: a + b.
"""
return a + b-
Blank output or backticks in files The extractor in
utils.pyremoves code fences. If a model replies without fences, the raw text is saved as-is. -
“No optimized.cpp found.” on compile Run Convertor first, then click Compile & Run.
-
Compiler not found Install a C++ compiler:
- macOS:
xcode-select --install(givesclang++) - Linux:
sudo apt install g++
- macOS:
-
Safety Don’t compile/run code you don’t trust. Review the generated C++ first. Or run on a different compiler.
The app uses gpt-4o-mini by default (low cost). You can change the model via OPENAI_MODEL in app.py. API usage incurs costs—monitor your key.
You can also substitute with any open source model using ollama if needed, however, accuracy and performance of the application will differ.
MIT (suggested). Add a LICENSE file if you plan to open-source.
Built with Gradio for the UI and OpenAI for generation.