Md-Asif commited on
Commit
a138ead
Β·
verified Β·
1 Parent(s): 7c9b130

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -49
app.py CHANGED
@@ -1,7 +1,6 @@
1
  """
2
  Qwen Coder Battle β€” Fine-tuned LoRA vs Base Model
3
  ===================================================
4
-
5
  Compares Md-Asif/qwen-coder-python-ft (a LoRA adapter) against its base
6
  Qwen2.5-Coder-3B model, side by side, on Python coding instructions.
7
 
@@ -13,6 +12,23 @@ Memory-efficient design for HF ZeroGPU free tier:
13
 
14
  If you'd rather run this as two fully separate models, see the
15
  `# ALTERNATE: two full models` comment block near the model loading code.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  """
17
 
18
  import ast
@@ -28,21 +44,66 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
28
  # ----------------------------------------------------------------------------
29
  # CONFIG β€” edit these to match how you actually trained the adapter
30
  # ----------------------------------------------------------------------------
31
-
32
  # ⚠️ IMPORTANT: this MUST be the exact base model you fine-tuned on top of.
33
- # If your adapter was trained on the plain (non-instruct) base, change this to:
 
 
34
  # "Qwen/Qwen2.5-Coder-3B"
 
 
35
  BASE_MODEL_ID = "Qwen/Qwen2.5-Coder-3B"
36
-
37
  ADAPTER_ID = "Md-Asif/qwen-coder-python-ft"
38
 
39
- SYSTEM_PROMPT = (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  "You are an expert Python programmer. Given an instruction, respond with "
41
  "clean, correct, well-documented Python code. Wrap code in a ```python "
42
  "fenced block. Keep explanations brief."
43
  )
44
 
45
  MAX_NEW_TOKENS_DEFAULT = 512
 
46
  DTYPE = torch.bfloat16
47
 
48
  # ----------------------------------------------------------------------------
@@ -51,13 +112,15 @@ DTYPE = torch.bfloat16
51
  # write as if a GPU is always present β€” the real GPU is attached only inside
52
  # functions decorated with @spaces.GPU.
53
  # ----------------------------------------------------------------------------
54
-
55
  print("Loading tokenizer...")
56
  try:
57
  tokenizer = AutoTokenizer.from_pretrained(ADAPTER_ID)
58
  except Exception:
59
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
60
 
 
 
 
61
  print("Loading base model (on CPU first β€” do NOT use device_map='auto' here;")
62
  print("it forces real CUDA writes before ZeroGPU has attached a GPU)...")
63
  base_model = AutoModelForCausalLM.from_pretrained(
@@ -91,11 +154,10 @@ model.eval()
91
  # ----------------------------------------------------------------------------
92
  # GENERATION
93
  # ----------------------------------------------------------------------------
94
-
95
- def build_inputs(instruction: str):
96
  messages = [
97
- {"role": "system", "content": SYSTEM_PROMPT},
98
- {"role": "user", "content": instruction},
99
  ]
100
  text = tokenizer.apply_chat_template(
101
  messages, tokenize=False, add_generation_prompt=True
@@ -103,13 +165,25 @@ def build_inputs(instruction: str):
103
  return tokenizer(text, return_tensors="pt").to(model.device)
104
 
105
 
106
- def _generate(instruction: str, max_new_tokens: int, temperature: float, use_sampling: bool):
107
- inputs = build_inputs(instruction)
 
 
 
 
 
 
 
 
 
 
 
 
108
  gen_kwargs = dict(
109
  **inputs,
110
  max_new_tokens=max_new_tokens,
111
  do_sample=use_sampling,
112
- pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
113
  )
114
  if use_sampling:
115
  gen_kwargs["temperature"] = max(temperature, 0.01)
@@ -122,25 +196,42 @@ def _generate(instruction: str, max_new_tokens: int, temperature: float, use_sam
122
 
123
  new_tokens = output_ids[0][inputs["input_ids"].shape[1]:]
124
  text = tokenizer.decode(new_tokens, skip_special_tokens=True)
125
- n_tokens = len(new_tokens)
126
- return text, elapsed, n_tokens
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
 
129
  @spaces.GPU(duration=90)
130
- def run_battle(instruction: str, max_new_tokens: int, temperature: float, use_sampling: bool):
131
  if not instruction or not instruction.strip():
132
  raise gr.Error("Please enter an instruction first.")
133
 
134
- # Fine-tuned model (adapter active)
135
- ft_text, ft_time, ft_tokens = _generate(instruction, max_new_tokens, temperature, use_sampling)
 
 
 
136
 
137
- # Base model (adapter disabled β€” same weights, no reload)
 
138
  with model.disable_adapter():
139
- base_text, base_time, base_tokens = _generate(instruction, max_new_tokens, temperature, use_sampling)
140
 
141
  ft_metrics = analyze_response(ft_text, ft_time, ft_tokens)
142
  base_metrics = analyze_response(base_text, base_time, base_tokens)
143
-
144
  return ft_text, base_text, ft_metrics, base_metrics
145
 
146
 
@@ -151,7 +242,6 @@ def run_battle(instruction: str, max_new_tokens: int, temperature: float, use_sa
151
  # against real test cases (e.g. HumanEval-style pass@1) β€” see the note in
152
  # the README for how to extend this.
153
  # ----------------------------------------------------------------------------
154
-
155
  CODE_BLOCK_RE = re.compile(r"```(?:python)?\s*\n(.*?)```", re.DOTALL)
156
 
157
 
@@ -174,8 +264,8 @@ def analyze_response(text: str, elapsed: float, n_tokens: int) -> dict:
174
  "num_classes": 0,
175
  "has_docstring": False,
176
  "lines_of_code": len([l for l in code.splitlines() if l.strip()]),
 
177
  }
178
-
179
  try:
180
  tree = ast.parse(code)
181
  metrics["syntax_valid"] = True
@@ -199,14 +289,12 @@ def analyze_response(text: str, elapsed: float, n_tokens: int) -> dict:
199
  # Small speed bonus, capped, so a slow-but-correct answer isn't punished hard
200
  score += min(10, metrics["tokens_per_sec"] / 5)
201
  metrics["quality_score"] = round(min(score, 100), 1)
202
-
203
  return metrics
204
 
205
 
206
  # ----------------------------------------------------------------------------
207
  # UI RENDERING HELPERS
208
  # ----------------------------------------------------------------------------
209
-
210
  def metric_row(label, ft_val, base_val, higher_is_better=True, suffix=""):
211
  try:
212
  ft_num, base_num = float(ft_val), float(base_val)
@@ -214,7 +302,6 @@ def metric_row(label, ft_val, base_val, higher_is_better=True, suffix=""):
214
  base_win = base_num > ft_num if higher_is_better else base_num < ft_num
215
  except (TypeError, ValueError):
216
  ft_win = base_win = False
217
-
218
  ft_cls = "metric-win" if ft_win else ""
219
  base_cls = "metric-win" if base_win else ""
220
  return f"""
@@ -228,7 +315,6 @@ def metric_row(label, ft_val, base_val, higher_is_better=True, suffix=""):
228
  def render_comparison(ft_metrics: dict, base_metrics: dict) -> str:
229
  winner = "Fine-tuned" if ft_metrics["quality_score"] >= base_metrics["quality_score"] else "Base"
230
  winner_badge = f'<div class="winner-badge">πŸ† {winner} model scored higher</div>'
231
-
232
  rows = "".join([
233
  metric_row("Quality score", ft_metrics["quality_score"], base_metrics["quality_score"], suffix="/100"),
234
  metric_row("Syntax valid", "βœ… Yes" if ft_metrics["syntax_valid"] else "❌ No",
@@ -242,7 +328,6 @@ def render_comparison(ft_metrics: dict, base_metrics: dict) -> str:
242
  higher_is_better=False, suffix="s"),
243
  metric_row("Tokens / sec", ft_metrics["tokens_per_sec"], base_metrics["tokens_per_sec"], suffix=" tok/s"),
244
  ])
245
-
246
  return f"""
247
  <div class="comparison-wrap">
248
  {winner_badge}
@@ -254,7 +339,9 @@ def render_comparison(ft_metrics: dict, base_metrics: dict) -> str:
254
  </table>
255
  <div class="disclaimer">
256
  Quality score is a static-analysis heuristic (syntax validity, structure,
257
- docstrings, speed) β€” not a guarantee of runtime correctness.
 
 
258
  </div>
259
  </div>"""
260
 
@@ -262,14 +349,12 @@ def render_comparison(ft_metrics: dict, base_metrics: dict) -> str:
262
  # ----------------------------------------------------------------------------
263
  # CUSTOM STYLING
264
  # ----------------------------------------------------------------------------
265
-
266
  CUSTOM_CSS = """
267
  :root {
268
  --accent: #7c3aed;
269
  --accent-2: #06b6d4;
270
  }
271
  .gradio-container { max-width: 1200px !important; margin: auto; }
272
-
273
  #hero {
274
  background: linear-gradient(135deg, #1e1b4b 0%, #4c1d95 45%, #0e7490 100%);
275
  border-radius: 18px;
@@ -279,7 +364,6 @@ CUSTOM_CSS = """
279
  }
280
  #hero h1 { margin: 0 0 6px 0; font-size: 1.7rem; }
281
  #hero p { margin: 0; opacity: 0.85; font-size: 0.95rem; }
282
-
283
  .model-card {
284
  border-radius: 14px;
285
  padding: 4px;
@@ -297,7 +381,6 @@ CUSTOM_CSS = """
297
  }
298
  .label-ft { background: rgba(124,58,237,0.15); color: #a78bfa; }
299
  .label-base { background: rgba(148,163,184,0.15); color: #94a3b8; }
300
-
301
  .comparison-wrap { padding: 6px 4px; }
302
  .winner-badge {
303
  text-align: center;
@@ -318,27 +401,37 @@ CUSTOM_CSS = """
318
  # ----------------------------------------------------------------------------
319
  # GRADIO APP
320
  # ----------------------------------------------------------------------------
321
-
322
  EXAMPLES = [
323
- "Write a function that checks if a string is a valid palindrome, ignoring punctuation and case.",
324
- "Implement a LRU cache from scratch without using functools.lru_cache.",
325
- "Write a script that reads a CSV file and returns the top 5 rows by a given column, using pandas.",
326
- "Create a decorator that retries a function up to N times with exponential backoff.",
 
 
327
  ]
328
 
329
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="violet", secondary_hue="cyan")) as demo:
330
  gr.HTML("""
331
  <div id="hero">
332
  <h1>⚑ Qwen Coder Battle</h1>
333
- <p>Md-Asif/qwen-coder-python-ft (LoRA fine-tune) vs Qwen2.5-Coder-3B (base) β€” same prompt, same seed, same GPU. Judge for yourself.</p>
334
  </div>
335
  """)
336
 
337
- instruction = gr.Textbox(
338
- label="Coding instruction",
339
- placeholder="e.g. Write a function that merges two sorted lists in O(n) time.",
340
- lines=3,
341
- )
 
 
 
 
 
 
 
 
 
342
 
343
  with gr.Row():
344
  run_btn = gr.Button("βš”οΈ Run the battle", variant="primary", scale=3)
@@ -348,7 +441,7 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="violet", second
348
  use_sampling = gr.Checkbox(value=False, label="Use sampling (off = greedy, fairest for comparison)")
349
  temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature (only if sampling is on)")
350
 
351
- gr.Examples(examples=EXAMPLES, inputs=instruction)
352
 
353
  with gr.Row():
354
  with gr.Column():
@@ -359,17 +452,16 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="violet", second
359
  base_output = gr.Code(language="python", label=None, lines=18)
360
 
361
  comparison_html = gr.HTML()
362
-
363
  ft_metrics_state = gr.State()
364
  base_metrics_state = gr.State()
365
 
366
- def on_run(instr, max_tok, temp, sample):
367
- ft_text, base_text, ft_m, base_m = run_battle(instr, max_tok, temp, sample)
368
  return ft_text, base_text, render_comparison(ft_m, base_m), ft_m, base_m
369
 
370
  run_btn.click(
371
  on_run,
372
- inputs=[instruction, max_tokens, temperature, use_sampling],
373
  outputs=[ft_output, base_output, comparison_html, ft_metrics_state, base_metrics_state],
374
  )
375
 
 
1
  """
2
  Qwen Coder Battle β€” Fine-tuned LoRA vs Base Model
3
  ===================================================
 
4
  Compares Md-Asif/qwen-coder-python-ft (a LoRA adapter) against its base
5
  Qwen2.5-Coder-3B model, side by side, on Python coding instructions.
6
 
 
12
 
13
  If you'd rather run this as two fully separate models, see the
14
  `# ALTERNATE: two full models` comment block near the model loading code.
15
+
16
+ --------------------------------------------------------------------------
17
+ IMPORTANT β€” prompt format
18
+ --------------------------------------------------------------------------
19
+ Md-Asif/python-fine-tune trains on FOUR distinct (system_prompt, task_tag)
20
+ pairs, keyed by a `task` column: GENERATE, REFACTOR, DEBUG, CODE_REVIEW.
21
+ Every training example's user turn is prefixed with "<TASK:XXXX>\n\n".
22
+
23
+ The LoRA adapter has only ever seen instructions in that exact shape. Feed
24
+ it a plain instruction with a different system prompt and no tag, and it's
25
+ operating out-of-distribution β€” often producing WORSE output than the
26
+ untouched base model, which was never trained to expect the tag at all.
27
+
28
+ So: the fine-tuned model gets the task-specific system prompt + tagged
29
+ instruction. The base model gets a plain, generic system prompt and the
30
+ RAW instruction (no tag) β€” that's the fair, apples-to-apples comparison,
31
+ since neither format is "natural" to the base model anyway.
32
  """
33
 
34
  import ast
 
44
  # ----------------------------------------------------------------------------
45
  # CONFIG β€” edit these to match how you actually trained the adapter
46
  # ----------------------------------------------------------------------------
 
47
  # ⚠️ IMPORTANT: this MUST be the exact base model you fine-tuned on top of.
48
+ # If your adapter was trained on the Instruct checkpoint, use:
49
+ # "Qwen/Qwen2.5-Coder-3B-Instruct"
50
+ # If it was trained on the plain base, use:
51
  # "Qwen/Qwen2.5-Coder-3B"
52
+ # Getting this wrong silently wrecks fine-tuned quality β€” the LoRA delta
53
+ # was computed relative to ONE specific set of base weights.
54
  BASE_MODEL_ID = "Qwen/Qwen2.5-Coder-3B"
 
55
  ADAPTER_ID = "Md-Asif/qwen-coder-python-ft"
56
 
57
+ # System prompts, EXACTLY matching what's in the training dataset's
58
+ # `messages[0]["content"]` for each task. Keep these byte-identical to the
59
+ # dataset β€” even small wording changes are a mild distribution shift.
60
+ TASK_SYSTEM_PROMPTS = {
61
+ "GENERATE": (
62
+ "You are a senior Python engineer.\n"
63
+ "Write complete, correct, production-ready Python code.\n"
64
+ "Include type hints, docstrings, and handle edge cases.\n"
65
+ "No placeholders. No TODOs. Only working code."
66
+ ),
67
+ "REFACTOR": (
68
+ "You are an expert Python refactoring assistant.\n"
69
+ "Rewrite the code following:\n"
70
+ "- PEP 8\n"
71
+ "- Type hints\n"
72
+ "- Docstrings\n"
73
+ "- Best practices\n"
74
+ "- SOLID principles\n\n"
75
+ "Do not change behavior. Only improve structure and quality."
76
+ ),
77
+ "DEBUG": (
78
+ "You are an expert Python debugging engineer.\n"
79
+ "Locate the exact bug.\n"
80
+ "Explain precisely why it happens.\n"
81
+ "Produce corrected, complete Python code."
82
+ ),
83
+ "CODE_REVIEW": (
84
+ "You are a senior Python code reviewer.\n"
85
+ "Review code like a staff engineer.\n\n"
86
+ "Find and fix:\n"
87
+ "- Syntax bugs\n"
88
+ "- Runtime bugs\n"
89
+ "- Logic bugs\n"
90
+ "- Performance issues\n"
91
+ "- Security issues\n"
92
+ "- Maintainability issues\n"
93
+ "Produce corrected production-quality Python code and explain every change."
94
+ ),
95
+ }
96
+
97
+ # What the BASE model gets instead β€” a neutral prompt with no dataset-specific
98
+ # tagging, since the base model was never trained on the <TASK:X> scheme.
99
+ BASE_SYSTEM_PROMPT = (
100
  "You are an expert Python programmer. Given an instruction, respond with "
101
  "clean, correct, well-documented Python code. Wrap code in a ```python "
102
  "fenced block. Keep explanations brief."
103
  )
104
 
105
  MAX_NEW_TOKENS_DEFAULT = 512
106
+ WARMUP_TOKENS = 8 # tiny generation to absorb CUDA cold-start before timing
107
  DTYPE = torch.bfloat16
108
 
109
  # ----------------------------------------------------------------------------
 
112
  # write as if a GPU is always present β€” the real GPU is attached only inside
113
  # functions decorated with @spaces.GPU.
114
  # ----------------------------------------------------------------------------
 
115
  print("Loading tokenizer...")
116
  try:
117
  tokenizer = AutoTokenizer.from_pretrained(ADAPTER_ID)
118
  except Exception:
119
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
120
 
121
+ if tokenizer.pad_token_id is None:
122
+ tokenizer.pad_token = tokenizer.eos_token
123
+
124
  print("Loading base model (on CPU first β€” do NOT use device_map='auto' here;")
125
  print("it forces real CUDA writes before ZeroGPU has attached a GPU)...")
126
  base_model = AutoModelForCausalLM.from_pretrained(
 
154
  # ----------------------------------------------------------------------------
155
  # GENERATION
156
  # ----------------------------------------------------------------------------
157
+ def _build_inputs(system_prompt: str, user_content: str):
 
158
  messages = [
159
+ {"role": "system", "content": system_prompt},
160
+ {"role": "user", "content": user_content},
161
  ]
162
  text = tokenizer.apply_chat_template(
163
  messages, tokenize=False, add_generation_prompt=True
 
165
  return tokenizer(text, return_tensors="pt").to(model.device)
166
 
167
 
168
+ def build_inputs_ft(instruction: str, task: str):
169
+ """Fine-tuned path: task-specific system prompt + <TASK:X> tagged instruction,
170
+ matching the training data format exactly."""
171
+ system_prompt = TASK_SYSTEM_PROMPTS[task]
172
+ tagged_instruction = f"<TASK:{task}>\n\n{instruction}"
173
+ return _build_inputs(system_prompt, tagged_instruction)
174
+
175
+
176
+ def build_inputs_base(instruction: str):
177
+ """Base path: neutral system prompt, raw instruction, no tag."""
178
+ return _build_inputs(BASE_SYSTEM_PROMPT, instruction)
179
+
180
+
181
+ def _run_generate(inputs, max_new_tokens: int, temperature: float, use_sampling: bool):
182
  gen_kwargs = dict(
183
  **inputs,
184
  max_new_tokens=max_new_tokens,
185
  do_sample=use_sampling,
186
+ pad_token_id=tokenizer.pad_token_id,
187
  )
188
  if use_sampling:
189
  gen_kwargs["temperature"] = max(temperature, 0.01)
 
196
 
197
  new_tokens = output_ids[0][inputs["input_ids"].shape[1]:]
198
  text = tokenizer.decode(new_tokens, skip_special_tokens=True)
199
+ return text, elapsed, len(new_tokens)
200
+
201
+
202
+ def _warmup():
203
+ """Absorb CUDA context attach / kernel autotune cost with a throwaway
204
+ generation, so neither the FT nor the base timing eats the cold-start
205
+ penalty. Must run once per @spaces.GPU-decorated call, before any timed
206
+ generation."""
207
+ dummy = _build_inputs(BASE_SYSTEM_PROMPT, "print hello world")
208
+ with torch.no_grad():
209
+ model.generate(
210
+ **dummy,
211
+ max_new_tokens=WARMUP_TOKENS,
212
+ do_sample=False,
213
+ pad_token_id=tokenizer.pad_token_id,
214
+ )
215
 
216
 
217
  @spaces.GPU(duration=90)
218
+ def run_battle(instruction: str, task: str, max_new_tokens: int, temperature: float, use_sampling: bool):
219
  if not instruction or not instruction.strip():
220
  raise gr.Error("Please enter an instruction first.")
221
 
222
+ _warmup()
223
+
224
+ # Fine-tuned model (adapter active, task-specific prompt + tag)
225
+ ft_inputs = build_inputs_ft(instruction, task)
226
+ ft_text, ft_time, ft_tokens = _run_generate(ft_inputs, max_new_tokens, temperature, use_sampling)
227
 
228
+ # Base model (adapter disabled β€” same weights, no reload β€” neutral prompt)
229
+ base_inputs = build_inputs_base(instruction)
230
  with model.disable_adapter():
231
+ base_text, base_time, base_tokens = _run_generate(base_inputs, max_new_tokens, temperature, use_sampling)
232
 
233
  ft_metrics = analyze_response(ft_text, ft_time, ft_tokens)
234
  base_metrics = analyze_response(base_text, base_time, base_tokens)
 
235
  return ft_text, base_text, ft_metrics, base_metrics
236
 
237
 
 
242
  # against real test cases (e.g. HumanEval-style pass@1) β€” see the note in
243
  # the README for how to extend this.
244
  # ----------------------------------------------------------------------------
 
245
  CODE_BLOCK_RE = re.compile(r"```(?:python)?\s*\n(.*?)```", re.DOTALL)
246
 
247
 
 
264
  "num_classes": 0,
265
  "has_docstring": False,
266
  "lines_of_code": len([l for l in code.splitlines() if l.strip()]),
267
+ "raw_text": text,
268
  }
 
269
  try:
270
  tree = ast.parse(code)
271
  metrics["syntax_valid"] = True
 
289
  # Small speed bonus, capped, so a slow-but-correct answer isn't punished hard
290
  score += min(10, metrics["tokens_per_sec"] / 5)
291
  metrics["quality_score"] = round(min(score, 100), 1)
 
292
  return metrics
293
 
294
 
295
  # ----------------------------------------------------------------------------
296
  # UI RENDERING HELPERS
297
  # ----------------------------------------------------------------------------
 
298
  def metric_row(label, ft_val, base_val, higher_is_better=True, suffix=""):
299
  try:
300
  ft_num, base_num = float(ft_val), float(base_val)
 
302
  base_win = base_num > ft_num if higher_is_better else base_num < ft_num
303
  except (TypeError, ValueError):
304
  ft_win = base_win = False
 
305
  ft_cls = "metric-win" if ft_win else ""
306
  base_cls = "metric-win" if base_win else ""
307
  return f"""
 
315
  def render_comparison(ft_metrics: dict, base_metrics: dict) -> str:
316
  winner = "Fine-tuned" if ft_metrics["quality_score"] >= base_metrics["quality_score"] else "Base"
317
  winner_badge = f'<div class="winner-badge">πŸ† {winner} model scored higher</div>'
 
318
  rows = "".join([
319
  metric_row("Quality score", ft_metrics["quality_score"], base_metrics["quality_score"], suffix="/100"),
320
  metric_row("Syntax valid", "βœ… Yes" if ft_metrics["syntax_valid"] else "❌ No",
 
328
  higher_is_better=False, suffix="s"),
329
  metric_row("Tokens / sec", ft_metrics["tokens_per_sec"], base_metrics["tokens_per_sec"], suffix=" tok/s"),
330
  ])
 
331
  return f"""
332
  <div class="comparison-wrap">
333
  {winner_badge}
 
339
  </table>
340
  <div class="disclaimer">
341
  Quality score is a static-analysis heuristic (syntax validity, structure,
342
+ docstrings, speed) β€” not a guarantee of runtime correctness. Fine-tuned
343
+ gets its trained system prompt + &lt;TASK:{{task}}&gt; tag; base gets a
344
+ neutral prompt with no tag β€” matching how each was actually trained.
345
  </div>
346
  </div>"""
347
 
 
349
  # ----------------------------------------------------------------------------
350
  # CUSTOM STYLING
351
  # ----------------------------------------------------------------------------
 
352
  CUSTOM_CSS = """
353
  :root {
354
  --accent: #7c3aed;
355
  --accent-2: #06b6d4;
356
  }
357
  .gradio-container { max-width: 1200px !important; margin: auto; }
 
358
  #hero {
359
  background: linear-gradient(135deg, #1e1b4b 0%, #4c1d95 45%, #0e7490 100%);
360
  border-radius: 18px;
 
364
  }
365
  #hero h1 { margin: 0 0 6px 0; font-size: 1.7rem; }
366
  #hero p { margin: 0; opacity: 0.85; font-size: 0.95rem; }
 
367
  .model-card {
368
  border-radius: 14px;
369
  padding: 4px;
 
381
  }
382
  .label-ft { background: rgba(124,58,237,0.15); color: #a78bfa; }
383
  .label-base { background: rgba(148,163,184,0.15); color: #94a3b8; }
 
384
  .comparison-wrap { padding: 6px 4px; }
385
  .winner-badge {
386
  text-align: center;
 
401
  # ----------------------------------------------------------------------------
402
  # GRADIO APP
403
  # ----------------------------------------------------------------------------
 
404
  EXAMPLES = [
405
+ ["GENERATE", "Write a function that checks if a string is a valid palindrome, ignoring punctuation and case."],
406
+ ["GENERATE", "Implement an LRU cache from scratch without using functools.lru_cache."],
407
+ ["GENERATE", "Write a script that reads a CSV file and returns the top 5 rows by a given column, using pandas."],
408
+ ["GENERATE", "Create a decorator that retries a function up to N times with exponential backoff."],
409
+ ["DEBUG", "def add(a, b):\n return a + b\n\nprint(add(2))\n\n# This raises a TypeError. Find and fix the bug."],
410
+ ["REFACTOR", "def f(x):\n y=[]\n for i in range(len(x)):\n if x[i]%2==0:\n y.append(x[i])\n return y"],
411
  ]
412
 
413
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="violet", secondary_hue="cyan")) as demo:
414
  gr.HTML("""
415
  <div id="hero">
416
  <h1>⚑ Qwen Coder Battle</h1>
417
+ <p>Md-Asif/qwen-coder-python-ft (LoRA fine-tune) vs Qwen2.5-Coder-3B (base) β€” each prompted the way it was actually trained. Judge for yourself.</p>
418
  </div>
419
  """)
420
 
421
+ with gr.Row():
422
+ instruction = gr.Textbox(
423
+ label="Coding instruction",
424
+ placeholder="e.g. Write a function that merges two sorted lists in O(n) time.",
425
+ lines=3,
426
+ scale=3,
427
+ )
428
+ task = gr.Dropdown(
429
+ choices=["GENERATE", "REFACTOR", "DEBUG", "CODE_REVIEW"],
430
+ value="GENERATE",
431
+ label="Task type",
432
+ info="Must match the dataset's <TASK:X> categories for a fair fine-tuned comparison.",
433
+ scale=1,
434
+ )
435
 
436
  with gr.Row():
437
  run_btn = gr.Button("βš”οΈ Run the battle", variant="primary", scale=3)
 
441
  use_sampling = gr.Checkbox(value=False, label="Use sampling (off = greedy, fairest for comparison)")
442
  temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature (only if sampling is on)")
443
 
444
+ gr.Examples(examples=EXAMPLES, inputs=[task, instruction])
445
 
446
  with gr.Row():
447
  with gr.Column():
 
452
  base_output = gr.Code(language="python", label=None, lines=18)
453
 
454
  comparison_html = gr.HTML()
 
455
  ft_metrics_state = gr.State()
456
  base_metrics_state = gr.State()
457
 
458
+ def on_run(instr, task_choice, max_tok, temp, sample):
459
+ ft_text, base_text, ft_m, base_m = run_battle(instr, task_choice, max_tok, temp, sample)
460
  return ft_text, base_text, render_comparison(ft_m, base_m), ft_m, base_m
461
 
462
  run_btn.click(
463
  on_run,
464
+ inputs=[instruction, task, max_tokens, temperature, use_sampling],
465
  outputs=[ft_output, base_output, comparison_html, ft_metrics_state, base_metrics_state],
466
  )
467