Anmol2507 commited on
Commit
15b03f2
·
verified ·
1 Parent(s): 6727016

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -13,12 +13,11 @@ from peft import PeftModel
13
  BASE_MODEL = "Qwen/Qwen2.5-7B-Instruct"
14
  ADAPTER_MODEL = "Anmol2507/LLM_as_judge_fine_tuned_Qwen_7B"
15
 
16
- DEVICE = "cuda"
17
- DTYPE = torch.bfloat16
18
 
19
 
20
  # ============================================================
21
- # LOAD TOKENIZER
22
  # ============================================================
23
 
24
  tokenizer = AutoTokenizer.from_pretrained(
@@ -28,31 +27,38 @@ tokenizer = AutoTokenizer.from_pretrained(
28
 
29
 
30
  # ============================================================
31
- # LOAD BASE MODEL
32
  # ============================================================
33
 
34
  base_model = AutoModelForCausalLM.from_pretrained(
35
  BASE_MODEL,
36
- torch_dtype=DTYPE,
37
- device_map=DEVICE,
38
  trust_remote_code=True
39
  )
40
 
41
 
42
  # ============================================================
43
- # LOAD LoRA ADAPTER
44
  # ============================================================
45
 
46
  model = PeftModel.from_pretrained(
47
  base_model,
48
- ADAPTER_MODEL
 
49
  )
50
 
 
 
 
 
 
 
51
  model.eval()
52
 
53
 
54
  # ============================================================
55
- # JUDGE PROMPT
56
  # ============================================================
57
 
58
  def build_prompt(problem, answer_a, answer_b):
@@ -63,14 +69,13 @@ def build_prompt(problem, answer_a, answer_b):
63
  "content": (
64
  "You are an expert AI judge evaluating two answers "
65
  "to the same programming or technical problem.\n\n"
66
- "Determine which answer is correct.\n"
67
- "Focus primarily on correctness, logical validity, "
68
- "and whether the answer actually solves the problem.\n\n"
69
  "Return ONLY valid JSON using exactly this format:\n"
70
  '{"A":"correct","B":"incorrect"}\n\n'
71
- "Possible labels are only:\n"
72
- '- "correct"\n'
73
- '- "incorrect"\n\n'
74
  "If both answers are correct, mark both as correct.\n"
75
  "If both answers are incorrect, mark both as incorrect."
76
  )
@@ -94,7 +99,7 @@ def build_prompt(problem, answer_a, answer_b):
94
 
95
 
96
  # ============================================================
97
- # MODEL INFERENCE
98
  # ============================================================
99
 
100
  @spaces.GPU(duration=60)
@@ -119,11 +124,11 @@ def judge(problem, answer_a, answer_b):
119
  prompt,
120
  return_tensors="pt",
121
  truncation=True,
122
- max_length=768
123
  )
124
 
125
  inputs = {
126
- key: value.to(DEVICE)
127
  for key, value in inputs.items()
128
  }
129
 
@@ -133,12 +138,12 @@ def judge(problem, answer_a, answer_b):
133
  **inputs,
134
  max_new_tokens=32,
135
  do_sample=False,
136
- temperature=None,
137
- top_p=None,
138
  pad_token_id=tokenizer.eos_token_id
139
  )
140
 
141
- generated_tokens = outputs[0][inputs["input_ids"].shape[1]:]
 
 
142
 
143
  response = tokenizer.decode(
144
  generated_tokens,
@@ -152,9 +157,7 @@ def judge(problem, answer_a, answer_b):
152
  # GRADIO UI
153
  # ============================================================
154
 
155
- with gr.Blocks(
156
- title="LLM as Judge"
157
- ) as demo:
158
 
159
  gr.Markdown(
160
  """
 
13
  BASE_MODEL = "Qwen/Qwen2.5-7B-Instruct"
14
  ADAPTER_MODEL = "Anmol2507/LLM_as_judge_fine_tuned_Qwen_7B"
15
 
16
+ MAX_LENGTH = 768
 
17
 
18
 
19
  # ============================================================
20
+ # TOKENIZER
21
  # ============================================================
22
 
23
  tokenizer = AutoTokenizer.from_pretrained(
 
27
 
28
 
29
  # ============================================================
30
+ # LOAD BASE MODEL ON CPU
31
  # ============================================================
32
 
33
  base_model = AutoModelForCausalLM.from_pretrained(
34
  BASE_MODEL,
35
+ torch_dtype=torch.bfloat16,
36
+ device_map="cpu",
37
  trust_remote_code=True
38
  )
39
 
40
 
41
  # ============================================================
42
+ # LOAD LoRA ADAPTER ON CPU
43
  # ============================================================
44
 
45
  model = PeftModel.from_pretrained(
46
  base_model,
47
+ ADAPTER_MODEL,
48
+ device_map="cpu"
49
  )
50
 
51
+
52
+ # ============================================================
53
+ # REGISTER MODEL WITH ZEROGPU
54
+ # ============================================================
55
+
56
+ model = model.to("cuda")
57
  model.eval()
58
 
59
 
60
  # ============================================================
61
+ # PROMPT
62
  # ============================================================
63
 
64
  def build_prompt(problem, answer_a, answer_b):
 
69
  "content": (
70
  "You are an expert AI judge evaluating two answers "
71
  "to the same programming or technical problem.\n\n"
72
+ "Determine whether each answer is correct.\n"
73
+ "Focus primarily on correctness and logical validity.\n\n"
 
74
  "Return ONLY valid JSON using exactly this format:\n"
75
  '{"A":"correct","B":"incorrect"}\n\n'
76
+ "Allowed labels:\n"
77
+ '"correct"\n'
78
+ '"incorrect"\n\n'
79
  "If both answers are correct, mark both as correct.\n"
80
  "If both answers are incorrect, mark both as incorrect."
81
  )
 
99
 
100
 
101
  # ============================================================
102
+ # INFERENCE
103
  # ============================================================
104
 
105
  @spaces.GPU(duration=60)
 
124
  prompt,
125
  return_tensors="pt",
126
  truncation=True,
127
+ max_length=MAX_LENGTH
128
  )
129
 
130
  inputs = {
131
+ key: value.to("cuda")
132
  for key, value in inputs.items()
133
  }
134
 
 
138
  **inputs,
139
  max_new_tokens=32,
140
  do_sample=False,
 
 
141
  pad_token_id=tokenizer.eos_token_id
142
  )
143
 
144
+ generated_tokens = outputs[0][
145
+ inputs["input_ids"].shape[1]:
146
+ ]
147
 
148
  response = tokenizer.decode(
149
  generated_tokens,
 
157
  # GRADIO UI
158
  # ============================================================
159
 
160
+ with gr.Blocks(title="LLM as Judge") as demo:
 
 
161
 
162
  gr.Markdown(
163
  """