Anmol2507 commited on
Commit
ebf9258
·
verified ·
1 Parent(s): a1221a5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +213 -0
app.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import torch
3
+ import gradio as gr
4
+
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from peft import PeftModel
7
+
8
+
9
+ # ============================================================
10
+ # MODEL CONFIG
11
+ # ============================================================
12
+
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(
25
+ BASE_MODEL,
26
+ trust_remote_code=True
27
+ )
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):
59
+
60
+ messages = [
61
+ {
62
+ "role": "system",
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
+ )
77
+ },
78
+ {
79
+ "role": "user",
80
+ "content": (
81
+ f"Problem:\n{problem}\n\n"
82
+ f"Answer A:\n{answer_a}\n\n"
83
+ f"Answer B:\n{answer_b}\n\n"
84
+ "Evaluate both answers."
85
+ )
86
+ }
87
+ ]
88
+
89
+ return tokenizer.apply_chat_template(
90
+ messages,
91
+ tokenize=False,
92
+ add_generation_prompt=True
93
+ )
94
+
95
+
96
+ # ============================================================
97
+ # MODEL INFERENCE
98
+ # ============================================================
99
+
100
+ @spaces.GPU(duration=60)
101
+ def judge(problem, answer_a, answer_b):
102
+
103
+ if not problem.strip():
104
+ return '{"error":"Problem is required."}'
105
+
106
+ if not answer_a.strip():
107
+ return '{"error":"Answer A is required."}'
108
+
109
+ if not answer_b.strip():
110
+ return '{"error":"Answer B is required."}'
111
+
112
+ prompt = build_prompt(
113
+ problem,
114
+ answer_a,
115
+ answer_b
116
+ )
117
+
118
+ inputs = tokenizer(
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
+
130
+ with torch.inference_mode():
131
+
132
+ outputs = model.generate(
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,
145
+ skip_special_tokens=True
146
+ ).strip()
147
+
148
+ return response
149
+
150
+
151
+ # ============================================================
152
+ # GRADIO UI
153
+ # ============================================================
154
+
155
+ with gr.Blocks(
156
+ title="LLM as Judge"
157
+ ) as demo:
158
+
159
+ gr.Markdown(
160
+ """
161
+ # 🧑‍⚖️ LLM as Judge
162
+
163
+ Evaluate two answers to the same technical problem
164
+ using a fine-tuned Qwen2.5-7B model.
165
+ """
166
+ )
167
+
168
+ problem = gr.Textbox(
169
+ label="Problem",
170
+ placeholder="Enter the programming or technical problem...",
171
+ lines=6
172
+ )
173
+
174
+ with gr.Row():
175
+
176
+ answer_a = gr.Textbox(
177
+ label="Answer A",
178
+ placeholder="Enter Answer A...",
179
+ lines=12
180
+ )
181
+
182
+ answer_b = gr.Textbox(
183
+ label="Answer B",
184
+ placeholder="Enter Answer B...",
185
+ lines=12
186
+ )
187
+
188
+ judge_button = gr.Button(
189
+ "⚖️ Evaluate",
190
+ variant="primary"
191
+ )
192
+
193
+ result = gr.Code(
194
+ label="Judge Result",
195
+ language="json"
196
+ )
197
+
198
+ judge_button.click(
199
+ fn=judge,
200
+ inputs=[
201
+ problem,
202
+ answer_a,
203
+ answer_b
204
+ ],
205
+ outputs=result
206
+ )
207
+
208
+
209
+ # ============================================================
210
+ # LAUNCH
211
+ # ============================================================
212
+
213
+ demo.launch()