|
import os |
|
|
|
import gradio as gr |
|
import torch |
|
from transformers.models.bert import BertTokenizer, BertForSequenceClassification |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("sundea/text1") |
|
model = AutoModelForSequenceClassification.from_pretrained("sundea/text1") |
|
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() |