Ploypatcha commited on
Commit
aafc4c1
·
verified ·
1 Parent(s): 53f57e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import numpy as np
5
+
6
+ model_name = "Ploypatcha/my-model-upload"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ labels = ["happy", "love", "angry", "sadness", "fear", "trust", "disgust", "surprise", "anticipation", "optimism", "pessimism"]
11
+
12
+ def predict(text):
13
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ probs = torch.sigmoid(outputs.logits).numpy()[0]
17
+ results = {labels[i]: float(np.round(probs[i], 3)) for i in range(len(labels))}
18
+ return results
19
+
20
+ gr.Interface(fn=predict, inputs=gr.Textbox(label="Enter english comment"), outputs="label").launch()