Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer,AutoModelForSequenceClassification,Trainer,TrainingArguments
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
model_name = 'thanhcong2001/Sentiment'
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name,num_labels = 2)
|
| 8 |
+
|
| 9 |
+
# set up device
|
| 10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
+
model.to(device)
|
| 12 |
+
def pre_sentiment(sentence):
|
| 13 |
+
model.eval()
|
| 14 |
+
inputs = tokenizer(sentence,return_tensors='pt',max_length=64,padding=True,truncation=True)
|
| 15 |
+
inputs = {k:v for k,v in inputs.items()}
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
probs = F.softmax(outputs.logits,dim=-1)
|
| 19 |
+
pre = torch.argmax(probs,dim=-1).item()
|
| 20 |
+
score = torch.max(probs).item()
|
| 21 |
+
result = 'POSITIVE' if pre == 1 else 'NEGATIVE'
|
| 22 |
+
return f'Result: {result} - Score: {score}'
|
| 23 |
+
import gradio as gr
|
| 24 |
+
demo = gr.Interface(fn=pre_sentiment,inputs='text',outputs='text',title='Predict sentence')
|
| 25 |
+
demo.launch()
|