File size: 4,263 Bytes
9e7eb94
af21194
 
 
 
9e7eb94
af21194
9e7eb94
 
 
 
 
af21194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
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:

1. **Username:** `user`
2. **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

1. Fork or duplicate this Space
2. Go to **Settings** β†’ **Repository secrets**
3. Add the following secrets:
   - `OPENAI_API_KEY` - Your OpenAI API key from https://platform.openai.com/api-keys
   - `ANTHROPIC_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

1. Clone the repository
2. Install dependencies:
   ```bash
   pip install -r requirements.txt
   ```
3. Set environment variables:
   ```bash
   export OPENAI_API_KEY="your-openai-key"
   export ANTHROPIC_API_KEY="your-anthropic-key"
   ```
4. Run the app:
   ```bash
   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:

1. **GPT-4o** (OpenAI)
   - Excellent at code generation
   - Good optimization strategies
   - Fast streaming responses

2. **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:
1. Fork the repository
2. Create a feature branch
3. 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

- [OpenAI Platform](https://platform.openai.com/)
- [Anthropic Console](https://console.anthropic.com/)
- [Gradio Documentation](https://www.gradio.app/docs/)
- [C++ Performance Optimization](https://en.cppreference.com/)

---

**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.