Spaces:
Running
Running
Download app.py from SaraHossam/SentimentAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/SaraHossam/SentimentAnalysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/SaraHossam/SentimentAnalysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SaraHossam/SentimentAnalysis/resolve/main/app.py
1.01 kB
| import gradio as gr | |
| # from transformers import pipeline | |
| from transformers import AutoTokenizer | |
| from transformers import AutoModelForSequenceClassification | |
| from scipy.special import softmax | |
| MODEL = f"cardiffnlp/twitter-roberta-base-sentiment" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL) | |
| def polarity_scores_roberta(example): | |
| encoded_text = tokenizer(example, return_tensors='pt') | |
| output = model(**encoded_text) | |
| scores = output[0][0].detach().numpy() | |
| scores = softmax(scores) | |
| scores_dict = { | |
| 'roberta_neg' : scores[0], | |
| 'roberta_neu' : scores[1], | |
| 'roberta_pos' : scores[2] | |
| } | |
| x=max(scores[0],scores[1],scores[2]) | |
| if x==scores[0]: | |
| return 'Negative' | |
| elif x==scores[1]: | |
| return 'Neutral' | |
| else: | |
| return 'Positive' | |
| def greet(name): | |
| return "Hello " + name + "!!" | |
| iface = gr.Interface(fn=polarity_scores_roberta, inputs="text", outputs="text") | |
| iface.launch() |