-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdemo_v1.py
More file actions
67 lines (51 loc) · 2.02 KB
/
demo_v1.py
File metadata and controls
67 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModelForSequenceClassification
class NAIPv1:
def __init__(self, model_path: str, device: str = "cuda", max_length: int = 512):
self.device = torch.device(device if torch.cuda.is_available() else "cpu")
self.max_length = max_length
# Load model
self.model = AutoModelForSequenceClassification.from_pretrained(
model_path,
num_labels=1,
load_in_8bit=True
).to(self.device).eval()
# Load tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
# ⚠️ DO NOT CHANGE PROMPT
self.prompt_template = (
f'''Given a certain paper, Title: {title}\n Abstract: {abstract}. \n Predict its normalized academic impact (between 0 and 1):'''
)
def score(self, title: str, abstract: str) -> float:
"""Return predicted impact score (between 0 and 1)."""
text = self.prompt_template.format(
title=title.strip().replace("\n", ""),
abstract=abstract.strip().replace("\n", "")
)
# Tokenize
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=self.max_length
).to(self.device)
# Inference
with torch.no_grad():
outputs = self.model(**inputs)
score = torch.sigmoid(outputs["logits"]).item()
return score
if __name__ == "__main__":
model_path = r"path_to_the_v1_dir"
scorer = NAIPv1(model_path=model_path, device="cuda")
print("🎯 Enter paper title and abstract. Press Ctrl+C to quit.\n")
while True:
try:
title = input("Enter a title: ")
abstract = input("Enter an abstract: ")
score = scorer.score(title, abstract)
print(f"🔮 Predicted Impact Score: {score:.4f}\n")
except KeyboardInterrupt:
print("\nExiting.")
break