trnt commited on
Commit
5653972
1 Parent(s): a81ae9a

Edit model root from spaces

Browse files
Files changed (2) hide show
  1. app.py +32 -9
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,21 +1,44 @@
1
  import gradio as gr
 
2
  from transformers import AutoModelForSequenceClassification
3
  from transformers import AutoTokenizer
4
  from transformers import pipeline
5
 
6
- model_loader = AutoModelForSequenceClassification.from_pretrained("content/models")
7
- tokenizer_loader = AutoTokenizer.from_pretrained("content/models")
8
- model_loader.eval()
9
- print("loaded")
 
 
 
 
 
10
 
11
- classifier = pipeline("sentiment-analysis", model=model_loader, tokenizer=tokenizer_loader, device=0)
 
 
 
12
 
13
 
14
- def greet(twitter):
15
- pred = classifier(twitter)[0]
16
- return "twitter is %s with score=%.4f" % (pred['label'], pred['score'])
 
 
 
 
 
 
 
17
 
18
 
19
  if __name__ == '__main__':
20
- interFace = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
21
  interFace.launch()
 
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 = True
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()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  transformers
2
- gradio
 
 
1
  transformers
2
+ gradio
3
+ torch