Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,42 +51,21 @@ print("\n=== Model Loading ===")
|
|
| 51 |
chat_model_name = "sapienzanlp/Minerva-7B-instruct-v1.0"
|
| 52 |
cls_model_name = "saiteki-kai/QA-DeBERTa-v3-large-binary-3"
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
cls_tokenizer = AutoTokenizer.from_pretrained(cls_model_name)
|
| 59 |
-
|
| 60 |
-
chat_model = chat_model.to(device) # type: ignore
|
| 61 |
cls_model = cls_model.to(device)
|
| 62 |
|
| 63 |
-
def generate_responses(
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
message = [{"role": "user", "content": prompt}]
|
| 67 |
-
text = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
|
| 68 |
-
texts.append(text)
|
| 69 |
-
|
| 70 |
-
print(texts[0])
|
| 71 |
-
|
| 72 |
-
model_inputs = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors="pt").to(model.device)
|
| 73 |
-
|
| 74 |
-
print(tokenizer.decode(model_inputs["input_ids"][0]))
|
| 75 |
-
|
| 76 |
-
with torch.inference_mode():
|
| 77 |
-
generated_ids = model.generate(
|
| 78 |
-
**model_inputs,
|
| 79 |
-
do_sample=False,
|
| 80 |
-
temperature=0,
|
| 81 |
-
repetition_penalty=1.1,
|
| 82 |
-
max_new_tokens=512,
|
| 83 |
-
)
|
| 84 |
-
prompt_lengths = model_inputs["attention_mask"].sum(dim=1)
|
| 85 |
-
generated_ids = [output_ids[length:] for length, output_ids in zip(prompt_lengths, generated_ids)]
|
| 86 |
-
print(tokenizer.decode(generated_ids[0], skip_special_tokens=False))
|
| 87 |
-
responses = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
| 88 |
|
| 89 |
-
return responses
|
| 90 |
|
| 91 |
def classify_pairs(model, tokenizer, prompts, responses):
|
| 92 |
texts = [prompt + "[SEP]" + response for prompt, response in zip(prompts, responses)]
|
|
@@ -109,7 +88,7 @@ def generate(submission: list[dict[str, str]], team_id: str) -> list[dict[str, s
|
|
| 109 |
ids = [s["id"] for s in submission]
|
| 110 |
prompts = [s["prompt"] for s in submission]
|
| 111 |
|
| 112 |
-
responses = generate_responses(
|
| 113 |
print(responses)
|
| 114 |
|
| 115 |
scores = classify_pairs(cls_model, cls_tokenizer, prompts, responses)
|
|
|
|
| 51 |
chat_model_name = "sapienzanlp/Minerva-7B-instruct-v1.0"
|
| 52 |
cls_model_name = "saiteki-kai/QA-DeBERTa-v3-large-binary-3"
|
| 53 |
|
| 54 |
+
pipeline = transformers.pipeline(
|
| 55 |
+
model=chat_model_name,
|
| 56 |
+
model_kwargs={"dtype": torch.bfloat16},
|
| 57 |
+
device=device,
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
cls_model = AutoModelForSequenceClassification.from_pretrained(cls_model_name, dtype=torch.bfloat16)
|
| 61 |
cls_tokenizer = AutoTokenizer.from_pretrained(cls_model_name)
|
|
|
|
|
|
|
| 62 |
cls_model = cls_model.to(device)
|
| 63 |
|
| 64 |
+
def generate_responses(pipeline, tokenizer, prompts):
|
| 65 |
+
messages = [[{"role": "user", "content": prompt}] for prompt in prompts]
|
| 66 |
+
responses = pipeline(messages, do_sample=False, max_new_tokens=512, repetition_penalty=1.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return [response[0]["generated_text"][-1]["content"] for response in responses]
|
| 69 |
|
| 70 |
def classify_pairs(model, tokenizer, prompts, responses):
|
| 71 |
texts = [prompt + "[SEP]" + response for prompt, response in zip(prompts, responses)]
|
|
|
|
| 88 |
ids = [s["id"] for s in submission]
|
| 89 |
prompts = [s["prompt"] for s in submission]
|
| 90 |
|
| 91 |
+
responses = generate_responses(pipeline, chat_tokenizer, prompts)
|
| 92 |
print(responses)
|
| 93 |
|
| 94 |
scores = classify_pairs(cls_model, cls_tokenizer, prompts, responses)
|