Spaces:
Runtime error
title: Python to C++ Code Optimizer
emoji: π
colorFrom: purple
colorTo: blue
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: mit
π Python to C++ Code Optimizer
An AI-powered tool that converts Python code to high-performance C++ using GPT-4o and Claude-3.5-Sonnet.
π― What It Does
This application takes Python code as input and uses frontier AI models to automatically convert it to optimized C++ code. It then allows you to:
- Compare the generated C++ code side-by-side with the original Python
- Execute both versions and compare performance
- See real-world speedups (typically 10-100x faster)
π Password Protection
This Space is password-protected. To access:
- Username:
user - Password: Contact the Space owner for the password
The password helps limit access while keeping the Space publicly discoverable.
β οΈ Security Warning
IMPORTANT: This application executes arbitrary code (both Python and C++).
- Only run code from trusted sources
- Malicious code can harm the system
- Use at your own risk
- Not recommended for production use without proper sandboxing
π§ Setup Instructions
For Hugging Face Spaces
- Fork or duplicate this Space
- Go to Settings β Repository secrets
- Add the following secrets:
OPENAI_API_KEY- Your OpenAI API key from https://platform.openai.com/api-keysANTHROPIC_API_KEY- Your Anthropic API key from https://console.anthropic.com/APP_PASSWORD- Your chosen password for accessing the app (e.g.,mySecurePass123)
For Local Development
- Clone the repository
- Install dependencies:
pip install -r requirements.txt - Set environment variables:
export OPENAI_API_KEY="your-openai-key" export ANTHROPIC_API_KEY="your-anthropic-key" - Run the app:
python app.py
For C++ Compilation
The app requires a C++ compiler:
- Linux:
g++(usually pre-installed) - macOS: Install Xcode Command Line Tools
- Windows: Install MinGW or use WSL
The default compilation command uses g++. You may need to adjust the compiler flags in app.py for your platform.
π Educational Use
This project is designed for educational purposes to demonstrate:
- AI-powered code generation and optimization
- Performance differences between Python and C++
- Real-time streaming from AI models
- Interactive code execution environments
π Example Performance
Pi Calculation (100M iterations):
- Python: ~6.3 seconds
- C++ (optimized): ~0.6 seconds
- Speedup: ~10x
Maximum Subarray (10K elements, 20 runs):
- Python: ~45 seconds
- C++ (optimized): ~0.4 seconds
- Speedup: ~110x
π‘οΈ Safety Features
- Execution timeouts (30 seconds)
- Error handling for compilation failures
- Clear security warnings in UI
- Sandboxed execution recommended for production
π Model Options
Choose between two frontier models:
GPT-4o (OpenAI)
- Excellent at code generation
- Good optimization strategies
- Fast streaming responses
Claude-3.5-Sonnet (Anthropic)
- Strong code understanding
- Detailed optimization
- High-quality output
π° Cost Considerations
Both models are paid APIs:
- GPT-4o: ~$5 per million input tokens
- Claude-3.5-Sonnet: ~$3 per million input tokens
For ultra-low cost, modify the code to use:
gpt-4o-mini(20x cheaper)claude-3-haiku(15x cheaper)
π€ Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
π License
MIT License - feel free to use and modify as needed.
β‘ Credits
Created as part of an LLM Engineering course demonstrating practical applications of frontier AI models.
π Resources
Disclaimer: This tool is for educational and research purposes. Always review generated code before using in production. The developers assume no liability for damages caused by code execution.