-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pipeline.py
34 lines (29 loc) · 983 Bytes
/
test_pipeline.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
from multilabel_pipeline import MultiLabelPipeline
from transformers import ElectraTokenizer
from model import ElectraForMultiLabelClassification
from pprint import pprint
import torch
import pickle
import streamlit as st
@st.cache_resource
def load_model():
with open("./tokenizer.pkl", "rb") as f:
tokenizer = pickle.load(f)
model_path = "./Model"
model = ElectraForMultiLabelClassification.from_pretrained(model_path)
return tokenizer, model
def predict(text, tokenizer, model):
inputs = tokenizer(text,return_tensors="pt")
outputs = model(**inputs)
scores = 1 / (1 + torch.exp(-outputs[0]))
threshold = 0
result = []
for item in scores:
labels = []
scores = []
for idx, s in enumerate(item):
if s > threshold:
labels.append(model.config.id2label[idx])
scores.append(s.item())
result.append({"labels": labels, "scores": scores})
return result