Skip to content

Repository files navigation

Code Smell Detection with Fine-Tuned GPT-2

This project uses a fine-tuned GPT-2 model to detect code smells in Python code. The model is trained to analyze code snippets and suggest potential improvements or highlight issues.

Project Overview

The goal of this project is to create a tool that assists in detecting code smells in Python code. We fine-tuned the GPT-2 model on a custom dataset of code snippets, enabling the model to generate predictions based on code structure and content.

Features

  • Fine-tuned GPT-2 model for code smell detection.
  • Supports input Python code snippets and provides feedback on possible issues.
  • Pipeline-based inference for easy usage of the fine-tuned model.

Technologies Used

  • Python for building the scripts.
  • Hugging Face's Transformers for model fine-tuning and generation.
  • Datasets for loading and preparing the dataset. nltk for text processing.
  • Virtual Environment for isolated dependency management.

Setup Instructions

1. Clone the Repository

git clone https://github.com/your-username/code-smell-detector.git
cd code-smell-detector

2. Set Up Virtual Environment

Make sure you have virtualenv installed, then create and activate your virtual environment:

# Install virtualenv if you don't have it
pip install virtualenv

# Create a virtual environment
virtualenv .env

# Activate the virtual environment
# On Windows
.env\Scripts\activate
# On macOS/Linux
source .env/bin/activate

3. Install Dependencies

Install the required packages using pip and the requirements.txt file:

pip install -r requirements.txt

4. Download NLTK Resources

Download the NLTK resources that are used in the project:

import nltk
nltk.download('punkt')

5. Fine-Tune the Model

To fine-tune the model on your dataset, run:

python analyzer.py

This will load your dataset, fine-tune the GPT-2 model, and save the model to the code_quality_model directory.

6. Test the Model

To test the fine-tuned model, run:

python test_model.py

This script will run the model on predefined Python code snippets and display the generated predictions.

7. Structure of the Project

  • analyzer.py: Fine-tunes the GPT-2 model on the provided dataset for code smell detection.
  • fine_tune_llm.py: Code for tuning the model, written separately
  • test_model.py: Tests the fine-tuned model with sample Python code snippets.
  • code_quality_dataset.json: Sample dataset used for training.
  • code_quality_model/: Directory where the fine-tuned model is saved.
  • requirements.txt: List of required packages for the project.

Docker Setup

Docker Hub

Dependencies

The project requires the following packages, which are listed in requirements.txt :

  • transformers
  • torch
  • datasets
  • nltk
  • virtualenv
  • ast
  • subprocess
  • math

About

A fine-tuned GPT-2 model for detecting code smells in Python, this project analyzes code snippets to suggest improvements and highlight potential issues, enhancing overall code quality.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages