Skip to content

BenyaminMahdavifar/Python2CPP-Agent

Repository files navigation

πŸš€ Python2CPP-Agent

An LLM-powered developer agent that translates Python code into standard, optimized C++17, compiles it with g++ -O3, runs both versions side-by-side, and generates a detailed performance and correctness benchmark report.

Python Version C++ Standard Gradio License


🎯 Core Features

  • Robust Code Extraction: Extracts raw Python scripts even when wrapped inside markdown code blocks.
  • LLM-Powered Translation: Translates complete logic structures (variables, arrays, functions, loops) to idiomatic, standard C++17.
  • Auto-Compilation: Uses g++ -O3 -std=c++17 to compile translated source files into static executables.
  • Performance Evaluation: Runs both implementations concurrently with customized timeout caps and measures runtime using high-precision timers.
  • Correctness Normalization: Normalizes terminal outputs (collapsing spaces, ignoring execution duration outputs) to verify algorithmic correctness without false alarms.
  • Interactive Workbench: Comes with both a rich command-line tool (cli.py) and an elegant Gradio browser UI (app.py).

πŸ“‚ Project Directory Structure

Python2CPP-Agent/
β”œβ”€β”€ README.md               # Detailed introduction and user manual
β”œβ”€β”€ LICENSE                 # MIT License
β”œβ”€β”€ CONTRIBUTING.md         # Open-source contributions guidelines
β”œβ”€β”€ CODE_OF_CONDUCT.md      # Community behavioral expectations
β”œβ”€β”€ requirements.txt        # PIP dependencies manifest
β”œβ”€β”€ pyproject.toml          # Packaging metadata and CLI commands
β”œβ”€β”€ .gitignore              # Ignored cache/output files config
β”œβ”€β”€ app.py                  # Gradio Web Workbench app
β”œβ”€β”€ cli.py                  # CLI command line application
β”œβ”€β”€ agent/                  # Core Modular Package
β”‚   β”œβ”€β”€ __init__.py         # Package initializer
β”‚   β”œβ”€β”€ translator.py       # OpenAI-compatible translator client
β”‚   β”œβ”€β”€ compiler.py         # Subprocess compiler manager
β”‚   β”œβ”€β”€ benchmark.py        # Normalizer and speedup metric computer
β”‚   β”œβ”€β”€ executor.py         # Safe, timed python/C++ execution wrapper
β”‚   β”œβ”€β”€ report.py           # Markdown/JSON formatting engine
β”‚   β”œβ”€β”€ utils.py            # Text parsing and file helpers
β”‚   └── prompts.py          # Translation prompts and LLM constraints
β”œβ”€β”€ examples/               # Demonstration Scripts
β”‚   β”œβ”€β”€ fibonacci.py        # Iterative Fibonacci checks
β”‚   β”œβ”€β”€ matrix.py           # Dense 3D matrix multiplication
β”‚   β”œβ”€β”€ sorting.py          # Vector Bubble Sort swaps
β”‚   └── prime.py            # Prime number check loops
β”œβ”€β”€ tests/                  # Lightweight Unit Tests
β”‚   β”œβ”€β”€ test_extract.py     # Code extraction parser test
β”‚   β”œβ”€β”€ test_compare.py     # Whitespace and timing filters test
β”‚   β”œβ”€β”€ test_executor.py    # Process execution constraints test
β”‚   └── test_report.py      # Markdown formatting validation test
β”œβ”€β”€ docs/                   # Architectural Guides
β”‚   β”œβ”€β”€ architecture.md     # Pipeline design overview
β”‚   β”œβ”€β”€ benchmark.md        # Correctness and performance details
β”‚   └── api.md              # Library API specifications
└── benchmark/              # Past Run Logs
    β”œβ”€β”€ results.json        # Performance history database
    └── report.md           # Pre-compiled results summary

βš™οΈ Quick Start

1. Prerequisites

  • Python: 3.10 or newer.
  • C++ Compiler: A C++17 compatible g++ compiler on your system path.
    • Linux (Ubuntu/Debian):
      sudo apt install g++
    • macOS: Install Xcode Command Line Tools by running:
      xcode-select --install
    • Windows: Install MinGW-w64 (e.g., via MSYS2 or WinLibs) or Visual Studio Build Tools.

2. Installation

Clone this repository and install dependencies:

git clone https://github.com/BenyaminMahdavifar/Python2CPP-Agent.git
cd Python2CPP-Agent
pip install -r requirements.txt

3. API Environment Variables

Set up one of the following environment variables depending on your chosen API provider:

Using Google Gemini API (Recommended & Free options):

export GEMINI_API_KEY="your-gemini-key"

Using OpenRouter:

export OR_API_KEY="your-openrouter-key"

Using OpenAI:

export OPENAI_API_KEY="your-openai-key"

πŸ› οΈ Usage

Command Line Interface (CLI)

Run translation and performance evaluations directly from your terminal:

# Run on an existing file
python cli.py -f examples/fibonacci.py

# Run on a raw code string
python cli.py -c "print(sum(i for i in range(100000)))"

# Change output directory and save as JSON
python cli.py -f examples/prime.py -o custom_workspace/ --json

Gradio Web Interface

Launch a local visual workspace directly in your web browser:

python app.py

Open http://localhost:3000 in your browser to start.


πŸ§ͺ Testing

We use pytest for unit testing. Install the dev dependencies and run tests:

pip install -e .[dev]
pytest

πŸ‘₯ Author

Benyamin Mahdavifar


πŸ“„ License

This project is licensed under the terms of the MIT License. See LICENSE for details.

About

An AI-powered agent that translates Python code into C++17, compiles it, executes both versions, and benchmarks performance.

Topics

Resources

License

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors