Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers.models.bert import BertTokenizer, BertForSequenceClassification
|
6 |
+
path='./roberta-base-cold'
|
7 |
+
vocab='vocab.txt'
|
8 |
+
tokenizer = BertTokenizer.from_pretrained(os.path.join(path,vocab))
|
9 |
+
model = BertForSequenceClassification.from_pretrained('roberta-base-cold')
|
10 |
+
model.eval()
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def get_output(text):
|
15 |
+
output=[]
|
16 |
+
model_input = tokenizer(text, return_tensors="pt", padding=True)
|
17 |
+
model_output = model(**model_input, return_dict=False)
|
18 |
+
prediction = torch.argmax(model_output[0].cpu(), dim=-1)
|
19 |
+
prediction = [p.item() for p in prediction]
|
20 |
+
for i in range(len(prediction)):
|
21 |
+
if prediction[i]==0:
|
22 |
+
output.append("消极")
|
23 |
+
else:
|
24 |
+
output.append('积极')
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
return output
|
29 |
+
|
30 |
+
demo=gr.Interface(fn=get_output,inputs='text',outputs='text')
|
31 |
+
demo.launch()
|