This project offers a user-friendly interface for converting Python code into optimized C++ code using multiple LLMs, including OpenAI, Gemini, and the Hugging Face model NTQAI/Nxcode-CQ-7B-orpo. The interface is built with Gradio, providing a simple and interactive UI. It supports efficient inference using CUDA and 4-bit quantization via the bitsandbytes library for faster performance on compatible GPUs.
The notebook does the following:
- Loads a pretrained large language model (
NTQAI/Nxcode-CQ-7B-orpo
) using Hugging Face Transformers. - Converts Python code into C++ through prompts and LLM inference.
- Extracts and displays the generated C++ code.
- Hosts a Gradio UI to allow users to input Python code and receive C++ output.
Install the required libraries with:
pip install openai google huggingface_hub google-genai gradio transformers torch bitsandbytes requests accelerate sentencepiece
Ensure you have a CUDA-compatible GPU for best performance.
Set your API keys in Google Colab using:
from google.colab import userdata
openai_api_key = userdata.get('OPENAI_API_KEY')
google_key = userdata.get('GOOGLE_API_KEY')
hf_token = userdata.get('HF_TOKEN')
- A Python sample function (estimating Ο) is prepared.
- Prompts are constructed to request the LLM to translate Python to optimized C++ for an M1 Mac.
- The
generate_code_NxCode
function sends this prompt to the LLM. - The LLM's output is decoded, and C++ code is extracted from markdown-style code blocks.
- A Gradio interface lets users submit their Python code and receive the C++ version.
Python Input:
def calculate(iterations, param1, param2):
...
Expected C++ Output:
#include <iostream>
...
After running the notebook, it launches a Gradio web interface where you can:
- Paste your Python code
- Click "Convert code"
- View the generated C++ in real-time
- Python
- Hugging Face Transformers
- BitsAndBytes for 4-bit quantization
- Gradio (UI)
- Google Colab (for secure key storage)
- Only supports basic Python-to-C++ logic for now.
- Intended for high-performance translation for M1 Mac systems.
- Output C++ is optimized but may require manual review for production use.