File size: 3,542 Bytes
be3b034 15d7982 be3b034 38b27cd be3b034 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | ---
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. |