danielferreira commited on
Commit
a788dcb
β€’
1 Parent(s): fef0bcc

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -1,23 +1,46 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
-
4
- pipeline = pipeline("text-classification", model="bhadresh-savani/bert-base-uncased-emotion", return_all_scores=True)
5
-
6
- def predict(input_text):
7
- preds = pipeline(input_text)
8
- pred= preds[0]
9
- res = {"Sadness 😭": pred[0]["score"],
10
- "Joy πŸ˜‚": pred[1]["score"],
11
- "Love 😍": pred[2]["score"],
12
- "Anger 😠": pred[3]["score"],
13
- "Fear 😨": pred[4]["score"],
14
- "Surprise 😲": pred[5]["score"],
 
 
 
 
 
 
 
 
 
 
15
  }
16
- return res
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- iface = gr.Interface(fn = predict,
19
- inputs = "text",
20
- outputs = gr.outputs.Label(num_top_classes=None, type="auto", label=None),
21
- title = 'Sentiment Analysis')
22
 
23
- iface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
3
+
4
+
5
+ class EmotionClassifier:
6
+ def __init__(self, model_name: str):
7
+ self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ self.pipeline = pipeline(
10
+ "text-classification",
11
+ model=self.model,
12
+ tokenizer=self.tokenizer,
13
+ return_all_scores=True,
14
+ )
15
+
16
+ def predict(self, input_text: str):
17
+ pred = self.pipeline(input_text)[0]
18
+ result = {
19
+ "Sadness 😭": pred[0]["score"],
20
+ "Joy πŸ˜‚": pred[1]["score"],
21
+ "Love 😍": pred[2]["score"],
22
+ "Anger 😠": pred[3]["score"],
23
+ "Fear 😨": pred[4]["score"],
24
+ "Surprise 😲": pred[5]["score"],
25
  }
26
+ return result
27
+
28
+
29
+ def main():
30
+ model = EmotionClassifier("bhadresh-savani/bert-base-uncased-emotion")
31
+ iface = gr.Interface(
32
+ fn=model.predict,
33
+ inputs=gr.inputs.Textbox(
34
+ lines=3,
35
+ placeholder="Type a phrase that has some emotion",
36
+ label="Input Text",
37
+ ),
38
+ outputs="label",
39
+ title="Emotion Classification",
40
+ )
41
+
42
+ iface.launch()
43
 
 
 
 
 
44
 
45
+ if __name__ == "__main__":
46
+ main()