stmasson commited on
Commit
0723424
·
verified ·
1 Parent(s): 239205c

v2.0: Proper code extraction for base model

Browse files
Files changed (1) hide show
  1. scripts/eval_humaneval_hf.py +24 -2
scripts/eval_humaneval_hf.py CHANGED
@@ -5,6 +5,8 @@
5
  """
6
  HumanEval Evaluation: Base Devstral vs Fine-tuned Alizee-Coder
7
  Runs on HF Jobs with GPU support
 
 
8
  """
9
 
10
  import os
@@ -97,7 +99,7 @@ def extract_python_code(text):
97
  return text.strip()
98
 
99
  def generate_completion_base(model, tokenizer, prompt):
100
- """Generate code completion for BASE model (direct completion)"""
101
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
102
 
103
  with torch.no_grad():
@@ -110,7 +112,27 @@ def generate_completion_base(model, tokenizer, prompt):
110
  eos_token_id=tokenizer.eos_token_id,
111
  )
112
 
113
- completion = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # Stop at function boundary
116
  stop_tokens = ["\ndef ", "\nclass ", "\nif __name__", "\n\n\n"]
 
5
  """
6
  HumanEval Evaluation: Base Devstral vs Fine-tuned Alizee-Coder
7
  Runs on HF Jobs with GPU support
8
+
9
+ VERSION: 2.0 - Proper code extraction for both base and fine-tuned models
10
  """
11
 
12
  import os
 
99
  return text.strip()
100
 
101
  def generate_completion_base(model, tokenizer, prompt):
102
+ """Generate code completion for BASE model (handles chat-like responses)"""
103
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
104
 
105
  with torch.no_grad():
 
112
  eos_token_id=tokenizer.eos_token_id,
113
  )
114
 
115
+ raw_completion = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
116
+
117
+ # Try to extract code from ```python blocks (if model generates chat-like response)
118
+ completion = extract_python_code(raw_completion)
119
+
120
+ # If extracted code looks like a full function, extract just the body
121
+ if completion.strip().startswith("def "):
122
+ lines = completion.split('\n')
123
+ body_lines = []
124
+ in_function = False
125
+ for line in lines:
126
+ if line.strip().startswith("def "):
127
+ in_function = True
128
+ continue
129
+ if in_function:
130
+ body_lines.append(line)
131
+ if body_lines:
132
+ completion = '\n'.join(body_lines)
133
+ # If no code block, use raw completion
134
+ elif completion == raw_completion.strip():
135
+ completion = raw_completion
136
 
137
  # Stop at function boundary
138
  stop_tokens = ["\ndef ", "\nclass ", "\nif __name__", "\n\n\n"]