barbieheimer commited on
Commit
d768c4b
1 Parent(s): 9c5a3d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -29
app.py CHANGED
@@ -1,30 +1,51 @@
1
- # here are some examples for sadness, joy, anger, and optimism.
2
  import gradio as gr
3
-
4
- # Load model directly
5
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
- from transformers import pipeline
7
-
8
- tokenizer = AutoTokenizer.from_pretrained("barbieheimer/MND_TweetEvalBert_model")
9
- model_load = AutoModelForSequenceClassification.from_pretrained("barbieheimer/MND_TweetEvalBert_model")
10
-
11
- # We can now use the model in the pipeline.
12
- classifier = pipeline("text-classification", model=model_load, tokenizer=tokenizer)
13
-
14
-
15
- def predict(prompt):
16
- completion = classifier(prompt)
17
- return completion[0]["label"], completion[0]["score"]
18
-
19
- examples = [
20
- ["The movie was a bummer."],
21
- ["I cannot wait to watch all these movies!"],
22
- ["The ending of the movie really irks me, gives me the ick fr."],
23
- ["The protagonist seems to have a lot of hope...."]
24
- ]
25
-
26
- model = "models/barbieheimer/MND_TweetEvalBert_model"
27
-
28
- gr.Interface.load("models/barbieheimer/MND_TweetEvalBert_model", fn=predict, title="Sentiment Analysis", examples=examples,
29
- inputs=gr.inputs.Textbox(lines=5, label="Type a Sentence here."),
30
- outputs=[gr.outputs.Textbox(label="Label"),gr.outputs.Textbox(label="Score")],).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
3
+
4
+ class EmotionClassifier:
5
+ def __init__(self, model_name: str):
6
+ self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ self.pipeline = pipeline(
9
+ "text-classification",
10
+ model=self.model,
11
+ tokenizer=self.tokenizer,
12
+ return_all_scores=True,
13
+ )
14
+
15
+
16
+ def predict(self, input_text: str):
17
+ pred = self.pipeline(input_text)[0]
18
+ result = {
19
+ "Anger 😠": pred[0]["score"],
20
+ "Joy 😂": pred[1]["score"],
21
+ "Optimism 😍": pred[2]["score"],
22
+ "Anger 😭": pred[3]["score"],
23
+ }
24
+ return result
25
+
26
+
27
+ def main():
28
+ model = EmotionClassifier("models/barbieheimer/MND_TweetEvalBert_model")
29
+ iface = gr.Interface(
30
+ fn=model.predict,
31
+ inputs=gr.inputs.Textbox(
32
+ lines=3,
33
+ placeholder="Type a phrase that has some emotion",
34
+ label="Input Text",
35
+ ),
36
+ outputs="label",
37
+ title="Emotion Classification",
38
+ examples=[
39
+ "I get so down when I'm alone",
40
+ "I believe that today everything will work out",
41
+ "It was so dark there I was afraid to go",
42
+ "I loved the gift you gave me",
43
+ "I was very surprised by your presentation.",
44
+ ],
45
+ )
46
+
47
+ iface.launch()
48
+
49
+
50
+ if __name__ == "__main__":
51
+ main()