Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,446 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
# CodeGen: Hybrid RAG Pipeline
|
| 13 |
-
|
| 14 |
-
[](https://www.python.org/)
|
| 15 |
-
[](https://pytorch.org/)
|
| 16 |
-
[](https://opensource.org/licenses/MIT)
|
| 17 |
-
|
| 18 |
-
A capstone project (eMasters CS: AI & ML, Group 2) that fine-tunes a small open-source code language model — **Qwen2.5-Coder-0.5B-Instruct** — with **LoRA**, builds a **hybrid semantic + AST/structural retrieval (RAG)** layer on top of it, and evaluates the result against the un-adapted base model and an independently invoked LLM baseline, across text-to-SQL, documentation-generation, program-synthesis, and commit-message-generation tasks.
|
| 19 |
-
|
| 20 |
-
---
|
| 21 |
-
|
| 22 |
-
## Table of Contents
|
| 23 |
-
|
| 24 |
-
- [Executive Summary](#-executive-summary)
|
| 25 |
-
- [Key Capabilities](#-key-capabilities)
|
| 26 |
-
- [System Architecture](#-system-architecture)
|
| 27 |
-
- [Pipeline Stages](#-pipeline-stages)
|
| 28 |
-
- [Benchmark Results](#-benchmark-results)
|
| 29 |
-
- [Dataset](#-dataset)
|
| 30 |
-
- [Installation](#-installation)
|
| 31 |
-
- [Quick Start](#-quick-start)
|
| 32 |
-
- [Repository Layout](#-repository-layout)
|
| 33 |
-
- [Configuration](#-configuration)
|
| 34 |
-
- [Requirements](#-requirements)
|
| 35 |
-
- [License](#-license)
|
| 36 |
-
|
| 37 |
-
---
|
| 38 |
-
|
| 39 |
-
## Executive Summary
|
| 40 |
-
|
| 41 |
-
CodeGen combines **Retrieval-Augmented Generation (RAG)** and **parameter-efficient LoRA fine-tuning** on top of a compact instruction-tuned code model to support three developer-facing capabilities: natural-language-to-SQL translation, automatic source-code documentation generation, and program/commit-message generation. The primary evaluation dataset is the **Spider** cross-domain text-to-SQL benchmark (1,034 dev-split examples), supplemented by small hand-curated example sets for documentation, program-synthesis, and commit-message generation.
|
| 42 |
-
|
| 43 |
-
Retrieval embeddings are produced by the small code LM itself (no second embedding model needs to be downloaded), and are combined with an AST/structural fingerprint index so retrieval can be reranked on structure, not just vector similarity.
|
| 44 |
-
|
| 45 |
-
---
|
| 46 |
-
|
| 47 |
-
## Key Capabilities
|
| 48 |
-
|
| 49 |
-
| Capability | Description |
|
| 50 |
-
|---|---|
|
| 51 |
-
| **Multi-Task Generation** | Program synthesis, docstring generation, Text-to-SQL translation, commit-message synthesis |
|
| 52 |
-
| **Code-LM Self-Embeddings** | Retrieval embeddings mean-pooled from the base model's own hidden states — no separate embedding model required (an external `BAAI/bge-small-en-v1.5` path is also available and config-switchable) |
|
| 53 |
-
| **Hybrid Retrieval** | FAISS (`IndexFlatIP`) dense semantic search fused with an AST/structural fingerprint reranker (α = 0.7 dense / 0.3 structural) |
|
| 54 |
-
| **LoRA Fine-Tuning** | PEFT LoRA adapter (r=16, α=32) on the attention projections, with a custom training loop (gradient checkpointing, prompt-token label masking, OOM-safe stepping) |
|
| 55 |
-
| **Small-LM vs. LLM Harness** | Pluggable large-LLM baseline generator supporting local, OpenAI-compatible, and Gemini backends |
|
| 56 |
-
| **Comprehensive Evaluation** | BLEU, ROUGE-1/L, token-F1, CodeBERTScore, SQL Exact Match, SQL Execution Accuracy, RAG gain-over-baseline, top-K sweep |
|
| 57 |
-
|
| 58 |
-
---
|
| 59 |
-
|
| 60 |
-
## System Architecture
|
| 61 |
-
|
| 62 |
-
```
|
| 63 |
-
┌─────────────────────────────────────────────────────────────────┐
|
| 64 |
-
│ 1. Configuration & Data │
|
| 65 |
-
│ • Load config.yaml (paths, model, LoRA, retrieval, eval) │
|
| 66 |
-
│ • Convert & normalize Spider (dev.json + tables.json + SQLite) │
|
| 67 |
-
│ • Build retrieval corpus (1,034 question/SQL pairs) │
|
| 68 |
-
└──────────────────────────┬──────────────────────────────────────┘
|
| 69 |
-
│
|
| 70 |
-
┌──────────────────────────▼──────────────────────────────────────┐
|
| 71 |
-
│ 2. Model Loading & LoRA Fine-Tuning │
|
| 72 |
-
│ • Load Qwen2.5-Coder-0.5B-Instruct (tokenizer + base weights) │
|
| 73 |
-
│ • Attach LoRA adapter, train on illustrative task examples │
|
| 74 |
-
│ • Save / reload adapter checkpoint │
|
| 75 |
-
└──────────────────────────┬──────────────────────────────────────┘
|
| 76 |
-
│
|
| 77 |
-
┌──────────────────────────▼──────────────────────────────────────┐
|
| 78 |
-
│ 3. Generation Tasks │
|
| 79 |
-
│ • Text-to-SQL • Documentation • Program Synthesis │
|
| 80 |
-
│ • Commit Messages, evaluated for base model vs. LoRA model │
|
| 81 |
-
└──────────────────────────┬──────────────────────────────────────┘
|
| 82 |
-
│
|
| 83 |
-
┌──────────────────────────▼──────────────────────────────────────┐
|
| 84 |
-
│ 4. Hybrid Indexing & RAG │
|
| 85 |
-
│ • CodeLMEmbedder → FAISS semantic index (IndexFlatIP) │
|
| 86 |
-
│ • ASTIndexer → structural fingerprints (Python AST / SQL regex)│
|
| 87 |
-
│ • HybridRetriever → dense + structural blended top-K search │
|
| 88 |
-
│ • RAGPipeline → context injection + generation │
|
| 89 |
-
└──────────────────────────┬──────────────────────────────────────┘
|
| 90 |
-
│
|
| 91 |
-
┌──────────────────────────▼──────────────────────────────────────┐
|
| 92 |
-
│ 5. Evaluation & Comparison │
|
| 93 |
-
│ • Base vs. LoRA vs. RAG vs. LLM-baseline (ModelComparator) │
|
| 94 |
-
│ • RAG gain-over-baseline & top-K sweep (RAGEvaluator) │
|
| 95 |
-
│ • Charts (matplotlib) + JSON metrics persisted to output/ │
|
| 96 |
-
└─────────────────────────────────────────────────────────────────┘
|
| 97 |
-
```
|
| 98 |
-
|
| 99 |
-
---
|
| 100 |
-
|
| 101 |
-
## Pipeline Stages
|
| 102 |
-
|
| 103 |
-
The pipeline is run through a set of Jupyter notebooks rather than a single CLI entry point:
|
| 104 |
-
|
| 105 |
-
| Notebook | Stage |
|
| 106 |
-
|---|---|
|
| 107 |
-
| `baseline_model.ipynb` | Loads the **un-adapted base model**, runs text-to-SQL and documentation generation, saves metrics to `outputs/generated/baseline_model_results.json` |
|
| 108 |
-
| `finetuned_model.ipynb` | Loads/trains the **LoRA adapter**, re-runs the same two tasks, saves metrics to `outputs/generated/finetuned_model_results.json` |
|
| 109 |
-
| `spider_eval.ipynb` | Spider-specific SQL execution evaluation |
|
| 110 |
-
| `eval.ipynb` | RAG-gain measurement, top-K sweep, small-LM-vs-LLM comparison |
|
| 111 |
-
| `main.ipynb` | Complete End-to-End pipeline |
|
| 112 |
-
|
| 113 |
-
Supporting production code lives in `src/` (see [Repository Layout](#-repository-layout)) and is imported by the notebooks — it is not currently exposed as a standalone CLI script.
|
| 114 |
-
|
| 115 |
-
### Task-Specific Generators (`src/generators/`)
|
| 116 |
-
|
| 117 |
-
| Task | Module | Benchmark used | Primary Metrics |
|
| 118 |
-
|---|---|---|---|
|
| 119 |
-
| **Text-to-SQL** | `text_to_sql.py`, `sql_generator.py` | Spider (100-sample subset of the 1,034-question dev split) | Exact Match, Execution Accuracy, BLEU, CodeBERTScore |
|
| 120 |
-
| **Documentation Generation** | `doc_gen.py` | 3 hand-authored illustrative examples | BLEU, ROUGE, CodeBERTScore, F1 |
|
| 121 |
-
| **Program Synthesis** | `program_generator.py` | 3 hand-authored illustrative examples | Qualitative + shared metric suite |
|
| 122 |
-
| **Commit Message Generation** | `commit_gen.py` | 3 hand-authored illustrative examples | Qualitative + shared metric suite |
|
| 123 |
-
| **LLM Baseline** | `llm_baseline.py` | Same prompts as program synthesis | Comparative scoring against the small LM |
|
| 124 |
-
|
| 125 |
-
### Hybrid Indexing & Retrieval (`src/embeddings/`)
|
| 126 |
-
|
| 127 |
-
| Component | Technology | Purpose |
|
| 128 |
-
|---|---|---|
|
| 129 |
-
| `code_lm_embedder.py` | Mean-pooled hidden states of the base model itself | Retrieval embeddings without a second model download |
|
| 130 |
-
| `embedding.py` | `BAAI/bge-small-en-v1.5` (external, config-switchable) | Alternative embedding backend |
|
| 131 |
-
| `indexing.py` | FAISS `IndexFlatIP` | Dense semantic vector store (cosine similarity via normalized inner product) |
|
| 132 |
-
| `ast_indexing.py` | Python `ast` module (Python code) / regex-based structural extractor (SQL) | Structural fingerprints — function/class names, or referenced tables/aggregates/clauses |
|
| 133 |
-
| `hybrid_retriever.py` | Blended reranker | Over-fetches `k × 4` from FAISS, reranks with `α * dense + (1-α) * structural` |
|
| 134 |
-
|
| 135 |
-
### Evaluation & Persistence (`src/evaluation/`)
|
| 136 |
-
|
| 137 |
-
| Component | Function |
|
| 138 |
-
|---|---|
|
| 139 |
-
| `metrics.py` | BLEU, ROUGE, token-F1, CodeBERTScore, execution accuracy primitives |
|
| 140 |
-
| `evaluation.py` | Task-level evaluation engine |
|
| 141 |
-
| `sql_eval.py` | SQL normalization and Spider-style execution comparison |
|
| 142 |
-
| `comparator.py` | Base vs. LoRA vs. RAG side-by-side comparison |
|
| 143 |
-
| `rag_evaluation.py` | RAG gain-over-baseline measurement and top-K sweep |
|
| 144 |
-
| `visualization.py` | Matplotlib bar-chart generation, saved to `output/plots/` |
|
| 145 |
-
|
| 146 |
-
---
|
| 147 |
-
|
| 148 |
-
## Benchmark Results
|
| 149 |
-
|
| 150 |
-
Numbers below are taken directly from `outputs/generated/baseline_model_results.json` and `finetuned_model_results.json` (100-sample Spider text-to-SQL subset, 3-sample documentation-generation set).
|
| 151 |
-
|
| 152 |
-
### Baseline (un-adapted) vs. LoRA Fine-Tuned
|
| 153 |
-
|
| 154 |
-
| Task | Metric | Base Model | LoRA Fine-Tuned |
|
| 155 |
-
|---|---|:---:|:---:|
|
| 156 |
-
| **Text-to-SQL** | BLEU | 0.2329 | **0.2467** |
|
| 157 |
-
| | CodeBERTScore | 0.9740 | **0.9759** |
|
| 158 |
-
| | F1 | 0.4642 | **0.4879** |
|
| 159 |
-
| | ROUGE-1 | 0.4378 | **0.4602** |
|
| 160 |
-
| | ROUGE-L | 0.4274 | **0.4503** |
|
| 161 |
-
| | Exact Match | 7% | **8%** |
|
| 162 |
-
| | Execution Accuracy | 11% | **14%** |
|
| 163 |
-
| **Documentation Generation** | BLEU | 0.0241 | **0.0528** |
|
| 164 |
-
| | CodeBERTScore | 0.9250 | **0.9447** |
|
| 165 |
-
| | F1 | 0.1181 | **0.2022** |
|
| 166 |
-
| | ROUGE-1 | 0.0831 | **0.1615** |
|
| 167 |
-
| | ROUGE-L | 0.0719 | **0.1494** |
|
| 168 |
-
|
| 169 |
-
LoRA fine-tuning (trained on only 9 illustrative prompt/completion examples — 2.16M trainable params, 0.44% of the model) improves every metric on both tasks. Gains are proportionally larger on documentation generation.
|
| 170 |
-
|
| 171 |
-
### RAG, Top-K Sweep, and LLM Baseline (5-question SQL slice)
|
| 172 |
-
|
| 173 |
-
- On the small 5-question evaluation slice used for this stage, the **RAG-augmented pipeline underperformed the no-RAG LoRA baseline** on every metric — a genuine, reproducible negative result, not a placeholder. See [Known Limitations](#-known-limitations--future-work) for why.
|
| 174 |
-
- A **top-K sweep** over K ∈ {1, 2, 3, 5, 8} shows the gain is least negative around **K = 2–3** and degrades further at K = 8.
|
| 175 |
-
- The **LLM-baseline comparison** fell back to the same local Qwen2.5-Coder-0.5B model in this run because no external API credential was resolved at runtime — see the security note below and Known Limitations.
|
| 176 |
-
- For reference, the rule-based Spider baseline scores **12.28% exact match / 33.66% execution accuracy** over the full 1,034-question dev set.
|
| 177 |
-
|
| 178 |
-
Plots for all of the above are saved under `output/plots/` (`scores_baseline_text_to_sql_vs_doc_generation.png`, `scores_finetuned_text_to_sql_vs_doc_generation.png`, `scores_model_comparison_checkpoint2.png`, `scores_model_comparison_full.png`, `topk_gain_sweep.png`).
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
## Dataset
|
| 182 |
-
|
| 183 |
-
This project uses the **[Spider](https://yale-lily.github.io/spider)** dataset — a large-scale, cross-domain text-to-SQL benchmark — as its primary evaluation dataset.
|
| 184 |
-
|
| 185 |
-
> **Note:** The Spider dataset is **not included in this repository**. GitHub's file/repo upload limits make it impractical to commit the raw dataset (question/SQL pairs, table schemas, and per-database SQLite files), so it must be downloaded separately before running the pipeline.
|
| 186 |
-
|
| 187 |
-
### Download
|
| 188 |
-
|
| 189 |
-
1. Download the dataset from Google Drive: **[Spider dataset (Google Drive)](https://drive.google.com/file/d/1403EGqzIDoHMdQF4c9Bkyl7dZLZ5Wt6J/view)**
|
| 190 |
-
- Alternatively, see the [official Spider dataset page](https://yale-lily.github.io/spider) for the source release and licensing terms.
|
| 191 |
-
2. Unzip the download and place its contents under `data/raw/Spider/` at the repository root, so that `dev.json`, `tables.json`, and the per-database SQLite files sit alongside each other, e.g.:
|
| 192 |
-
|
| 193 |
-
```
|
| 194 |
-
data/raw/Spider/
|
| 195 |
-
├── dev.json
|
| 196 |
-
├── tables.json
|
| 197 |
-
└── database/
|
| 198 |
-
├── concert_singer/
|
| 199 |
-
│ └── concert_singer.sqlite
|
| 200 |
-
└── ...
|
| 201 |
-
```
|
| 202 |
-
|
| 203 |
-
3. Run `src/scripts/convert_spider_to_jsonl.py` to convert the raw files into the normalized `data/processed/spider_eval.jsonl` and retrieval corpus used by the notebooks (see [Quick Start](#-quick-start)).
|
| 204 |
-
|
| 205 |
-
`data/raw/`, `data/processed/`, and `data/indices/` are excluded via `.gitignore` for the same reason — they're generated/downloaded locally rather than versioned.
|
| 206 |
-
|
| 207 |
-
---
|
| 208 |
-
|
| 209 |
-
## Installation
|
| 210 |
-
|
| 211 |
-
### Prerequisites
|
| 212 |
-
|
| 213 |
-
```bash
|
| 214 |
-
Python 3.10 or higher
|
| 215 |
-
A CUDA-capable GPU is optional — the code auto-detects CUDA > MPS (Apple Silicon) > CPU
|
| 216 |
-
Recommended: 16 GB RAM, 8 GB+ VRAM if using GPU
|
| 217 |
-
```
|
| 218 |
-
|
| 219 |
-
### Step 1: Clone Repository
|
| 220 |
-
|
| 221 |
-
```bash
|
| 222 |
-
git clone https://github.com/Rithusravya/Emasters_Group-2_CapstoneProject.git
|
| 223 |
-
cd Emasters_Group-2_CapstoneProject
|
| 224 |
-
```
|
| 225 |
-
|
| 226 |
-
### Step 2: Create Virtual Environment
|
| 227 |
-
|
| 228 |
-
```bash
|
| 229 |
-
python -m venv venv
|
| 230 |
-
source venv/bin/activate # macOS/Linux
|
| 231 |
-
# OR
|
| 232 |
-
venv\Scripts\activate # Windows
|
| 233 |
-
```
|
| 234 |
-
|
| 235 |
-
### Step 3: Install Dependencies
|
| 236 |
-
|
| 237 |
-
```bash
|
| 238 |
-
pip install --upgrade pip
|
| 239 |
-
pip install -r requirements.txt
|
| 240 |
-
```
|
| 241 |
-
|
| 242 |
-
### Step 4: Verify Installation
|
| 243 |
-
|
| 244 |
-
```bash
|
| 245 |
-
python -c "import torch; print(f'CUDA Available: {torch.cuda.is_available()}')"
|
| 246 |
-
```
|
| 247 |
-
|
| 248 |
-
---
|
| 249 |
-
|
| 250 |
-
## Quick Start
|
| 251 |
-
|
| 252 |
-
This project is driven through notebooks, not a CLI. Typical order:
|
| 253 |
-
|
| 254 |
-
1. **Download & prepare data** — download the Spider dataset as described in [Dataset](#-dataset) and place it under `data/raw/Spider/`. Then run `src/scripts/convert_spider_to_jsonl.py` to convert the raw Spider files into `data/processed/spider_eval.jsonl` and a retrieval corpus; `src/scripts/prepare_processed_data.py` builds the MongoDB-style variant.
|
| 255 |
-
2. **Baseline evaluation** — open `baseline_model.ipynb` and run all cells to evaluate the un-adapted base model on text-to-SQL and documentation generation.
|
| 256 |
-
3. **LoRA fine-tuning + evaluation** — open `finetuned_model.ipynb` to train (or reload) the LoRA adapter at `models/checkpoints/lora_finetuned/` and re-run the same evaluations.
|
| 257 |
-
4. **RAG / top-K / LLM comparison** — open `eval.ipynb` to build the FAISS + AST indices, measure RAG gain, sweep top-K, and compare against the LLM baseline.
|
| 258 |
-
5. **Spider execution evaluation** — open `spider_eval.ipynb` for SQL-execution-based scoring against the SQLite databases.
|
| 259 |
-
|
| 260 |
-
Programmatic usage of the core pipeline, e.g. for text-to-SQL generation:
|
| 261 |
-
|
| 262 |
-
```python
|
| 263 |
-
from src.config import load_config
|
| 264 |
-
from src.models.load_model import ModelLoader
|
| 265 |
-
from src.generators.program_generator import GenerationPipeline
|
| 266 |
-
from src.generators.text_to_sql import TextToSQLGenerator
|
| 267 |
-
|
| 268 |
-
config = load_config() # reads configs/config.yaml
|
| 269 |
-
loader = ModelLoader(config)
|
| 270 |
-
models, tokenizer = loader.load_models(lora_path=config.lora.output_dir)
|
| 271 |
-
|
| 272 |
-
pipeline = GenerationPipeline(models["lora"] or models["base"], tokenizer, config.generation)
|
| 273 |
-
sql_gen = TextToSQLGenerator(pipeline)
|
| 274 |
-
|
| 275 |
-
result = sql_gen.generate_queries(
|
| 276 |
-
question="How many singers are there?",
|
| 277 |
-
schema="CREATE TABLE singer (singer_id INT, name TEXT, age INT)",
|
| 278 |
-
)
|
| 279 |
-
print(result["sql_query"])
|
| 280 |
-
```
|
| 281 |
-
|
| 282 |
-
---
|
| 283 |
-
|
| 284 |
-
## Repository Layout
|
| 285 |
-
|
| 286 |
-
```
|
| 287 |
-
Emasters_Group-2_CapstoneProject/
|
| 288 |
-
├── configs/
|
| 289 |
-
│ └── config.yaml # Project, model, LoRA, retrieval, eval & output paths
|
| 290 |
-
├── data/
|
| 291 |
-
│ ├── raw/ # Raw Spider files (dev.json, tables.json, SQLite DBs)
|
| 292 |
-
│ ├── processed/ # Normalized JSONL (spider_eval, retrieval_corpus, etc.)
|
| 293 |
-
│ └── indices/ # Saved FAISS index, AST store, FAISS metadata
|
| 294 |
-
├── models/
|
| 295 |
-
│ └── checkpoints/lora_finetuned/ # Saved LoRA adapter + tokenizer files
|
| 296 |
-
├── src/
|
| 297 |
-
│ ├── config.py # YAML → dataclass config loader
|
| 298 |
-
│ ├── data/
|
| 299 |
-
│ │ └── data_loader.py # Generic JSON/JSONL dataset loader (Spider, etc.)
|
| 300 |
-
│ ├── models/
|
| 301 |
-
│ │ └── load_model.py # Base model + LoRA loading, training loop, base-vs-LoRA generation
|
| 302 |
-
│ ├── generators/
|
| 303 |
-
│ │ ├── program_generator.py # Core GenerationPipeline + prompt builder
|
| 304 |
-
│ │ ├── text_to_sql.py # Text-to-SQL generator
|
| 305 |
-
│ │ ├── sql_generator.py # SQL prompt/parsing helpers
|
| 306 |
-
│ │ ├── doc_gen.py # Documentation generator
|
| 307 |
-
│ │ ├── commit_gen.py # Commit-message generator
|
| 308 |
-
│ │ └── llm_baseline.py # Pluggable local/OpenAI/Gemini LLM baseline
|
| 309 |
-
│ ├── embeddings/
|
| 310 |
-
│ │ ├── code_lm_embedder.py # Embeddings from the small code LM's own hidden states
|
| 311 |
-
│ │ ├── embedding.py # External embedding-model backend (BGE)
|
| 312 |
-
│ │ ├── indexing.py # FAISS semantic index manager
|
| 313 |
-
│ │ ├── ast_indexing.py # AST / SQL structural fingerprint indexer
|
| 314 |
-
│ │ └── hybrid_retriever.py # Dense + structural blended retrieval
|
| 315 |
-
│ ├── rag/
|
| 316 |
-
│ │ └── rag_pipeline.py # Context retrieval, prompt building, RAG generation
|
| 317 |
-
│ ├── evaluation/
|
| 318 |
-
│ │ ├── metrics.py # BLEU, ROUGE, F1, CodeBERTScore, execution accuracy
|
| 319 |
-
│ │ ├── evaluation.py # Task-level evaluation engine
|
| 320 |
-
│ │ ├── sql_eval.py # Spider SQL normalization & execution comparison
|
| 321 |
-
│ │ ├── comparator.py # Base vs. LoRA vs. RAG comparison
|
| 322 |
-
│ │ ├── rag_evaluation.py # RAG gain-over-baseline + top-K sweep
|
| 323 |
-
│ │ └── visualization.py # Chart generation
|
| 324 |
-
│ └── scripts/
|
| 325 |
-
│ ├── convert_spider_to_jsonl.py # Raw Spider → normalized JSONL
|
| 326 |
-
│ └── prepare_processed_data.py # Build derived/MongoDB-style datasets
|
| 327 |
-
├── outputs/generated/ # Per-task generated samples & timestamped run results
|
| 328 |
-
├── output/plots/ # Saved comparison charts
|
| 329 |
-
├── backup/ # Earlier snapshot of evaluation/rag/embeddings modules
|
| 330 |
-
├── baseline_model.ipynb # Base model evaluation
|
| 331 |
-
├── finetuned_model.ipynb # LoRA fine-tuning + evaluation
|
| 332 |
-
├── spider_eval.ipynb # Spider execution-based SQL evaluation
|
| 333 |
-
├── eval.ipynb # RAG gain / top-K sweep / LLM comparison
|
| 334 |
-
├── main.ipynb # Colab entry point
|
| 335 |
-
├── requirements.txt
|
| 336 |
-
└── README.md
|
| 337 |
-
```
|
| 338 |
-
|
| 339 |
-
---
|
| 340 |
-
|
| 341 |
-
## Configuration (`configs/config.yaml`)
|
| 342 |
-
|
| 343 |
-
Key fields actually read by `src/config.py`:
|
| 344 |
-
|
| 345 |
-
```yaml
|
| 346 |
-
model_name: "Qwen/Qwen2.5-Coder-0.5B-Instruct"
|
| 347 |
-
lora_adapter_path: "models/checkpoints/lora_finetuned"
|
| 348 |
-
embedding_model: "BAAI/bge-small-en-v1.5" # used only when embedding.source == "external"
|
| 349 |
-
|
| 350 |
-
embedding:
|
| 351 |
-
source: "code_lm" # "code_lm" (default, self-embeddings) or "external"
|
| 352 |
-
|
| 353 |
-
retrieval:
|
| 354 |
-
hybrid: true
|
| 355 |
-
structural_weight_alpha: 0.7
|
| 356 |
-
structural_language: "sql"
|
| 357 |
-
topk_sweep: [1, 2, 3, 5, 8]
|
| 358 |
-
|
| 359 |
-
llm_baseline:
|
| 360 |
-
backend: "local" # "local" or an external provider ("openai" / "gemini")
|
| 361 |
-
local_model: "Qwen/Qwen2.5-Coder-0.5B-Instruct"
|
| 362 |
-
api_model: "gpt-5.4-mini"
|
| 363 |
-
max_new_tokens: 256
|
| 364 |
-
temperature: 0.2
|
| 365 |
-
|
| 366 |
-
lora:
|
| 367 |
-
r: 16
|
| 368 |
-
alpha: 32
|
| 369 |
-
dropout: 0.05
|
| 370 |
-
target_modules: ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
|
| 371 |
-
batch_size: 4
|
| 372 |
-
learning_rate: 0.0003
|
| 373 |
-
epochs: 5
|
| 374 |
-
output_dir: "models/checkpoints/lora_finetuned"
|
| 375 |
-
|
| 376 |
-
indexing:
|
| 377 |
-
top_k: 3
|
| 378 |
-
vector_dim: 1024 # must match the embedding model's output dimension
|
| 379 |
-
|
| 380 |
-
outputs:
|
| 381 |
-
logs_dir: "output/logs"
|
| 382 |
-
metrics_path: "output/metrics/evaluation_metrics.json"
|
| 383 |
-
plots_dir: "output/plots"
|
| 384 |
-
```
|
| 385 |
-
|
| 386 |
-
---
|
| 387 |
-
|
| 388 |
-
## Requirements
|
| 389 |
-
|
| 390 |
-
| Package | Version | Purpose |
|
| 391 |
-
|---|---|---|
|
| 392 |
-
| `torch` | 2.5.1 | Core deep learning framework |
|
| 393 |
-
| `transformers` | 4.46.3 | Pretrained model + tokenizer loading (Qwen2.5-Coder) |
|
| 394 |
-
| `peft` | 0.13.2 | LoRA fine-tuning |
|
| 395 |
-
| `datasets` | 3.1.0 | Dataset utilities |
|
| 396 |
-
| `faiss-cpu` | 1.9.0 | Dense vector similarity search |
|
| 397 |
-
| `tree-sitter` / `tree-sitter-python` | 0.23.2 / 0.23.6 | (Available for AST-based parsing) |
|
| 398 |
-
| `codebleu` | 0.7.0 | CodeBLEU metric |
|
| 399 |
-
| `codebert-score` | 0.3.13 | CodeBERTScore semantic similarity metric |
|
| 400 |
-
| `evaluate` | 0.4.3 | Hugging Face evaluation metrics |
|
| 401 |
-
| `rouge-score` | 0.1.2 | ROUGE metrics |
|
| 402 |
-
| `nltk` | 3.9.1 | Text preprocessing / BLEU support |
|
| 403 |
-
| `PyYAML` | 6.0.2 | Config parsing |
|
| 404 |
-
| `matplotlib` / `seaborn` | 3.10.0 / 0.13.2 | Plotting |
|
| 405 |
-
| `numpy` | 2.1.3 | Numerical computing |
|
| 406 |
-
|
| 407 |
-
See `requirements.txt` for the full pinned list.
|
| 408 |
-
|
| 409 |
-
---
|
| 410 |
-
|
| 411 |
-
## Contributing
|
| 412 |
-
|
| 413 |
-
1. **Fork** the repository
|
| 414 |
-
2. **Create** a feature branch (`git checkout -b feature/amazing-feature`)
|
| 415 |
-
3. **Commit** your changes (`git commit -m 'Add amazing feature'`)
|
| 416 |
-
4. **Push** to the branch (`git push origin feature/amazing-feature`)
|
| 417 |
-
5. **Open** a Pull Request
|
| 418 |
-
|
| 419 |
-
---
|
| 420 |
-
|
| 421 |
-
## License
|
| 422 |
-
|
| 423 |
-
This project is licensed under the **MIT License** — see the `LICENSE` file for details.
|
| 424 |
-
|
| 425 |
-
---
|
| 426 |
-
|
| 427 |
-
## Citation
|
| 428 |
-
|
| 429 |
-
```bibtex
|
| 430 |
-
@software{codegen_capstone_2026,
|
| 431 |
-
title = {CodeGen: Small Code-LM Fine-Tuning and Hybrid RAG Pipeline},
|
| 432 |
-
author = {Emasters Group 2},
|
| 433 |
-
year = {2026},
|
| 434 |
-
url = {https://github.com/Rithusravya/Emasters_Group-2_CapstoneProject}
|
| 435 |
-
}
|
| 436 |
-
```
|
| 437 |
-
|
| 438 |
-
---
|
| 439 |
-
|
| 440 |
-
<div align="center">
|
| 441 |
-
|
| 442 |
-
**Built by IIIT-H eMasters Group 2**
|
| 443 |
-
|
| 444 |
-
[⬆ Back to Top](#-codegen-small-code-lm-fine-tuning--hybrid-rag-pipeline)
|
| 445 |
-
|
| 446 |
-
</div>
|
|
|
|
| 1 |
---
|
| 2 |
+
title: CodeGen
|
| 3 |
+
emoji: 💻
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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