Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
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 |
-
|
| 17 |
-
DTYPE = torch.bfloat16
|
| 18 |
|
| 19 |
|
| 20 |
# ============================================================
|
| 21 |
-
#
|
| 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=
|
| 37 |
-
device_map=
|
| 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 |
-
#
|
| 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
|
| 67 |
-
"Focus primarily on correctness
|
| 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 |
-
"
|
| 72 |
-
'
|
| 73 |
-
'
|
| 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 |
-
#
|
| 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=
|
| 123 |
)
|
| 124 |
|
| 125 |
inputs = {
|
| 126 |
-
key: value.to(
|
| 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][
|
|
|
|
|
|
|
| 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 |
"""
|