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.
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.
- 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.
- 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.
1. Clone the Repository
git clone https://github.com/your-username/code-smell-detector.git
cd code-smell-detector2. 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/activate3. Install Dependencies
Install the required packages using pip and the requirements.txt file:
pip install -r requirements.txt4. 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.pyThis 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.pyThis 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 separatelytest_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.
Dependencies
The project requires the following packages, which are listed in requirements.txt :
transformerstorchdatasetsnltkvirtualenvastsubprocessmath