khadija3818 commited on
Commit
14c7e0f
β€’
1 Parent(s): e1aecaf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSequenceClassification
4
+ from transformers import AutoTokenizer
5
+ from transformers import pipeline
6
+
7
+ model_path = "trnt/twitter_emotions"
8
+ is_gpu = False
9
+ device = torch.device('cuda') if is_gpu else torch.device('cpu')
10
+ print(device)
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
13
+ model.to(device)
14
+ model.eval()
15
+ print("Model was loaded")
16
+
17
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=is_gpu-1)
18
+ emotions = {'LABEL_0': 'sadness', 'LABEL_1': 'joy', 'LABEL_2': 'love', 'LABEL_3': 'anger', 'LABEL_4': 'fear',
19
+ 'LABEL_5': 'surprise'}
20
+ examples = ["I love you!", "I hate you!"]
21
+
22
+
23
+ def predict(twitter):
24
+ pred = classifier(twitter, return_all_scores=True)[0]
25
+ res = {"Sadness": pred[0]["score"],
26
+ "Joy": pred[1]["score"],
27
+ "Love": pred[2]["score"],
28
+ "Anger": pred[3]["score"],
29
+ "Fear": pred[4]["score"],
30
+ "Surprise": pred[5]["score"]}
31
+ # "This tweet is %s with probability=%.2f" % (emotions[pred['label']], 100 * pred['score']) + "%"
32
+ return res
33
+
34
+
35
+ if __name__ == '__main__':
36
+ interFace = gr.Interface(fn=predict,
37
+ inputs=gr.inputs.Textbox(placeholder="Enter a tweet here", label="Tweet content", lines=5),
38
+ outputs=gr.outputs.Label(num_top_classes=6, label="Emotions of this tweet is "),
39
+ verbose=True,
40
+ examples=examples,
41
+ title="Emotions of English tweet",
42
+ description="",
43
+ theme="grass")
44
+ interFace.launch()