Skip to content

swalehaparvin/Python-to-CPP-Converter

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸŒ€ Python-to-C++ Code Converter using LLMs

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.

🧠 Overview

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.

πŸ“¦ Dependencies

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.

πŸ” Required Tokens

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')

πŸš€ How It Works

  1. A Python sample function (estimating Ο€) is prepared.
  2. Prompts are constructed to request the LLM to translate Python to optimized C++ for an M1 Mac.
  3. The generate_code_NxCode function sends this prompt to the LLM.
  4. The LLM's output is decoded, and C++ code is extracted from markdown-style code blocks.
  5. A Gradio interface lets users submit their Python code and receive the C++ version.

πŸ§ͺ Sample Code Example

Python Input:

def calculate(iterations, param1, param2):
    ...

Expected C++ Output:

#include <iostream>
...

🌐 Web UI

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

πŸ› οΈ Tech Stack

  • Python
  • Hugging Face Transformers
  • BitsAndBytes for 4-bit quantization
  • Gradio (UI)
  • Google Colab (for secure key storage)

πŸ“Œ Notes

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 100.0%