Spaces:
Sleeping
Sleeping
Download app.py from Pavan97/emotion_analysis_new: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Pavan97/emotion_analysis_new/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pavan97/emotion_analysis_new/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pavan97/emotion_analysis_new/resolve/main/app.py
1.63 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import torch | |
| from transformers import RobertaForSequenceClassification, RobertaTokenizer | |
| id2label = { | |
| "0": "admiration", | |
| "1": "amusement", | |
| "2": "anger", | |
| "3": "annoyance", | |
| "4": "approval", | |
| "5": "caring", | |
| "6": "confusion", | |
| "7": "curiosity", | |
| "8": "desire", | |
| "9": "disappointment", | |
| "10": "disapproval", | |
| "11": "disgust", | |
| "12": "embarrassment", | |
| "13": "excitement", | |
| "14": "fear", | |
| "15": "gratitude", | |
| "16": "grief", | |
| "17": "joy", | |
| "18": "love", | |
| "19": "nervousness", | |
| "20": "optimism", | |
| "21": "pride", | |
| "22": "realization", | |
| "23": "relief", | |
| "24": "remorse", | |
| "25": "sadness", | |
| "26": "surprise", | |
| "27": "neutral" | |
| } | |
| def predict(text): | |
| model = RobertaForSequenceClassification.from_pretrained("Pavan97/emotion_analysis_new", num_labels = 28) | |
| tokenizer = RobertaTokenizer.from_pretrained("roberta-base") | |
| tokenized_text = tokenizer(text, return_tensors = 'pt') | |
| with torch.no_grad(): | |
| labels = model(**tokenized_text) | |
| labels = torch.nn.functional.softmax(labels.logits[0], dim = -1) | |
| predicted_labels = {} | |
| for idx, label in enumerate(labels): | |
| predicted_labels[id2label[str(idx)]] = label.numpy().item() | |
| return predicted_labels | |
| iface = gr.Interface(fn = predict, | |
| inputs = "text", | |
| outputs = gr.Label(num_top_classes=5), | |
| title = 'Emotion Analysis', | |
| description = "Get the emotion of the input text",) | |
| iface.launch(inline = False) |