-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
68 lines (53 loc) · 1.79 KB
/
predict.py
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
67
68
import os
import torch
import argparse
import pandas as pd
from transformers import BertForSequenceClassification, BertTokenizer
model = BertForSequenceClassification.from_pretrained("./results/checkpoint")
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
label2id = {
"keyword search": 0,
"applet description": 1,
"generic problem description": 2,
}
id2label = {v: k for k, v in label2id.items()}
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
prog="Prompt Classifier", usage="%(prog)s [options]"
)
argparser.add_argument(
"-i", "--input", default="input/mock.csv", help="Input csv filename"
)
argparser.add_argument(
"-o", "--output", default="output", help="Output file directory"
)
try:
args = argparser.parse_args()
filename = args.input
if not filename.endswith(".csv"):
raise NameError("Not supported file format")
df = pd.read_csv(filename)
prompts = df["prompt"].to_list()
inputs = tokenizer(
prompts,
padding="max_length",
truncation=True,
return_tensors="pt",
max_length=128,
)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predictions = logits.argmax(dim=-1).cpu().numpy()
outputs = []
for prompt, pred in zip(prompts, predictions):
outputs.append({"prompt": prompt, "kind": id2label[pred]})
df = pd.DataFrame(outputs)
df.to_csv(
os.path.join(
args.output, os.path.basename(filename).split(".")[0] + "_out.csv"
),
index=False,
)
except Exception as e:
print("Error while inferencing\n{}".format(e))