Maryam-1 commited on
Commit
f4b35df
·
1 Parent(s): 2b10013

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -9,27 +9,36 @@ subprocess.run(["pip", "install", "-r", "requirements.txt"])
9
  import gradio as gr
10
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
11
 
12
-
13
  # Load the model from Hugging Face Model Hub
14
  model_name = "SamLowe/roberta-base-go_emotions"
15
  tokenizer = AutoTokenizer.from_pretrained(model_name)
16
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
17
 
 
 
 
 
 
 
 
 
18
  def predict_emotion(text):
19
  inputs = tokenizer(text, return_tensors="pt")
20
  outputs = model(**inputs)
21
  logits = outputs.logits
22
  predicted_class = logits.argmax().item()
23
 
24
- return {"emotion_label": predicted_class}
 
25
 
26
  iface = gr.Interface(
27
  fn=predict_emotion,
28
  inputs=gr.Textbox(),
29
- outputs="label",
30
  live=True,
31
  title="Emotion Prediction",
32
  description="Enter a sentence for emotion prediction.",
33
  )
34
 
35
  iface.launch()
 
 
9
  import gradio as gr
10
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
11
 
 
12
  # Load the model from Hugging Face Model Hub
13
  model_name = "SamLowe/roberta-base-go_emotions"
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
16
 
17
+ # Define emotion labels used by the model
18
+ emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
19
+ "caring", "confusion", "curiosity", "desire", "disappointment",
20
+ "disapproval", "disgust", "embarrassment", "excitement",
21
+ "fear", "gratitude", "grief", "joy", "love", "nervousness",
22
+ "optimism", "pride", "realization", "relief", "remorse",
23
+ "sadness", "surprise", "neutral"]
24
+
25
  def predict_emotion(text):
26
  inputs = tokenizer(text, return_tensors="pt")
27
  outputs = model(**inputs)
28
  logits = outputs.logits
29
  predicted_class = logits.argmax().item()
30
 
31
+ predicted_emotion = emotion_labels[predicted_class]
32
+ return {"predicted_emotion": predicted_emotion}
33
 
34
  iface = gr.Interface(
35
  fn=predict_emotion,
36
  inputs=gr.Textbox(),
37
+ outputs="text",
38
  live=True,
39
  title="Emotion Prediction",
40
  description="Enter a sentence for emotion prediction.",
41
  )
42
 
43
  iface.launch()
44
+