|
import os |
|
|
|
import gradio as gr |
|
import torch |
|
from transformers.models.bert import BertTokenizer, BertForSequenceClassification |
|
path='/home/user/app' |
|
vocab='vocab.txt' |
|
tokenizer = BertTokenizer.from_pretrained(os.path.join(path,vocab)) |
|
model = BertForSequenceClassification.from_pretrained(path) |
|
model.eval() |
|
|
|
|
|
|
|
def get_output(text): |
|
output=[] |
|
model_input = tokenizer(text, return_tensors="pt", padding=True) |
|
model_output = model(**model_input, return_dict=False) |
|
prediction = torch.argmax(model_output[0].cpu(), dim=-1) |
|
prediction = [p.item() for p in prediction] |
|
for i in range(len(prediction)): |
|
if prediction[i]==0: |
|
output.append("消极") |
|
else: |
|
output.append('积极') |
|
|
|
|
|
|
|
return output |
|
|
|
demo=gr.Interface(fn=get_output,inputs='text',outputs='text') |
|
demo.launch() |