Skip to content

felixoder/felix-detect-fix

Repository files navigation

Felix Detect & Fix - VS Code Extension

Felix Detect & Fix License

A powerful VS Code extension that detects and fixes code bugs using machine learning. This extension integrates with a bug detection model and a bug-fixing model hosted on Hugging Face, allowing developers to improve code quality efficiently.

Features ✨

  • πŸš€ Detect Bugs: Classifies code as "buggy" or "bug-free."
  • πŸ”§ Fix Bugs: Automatically suggests fixes for detected issues.
  • πŸ’‘ Manual Control: Users decide when to run the detection and fixing functions.
  • ⚑ Fast & Local Processing: Uses Hugging Face models locally, avoiding API calls.
  • πŸ’‘ Cross-Platform: people can use this for every os windows/linux/macos etc on vesion 0.0.2 onwards.

Installation πŸ› οΈ

  1. Download and install the extension from the VS Code Marketplace.
  2. Ensure you have Node.js and VS Code installed.
  3. Open VS Code and enable the extension.

Usage πŸš€

project structure
   |_ your_code.py
   |_ bug*detector*model [download from ```huggingface-cli download felixoder/bug_detector_model --local-dir ./bug_detector_model\n```]
   |_ bug*fixer_model [download from ```huggingface-cli download felixoder/bug_fixer_model --local-dir ./bug_fixer_model```]
   |_ run_model.py [see ## run_model.py]

run_model.py script:

pip install torch
pip install transformers
import sys

import torch
from transformers import (
    AutoModelForCausalLM,
    AutoModelForSequenceClassification,
    AutoTokenizer,
)

detector_name = "./bug_detector_model"
fixer_name = "./bug_fixer_model"

# Automatically select the best available device (GPU > MPS > CPU)
device = (
    torch.device("cuda")
    if torch.cuda.is_available()
    else torch.device("mps")
    if torch.backends.mps.is_available()
    else torch.device("cpu")
)

# Use FP16 if on GPU, else FP32
torch_dtype = torch.float16 if device.type == "cuda" else torch.float32

tokenizer = AutoTokenizer.from_pretrained(detector_name)
model = AutoModelForSequenceClassification.from_pretrained(
    detector_name, torch_dtype=torch_dtype
).to(device)

fixer_tokenizer = AutoTokenizer.from_pretrained(fixer_name)
fixer_model = AutoModelForCausalLM.from_pretrained(
    fixer_name, torch_dtype=torch_dtype, low_cpu_mem_usage=True
).to(device)


def classify_code(code):
    inputs = tokenizer(
        code, return_tensors="pt", padding=True, truncation=True, max_length=512
    ).to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    predicted_label = torch.argmax(outputs.logits, dim=1).item()
    return "bug-free" if predicted_label == 0 else "buggy"


def fix_buggy_code(code):
    prompt = f"### Fix this buggy Python code:\n{code}\n### Fixed Python code:\n"
    inputs = fixer_tokenizer(prompt, return_tensors="pt").to(device)

    with torch.no_grad():
        outputs = fixer_model.generate(
            **inputs, max_length=256, do_sample=False, num_return_sequences=1
        )

    fixed_code = fixer_tokenizer.decode(outputs[0], skip_special_tokens=True)
    return (
        fixed_code.split("### Fixed Python code:")[1].strip()
        if "### Fixed Python code:" in fixed_code
        else fixed_code
    )


if __name__ == "__main__":
    command = sys.argv[1]
    code = sys.argv[2]

    if command == "classify":
        print(classify_code(code))
    elif command == "fix":
        print(fix_buggy_code(code))

  1. Detect Bugs:

    • Open a python code file.
    • Run the command: Detect Bugs
    • The extension highlights buggy code sections.
  2. Use a build template:

    • Paste this in your terminal
     wget -O setup_and_run.sh https://raw.githubusercontent.com/felixoder/felix-detect-fix/master/setup_and_run.sh
        chmod +x setup_and_run.sh
        ./setup_and_run.sh
    
  3. Fix Bugs:

    • After detecting bugs, run Fix Bugs
    • The model suggests code fixes.
  4. NEW RELEASE 1.0.4 onwards

    • No need to install the model from wget I have made some changes so that we can install the model in a nice way.
    • you can run the model or try using CLI then it will detect that the model(detect || fixer) is available in model/ route. if it is it will use them otherwise it will install those and use them.

Requirements πŸ“¦

Contributing 🀝

  1. Fork the repo & create a new branch.
  2. Make your changes & commit.
  3. Open a Pull Request!

License πŸ“œ

This project is licensed under the MIT License.


Made with ❀️ by Debayan Ghosh.