| --- |
| sdk: streamlit |
| sdk_version: 1.60.0 |
| --- |
| # Code Explainer & Bug Fixer (NLP Project) |
|
|
| An NLP pipeline that takes a code snippet and: |
| 1. **Explains** what it does, in plain English |
| 2. **Detects** whether it likely has a bug |
| 3. **Fixes** it if a bug is found |
|
|
| ## Why this architecture |
|
|
| | Sub-task | Model type used | Why | |
| |---|---|---| |
| | Explain code | Pretrained CodeT5 (transformer, encoder-decoder) | Already fine-tuned for codeβtext summarization. No training needed. | |
| | Detect/fix bugs | Fine-tuned CodeT5 (transformer, seq2seq) | Bug fixing = "translate" buggy code into fixed code. Long-range attention matters (a bug on line 1 can depend on a declaration on line 40). | |
| | Baseline comparison | RNN / GRU / LSTM (from scratch, PyTorch) | Included so you can show, empirically, *why* transformers beat recurrent models on this task β good material for a project report. | |
|
|
| **Key point for your report:** RNN/LSTM/GRU process tokens sequentially and compress everything into a fixed-size hidden state, so they tend to "forget" things from early in a long function by the time they reach the end. Transformers use self-attention, so every token can directly attend to every other token regardless of distance β this matters a lot for code, where dependencies (variable scope, matching brackets, function signatures) are often far apart. |
|
|
| ## Files |
|
|
| - `app.py` β main pipeline: load pretrained models, explain + detect + fix code. **Runs out of the box.** |
| - `train.py` β fine-tunes CodeT5 on the CodeXGLUE code-refinement dataset (buggy β fixed code pairs). Run this to get a real bug-fixing model instead of the zero-shot fallback in `app.py`. |
| - `lstm_baseline.py` β self-contained RNN/GRU/LSTM classifier for bug detection, used purely as a comparison baseline. |
| - `requirements.txt` β dependencies. |
|
|
| ## How to run |
|
|
| Recommended: use **Google Colab** (free GPU) since fine-tuning on CPU is very slow. |
|
|
| ```bash |
| pip install -r requirements.txt |
| python app.py # runs the pretrained pipeline immediately |
| python lstm_baseline.py # trains and compares RNN vs GRU vs LSTM (toy data) |
| python train.py # fine-tunes CodeT5 on real bug-fix data (needs GPU, ~1-2 hrs) |
| ``` |
|
|
| After `train.py` finishes, edit `app.py`: |
| ```python |
| FIX_MODEL_NAME = "./checkpoints/codet5-bugfix-finetuned" |
| ``` |
| to use your fine-tuned model instead of the zero-shot base model. |
|
|
| ## Extending this into a fuller project |
|
|
| 1. **Better bug detection**: replace the crude diff-based heuristic in `detect_and_fix()` with a proper classifier β fine-tune CodeBERT on the CodeXGLUE **defect-detection** task (binary: buggy/clean) for a real accuracy number. |
| 2. **UI**: wrap `analyze_code()` in a simple Streamlit or Gradio app so you can demo it live β takes ~20 lines. |
| 3. **Multi-language support**: `codet5-base-multi-sum` already handles Python, Java, JS, PHP, Ruby, Go for the explanation step. |
| 4. **Evaluation metrics**: for explanations, report BLEU/ROUGE against reference docstrings; for bug-fixing, report exact-match accuracy and CodeBLEU (standard in this literature). |
|
|
| ## Datasets you'll want to know about (for citing in your report) |
|
|
| - **CodeXGLUE** (Microsoft) β umbrella benchmark with `code-refinement` (bug fixing) and `defect-detection` (bug classification) tasks. |
| - **Bugs2Fix** β Python bug-fix pairs, smaller and easier to iterate on than CodeXGLUE's Java set. |
| - **CodeSearchNet** β large corpus of (code, docstring) pairs, useful if you want to fine-tune your own explainer instead of using the pretrained one. |