A powerful tool that converts Python code to high-performance C++ code using state-of-the-art AI models. This project leverages OpenAI's GPT, Anthropic's Claude, and HuggingFace's CodeQwen models to automatically translate Python algorithms into optimized C++ implementations.
- Multi-Model Support: Choose from GPT-4, Claude 3.5 Sonnet, or CodeQwen for code translation
- Performance Optimization: Generates highly optimized C++ code with appropriate compiler flags
- Cross-Platform: Supports Windows (Visual Studio), Linux (GCC/Clang), and macOS (Clang with Apple M1 optimizations)
- Interactive UI: Gradio-based web interface for easy code conversion and testing
- Real-time Comparison: Execute both Python and C++ versions to compare performance and verify correctness
- Streaming Output: Real-time code generation with streaming responses from AI models
- Sample Programs: Pre-built examples including pi calculation and maximum subarray algorithms
pip install openai anthropic google-generativeai python-dotenv gradio huggingface-hub transformers ipython jupyter- Python 3.7+
- C++ Compiler: One of the following:
- Windows: Visual Studio 2019/2022 Build Tools
- Linux: GCC (g++) or Clang++
- macOS: Xcode Command Line Tools (Clang++)
You'll need API keys for the AI models you want to use:
- OpenAI API key (for GPT models)
- Anthropic API key (for Claude models)
- HuggingFace token (for CodeQwen models)
Note: For CodeQwen functionality, you'll need to set up HuggingFace endpoints. The notebook includes placeholder URLs that need to be replaced with your actual endpoint URLs.
-
Clone the repository:
git clone https://github.com/Sameeh07/Code_Generator.git cd Code_Generator -
Install Python dependencies:
pip install openai anthropic google-generativeai python-dotenv gradio huggingface-hub transformers ipython jupyter
-
Set up environment variables: Create a
.envfile in the project root:OPENAI_API_KEY=your_openai_api_key_here ANTHROPIC_API_KEY=your_anthropic_api_key_here HF_TOKEN=your_huggingface_token_here
-
Configure HuggingFace endpoints (for CodeQwen): Update the endpoint URLs in the notebook:
CODE_QWEN_URL = "your_codeqwen_endpoint_url" CODE_GEMMA_URL = "your_codegemma_endpoint_url" # Optional
-
Install C++ compiler (platform-specific):
- Windows: Install Visual Studio Community or Build Tools
- Linux:
sudo apt install g++orsudo apt install clang - macOS:
xcode-select --install
-
Start Jupyter:
jupyter notebook
-
Open the notebook: Open
code gen.ipynbin your browser -
Run the cells to initialize the environment and start the Gradio interface
The Gradio interface provides:
- Python Code Input: Paste or type your Python code
- Model Selection: Choose between GPT, Claude, or CodeQwen
- Sample Programs: Quick access to pre-built examples
- Platform Detection: Automatic detection of your system and available compilers
- Real-time Conversion: Stream the C++ code generation process
- Execution: Run both Python and C++ versions to compare results
from your_module import optimize_gpt, optimize_claude, stream_code_qwen
# Python code to convert
python_code = """
def calculate_fibonacci(n):
if n <= 1:
return n
return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)
print(calculate_fibonacci(30))
"""
# Convert using GPT
optimize_gpt(python_code)
# Convert using Claude
optimize_claude(python_code)
# The optimized C++ code will be saved to 'optimized.cpp'High-performance pi calculation using the Leibniz formula:
def calculate(iterations, param1, param2):
result = 1.0
for i in range(1, iterations+1):
j = i * param1 - param2
result -= (1/j)
j = i * param1 + param2
result += (1/j)
return result
result = calculate(100_000_000, 4, 1) * 4Complex algorithm with random number generation:
def max_subarray_sum(n, seed, min_val, max_val):
# Uses Linear Congruential Generator for reproducible random numbers
# Implements Kadane's algorithm for maximum subarray sum
# Processes multiple iterations for statistical analysis- OpenAI GPT-4: Latest GPT-4 model with excellent code generation capabilities
- Anthropic Claude 3.5 Sonnet: Advanced reasoning and code optimization
- CodeQwen 1.5-7B: Specialized code generation model from Alibaba
- Streaming: Real-time code generation with immediate feedback
- Memory Management: Optimized for large-scale computations
- Type Safety: Automatic handling of integer overflow prevention
- Platform Detection: Automatic compiler and architecture detection
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source.
- OpenAI for GPT models
- Anthropic for Claude models
- HuggingFace for CodeQwen and model hosting
- Gradio for the interactive interface