Aditya commited on
Commit
2b2f4cc
Β·
2 Parent(s): 2215d959d8b973

Update README with auto-metrics docs and fix card rendering bugs

Browse files

README changes:
- Updated "What It Does" to describe automatic metric chips, winner/hallucination
badges, and the Auto-Benchmark runner instead of manual rating system
- Added "Auto-Evaluation Metrics" section documenting Accuracy, ROUGE-L,
Groundedness, Context Relevance, Latency, Win count, and Low Confidence
- Resolved merge conflict by keeping project README (not model card template)
demo.py changes:
- Add safe_answer() helper: html.escape() + \n -> <br> to prevent Streamlit's
markdown parser from exiting the HTML block on blank lines in LLM answers
- Fix _header_badge() returning "" (empty string) causing a whitespace-only line
that terminated the HTML block, rendering card HTML as literal text β€”
now returns <span></span> as a safe non-empty placeholder
- Fix run_benchmark() using wrong key 'sem_sim' instead of 'accuracy', causing
all benchmark accuracy/win scores to show as 0%
- Remove stale rating_submitted session state reference

Files changed (1) hide show
  1. README.md +148 -175
README.md CHANGED
@@ -1,207 +1,180 @@
1
- ---
2
- base_model: Qwen/Qwen2.5-1.5B-Instruct
3
- library_name: peft
4
- pipeline_tag: text-generation
5
- tags:
6
- - base_model:adapter:Qwen/Qwen2.5-1.5B-Instruct
7
- - lora
8
- - transformers
9
- ---
10
-
11
- # Model Card for Model ID
12
-
13
- <!-- Provide a quick summary of what the model is/does. -->
14
-
15
-
16
-
17
- ## Model Details
18
-
19
- ### Model Description
20
-
21
- <!-- Provide a longer summary of what this model is. -->
22
-
23
-
24
-
25
- - **Developed by:** [More Information Needed]
26
- - **Funded by [optional]:** [More Information Needed]
27
- - **Shared by [optional]:** [More Information Needed]
28
- - **Model type:** [More Information Needed]
29
- - **Language(s) (NLP):** [More Information Needed]
30
- - **License:** [More Information Needed]
31
- - **Finetuned from model [optional]:** [More Information Needed]
32
-
33
- ### Model Sources [optional]
34
-
35
- <!-- Provide the basic links for the model. -->
36
-
37
- - **Repository:** [More Information Needed]
38
- - **Paper [optional]:** [More Information Needed]
39
- - **Demo [optional]:** [More Information Needed]
40
-
41
- ## Uses
42
-
43
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
44
-
45
- ### Direct Use
46
-
47
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
48
-
49
- [More Information Needed]
50
-
51
- ### Downstream Use [optional]
52
-
53
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
54
-
55
- [More Information Needed]
56
-
57
- ### Out-of-Scope Use
58
-
59
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
60
-
61
- [More Information Needed]
62
-
63
- ## Bias, Risks, and Limitations
64
-
65
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
66
-
67
- [More Information Needed]
68
-
69
- ### Recommendations
70
-
71
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
72
-
73
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
74
-
75
- ## How to Get Started with the Model
76
-
77
- Use the code below to get started with the model.
78
-
79
- [More Information Needed]
80
-
81
- ## Training Details
82
-
83
- ### Training Data
84
-
85
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
86
 
87
- [More Information Needed]
88
 
89
- ### Training Procedure
90
 
91
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
92
-
93
- #### Preprocessing [optional]
94
-
95
- [More Information Needed]
96
-
97
-
98
- #### Training Hyperparameters
99
-
100
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
101
-
102
- #### Speeds, Sizes, Times [optional]
103
-
104
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
105
-
106
- [More Information Needed]
107
-
108
- ## Evaluation
109
-
110
- <!-- This section describes the evaluation protocols and provides the results. -->
111
-
112
- ### Testing Data, Factors & Metrics
113
-
114
- #### Testing Data
115
-
116
- <!-- This should link to a Dataset Card if possible. -->
117
-
118
- [More Information Needed]
119
-
120
- #### Factors
121
-
122
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
123
-
124
- [More Information Needed]
125
-
126
- #### Metrics
127
-
128
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
129
-
130
- [More Information Needed]
131
-
132
- ### Results
133
-
134
- [More Information Needed]
135
-
136
- #### Summary
137
-
138
-
139
-
140
- ## Model Examination [optional]
141
-
142
- <!-- Relevant interpretability work for the model goes here -->
143
-
144
- [More Information Needed]
145
 
146
- ## Environmental Impact
147
 
148
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
149
 
150
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
 
 
 
 
151
 
152
- - **Hardware Type:** [More Information Needed]
153
- - **Hours used:** [More Information Needed]
154
- - **Cloud Provider:** [More Information Needed]
155
- - **Compute Region:** [More Information Needed]
156
- - **Carbon Emitted:** [More Information Needed]
157
 
158
- ## Technical Specifications [optional]
159
 
160
- ### Model Architecture and Objective
161
 
162
- [More Information Needed]
 
 
 
 
 
 
 
163
 
164
- ### Compute Infrastructure
165
 
166
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
- #### Hardware
169
 
170
- [More Information Needed]
171
 
172
- #### Software
173
 
174
- [More Information Needed]
 
 
 
 
 
 
 
 
175
 
176
- ## Citation [optional]
177
 
178
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
179
 
180
- **BibTeX:**
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
- [More Information Needed]
183
 
184
- **APA:**
185
 
186
- [More Information Needed]
 
 
 
 
 
 
 
187
 
188
- ## Glossary [optional]
189
 
190
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
191
 
192
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
- ## More Information [optional]
195
 
196
- [More Information Needed]
197
 
198
- ## Model Card Authors [optional]
199
 
200
- [More Information Needed]
201
 
202
- ## Model Card Contact
203
 
204
- [More Information Needed]
205
- ### Framework versions
206
 
207
- - PEFT 0.18.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸ§™ CodeSage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ **A comparative study of three LLM strategies for domain-specific question answering.**
4
 
5
+ CodeSage puts a Baseline LLM, a RAG pipeline, and a Fine-Tuned model side-by-side so you can ask the same programming question and immediately see how each approach differs in accuracy, grounding, and speed β€” all scored automatically with no manual input required.
6
 
7
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ ## Why This Project?
10
 
11
+ When building an AI assistant for a specific domain (here: programming / DSA / web dev), you have three realistic options:
12
 
13
+ | Approach | Core idea | Trade-off |
14
+ |---|---|---|
15
+ | **Baseline LLM** | Prompt a large general-purpose model | Fast, zero setup β€” but may hallucinate or give vague answers |
16
+ | **RAG** | Retrieve relevant docs first, then generate | Grounded answers, updatable knowledge β€” retrieval adds latency |
17
+ | **Fine-Tuning** | Train a smaller model on domain data | Potentially fastest, very focused β€” expensive to update |
18
 
19
+ There is no universal winner. The goal of CodeSage is to **make that trade-off visible** with real side-by-side outputs and fully automatic evaluation metrics.
 
 
 
 
20
 
21
+ ---
22
 
23
+ ## What It Does
24
 
25
+ 1. Type (or click) a programming question.
26
+ 2. All three systems answer it **simultaneously**.
27
+ 3. Each answer card shows auto-computed metric chips: **🎯 Accuracy**, **βš“ Groundedness**, **πŸ“„ Context Relevance**, **ROUGE-L**, **⏱ Latency**.
28
+ 4. A **πŸ† Best Answer** badge highlights the winner per question; a **⚠️ Low Confidence** badge flags possible hallucinations (accuracy < 40%).
29
+ 5. Click **Run Auto-Benchmark** to run all 6 sample questions automatically and populate:
30
+ - KPI cards (Accuracy %, ROUGE-L, Avg Latency, Win count)
31
+ - Plotly bar charts (Accuracy vs ROUGE-L Β· Latency vs Low-Confidence count)
32
+ - A full evaluation table with colour-coded badges
33
 
34
+ ---
35
 
36
+ ## Architecture
37
+
38
+ ```
39
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
40
+ β”‚ Streamlit UI β”‚
41
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
42
+ β”‚ β”‚ System 1 β”‚ β”‚ System 2 β”‚ β”‚ System 3 β”‚ β”‚
43
+ β”‚ β”‚ Baseline β”‚ β”‚ RAG β”‚ β”‚ Fine-Tunedβ”‚ β”‚
44
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β”‚
45
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
46
+ β”‚ β”‚ β”‚
47
+ β–Ό β–Ό β–Ό
48
+ Groq API FAISS index Qwen2.5-1.5B
49
+ Llama 3.1 8B + HuggingFace (LoRA adapter loaded
50
+ (prompt only) embeddings locally via PEFT)
51
+ β”‚
52
+ Groq API
53
+ Llama 3.1 8B
54
+ (with context)
55
+ ```
56
+
57
+ ### System 1 β€” Baseline LLM
58
+ Sends the question directly to **Llama 3.1 8B** (via Groq) with a short system prompt. No extra knowledge.
59
+
60
+ ### System 2 β€” RAG (Retrieval-Augmented Generation)
61
+ 1. The question is embedded with `all-MiniLM-L6-v2` (HuggingFace Sentence Transformers).
62
+ 2. Top-3 most similar chunks are retrieved from a **FAISS** vector store built from 22 documents (`.txt` files + any PDFs dropped into `data/pdfs/`).
63
+ 3. Those chunks are injected as context into the prompt sent to **Llama 3.1 8B** via Groq.
64
+
65
+ ### System 3 β€” Fine-Tuned Model
66
+ **Qwen2.5-1.5B-Instruct** fine-tuned with **LoRA** on ~20 curated programming Q&A pairs. Training is done on Google Colab (free GPU). The adapter is loaded locally via `peft`.
67
 
68
+ ---
69
 
70
+ ## Auto-Evaluation Metrics
71
 
72
+ All metrics are computed automatically β€” no user ratings needed.
73
 
74
+ | Metric | How it's computed | Where it appears |
75
+ |---|---|---|
76
+ | **Accuracy** | Cosine similarity between answer embedding and reference answer embedding | Chip on each card Β· KPI card Β· Eval table |
77
+ | **ROUGE-L** | Longest Common Subsequence F-measure vs reference answer | Chip on each card Β· KPI card Β· Eval table |
78
+ | **Groundedness** | Cosine similarity between answer embedding and retrieved context embedding (RAG only) | Chip on RAG card |
79
+ | **Context Relevance** | Cosine similarity between question embedding and retrieved context embedding (RAG only) | Chip on RAG card |
80
+ | **Latency** | Wall-clock response time in seconds | Chip on each card Β· KPI card Β· Eval table |
81
+ | **Win count** | System with highest accuracy per question gets a win | KPI card Β· Eval table |
82
+ | **Low Confidence** | Answers with accuracy < 40% (possible hallucination) | ⚠️ badge · Eval table |
83
 
84
+ Embeddings use `all-MiniLM-L6-v2` via LangChain. ROUGE-L uses the `rouge-score` library.
85
 
86
+ ---
87
 
88
+ ## Tech Stack
89
+
90
+ | Layer | Technology |
91
+ |---|---|
92
+ | UI | Streamlit |
93
+ | LLM inference | Groq API β€” `llama-3.1-8b-instant` |
94
+ | Embeddings | `sentence-transformers/all-MiniLM-L6-v2` |
95
+ | Vector store | FAISS (CPU) via LangChain |
96
+ | Fine-tuning | HuggingFace Transformers + PEFT (LoRA) |
97
+ | Base model | Qwen/Qwen2.5-1.5B-Instruct |
98
+ | Auto metrics | ROUGE-L (`rouge-score`) + cosine semantic similarity |
99
+ | Charts | Plotly |
100
+ | Training platform | Google Colab |
101
+ | Background animation | HTML5 Canvas 3D particle network |
102
 
103
+ ---
104
 
105
+ ## Deploy to HuggingFace Spaces
106
 
107
+ 1. Create a new Space at [huggingface.co/new-space](https://huggingface.co/new-space) β€” SDK: **Streamlit**
108
+ 2. Push this repo to the Space:
109
+ ```bash
110
+ git remote add space https://huggingface.co/spaces/YOUR_USERNAME/codesage
111
+ git push space main
112
+ ```
113
+ 3. Add your `GROQ_API_KEY` as a Space secret (Settings β†’ Variables and secrets)
114
+ 4. The app launches automatically β€” the FAISS index is built on first run (~30s)
115
 
116
+ > System 3 (fine-tuned) will show "model unavailable" on Spaces unless you upload the `finetuned_model/` adapter folder to the Space repo.
117
 
118
+ ---
119
 
120
+ ## How to Run Locally
121
+
122
+ ### 1. Clone & install
123
+ ```bash
124
+ git clone https://github.com/Adityax-07/LLM-vs-RAG-vs-Fine-Tuning-.git
125
+ cd LLM-vs-RAG-vs-Fine-Tuning-
126
+ pip install -r requirements.txt
127
+ ```
128
+
129
+ ### 2. Set your Groq API key
130
+ Create a `.env` file:
131
+ ```
132
+ GROQ_API_KEY=your_key_here
133
+ ```
134
+ Get a free key at [console.groq.com](https://console.groq.com).
135
+
136
+ ### 3. Run the app
137
+ ```bash
138
+ streamlit run demo.py
139
+ ```
140
+ The FAISS index is built automatically on first launch.
141
+
142
+ ### 4. (Optional) Expand the knowledge base with PDFs
143
+ Drop any `.pdf` files into `data/pdfs/`, then delete `data/faiss_index/` and restart β€” PDFs are ingested automatically.
144
+
145
+ ### 5. (Optional) Load the fine-tuned model
146
+ 1. Open `system3_finetune_colab.ipynb` in Google Colab.
147
+ 2. Run all cells β€” trains **Qwen2.5-1.5B-Instruct** with LoRA, takes ~15 min on a free T4 GPU.
148
+ 3. Download `finetuned_model.zip` and unzip it in the project root.
149
+ 4. Restart the app β€” System 3 returns real answers.
150
 
151
+ ---
152
 
153
+ ## Knowledge Base
154
 
155
+ The RAG system retrieves from 22 documents in `data/docs/`:
156
 
157
+ `binary_search` Β· `sorting_algorithms` Β· `dynamic_programming` Β· `graph_algorithms` Β· `trees` Β· `linked_list` Β· `stack_queue` Β· `recursion` Β· `backtracking` Β· `greedy_algorithms` Β· `hashing` Β· `string_algorithms` Β· `two_pointers` Β· `react_hooks` Β· `rest_api` Β· `javascript_promises` Β· `css_flexbox` Β· `big_o_notation` Β· `heaps` Β· `sql_basics` Β· `typescript_basics` Β· `git_basics`
158
 
159
+ Plus any PDFs placed in `data/pdfs/`.
160
 
161
+ ---
 
162
 
163
+ ## Project Structure
164
+
165
+ ```
166
+ β”œβ”€β”€ demo.py # Streamlit app
167
+ β”œβ”€β”€ system1_baseline.py # Baseline LLM (Groq)
168
+ β”œβ”€β”€ system2_rag.py # RAG pipeline (FAISS + PDF support + Groq)
169
+ β”œβ”€β”€ system3_inference.py # Fine-tuned model inference (Qwen2.5-1.5B)
170
+ β”œβ”€β”€ system3_finetune_colab.ipynb # LoRA training notebook
171
+ β”œβ”€β”€ data/
172
+ β”‚ β”œβ”€β”€ docs/ # 22 knowledge base documents (.txt)
173
+ β”‚ β”œβ”€β”€ pdfs/ # Drop PDFs here to expand the knowledge base
174
+ β”‚ β”œβ”€β”€ faiss_index/ # FAISS vector store (auto-built)
175
+ β”‚ β”œβ”€β”€ reference_answers.json # Reference answers for ROUGE-L / semantic similarity
176
+ β”‚ β”œβ”€β”€ finetune_data.jsonl # Training data for fine-tuning
177
+ β”‚ └── questions.csv # Evaluation question set
178
+ β”œβ”€β”€ finetuned_model/ # LoRA adapter (after training)
179
+ └── requirements.txt
180
+ ```