Download functions.py from petermutwiri/Movie_Review_Application: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/spaces/petermutwiri/Movie_Review_Application/resolve/main/functions.py
- Command line
-
hf download hf://spaces/petermutwiri/Movie_Review_Application/functions.py
-
curl -L -o functions.py https://huggingface.co/spaces/petermutwiri/Movie_Review_Application/resolve/main/functions.py
1.59 kB
| from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification | |
| from scipy.special import softmax | |
| import emoji | |
| # Define the preprocess function | |
| def preprocess(text): | |
| new_text = [] | |
| for t in text.split(" "): | |
| # Check if the token is an emoji | |
| if emoji.is_emoji(t): | |
| # Add the emoji to the new text list | |
| new_text.append(t) | |
| else: | |
| # Check if the token starts with '@' and has more than one character | |
| if t.startswith('@') and len(t) > 1: | |
| # Replace the token with '@user' | |
| t = '@user' | |
| # Check if the token starts with 'http' | |
| if t.startswith('http'): | |
| # Replace the token with 'http' | |
| t = 'http' | |
| # Add the processed token to the new text list | |
| new_text.append(t) | |
| return " ".join(new_text) | |
| # Define the sentiment_analysis function | |
| def sentiment_analysis(text, tokenizer, model): | |
| text = preprocess(text) | |
| encoded_input = tokenizer(text, return_tensors='pt') | |
| output = model(**encoded_input) | |
| scores_ = output[0][0].detach().numpy() | |
| scores_ = softmax(scores_) | |
| labels = ['Negative', 'Positive'] | |
| scores = {l: float(s) for (l, s) in zip(labels, scores_)} | |
| return scores | |
| # Define the map_sentiment_score_to_rating function | |
| def map_sentiment_score_to_rating(score): | |
| min_score = 0.0 | |
| max_score = 1.0 | |
| min_rating = 1 | |
| max_rating = 10 | |
| rating = ((score - min_score) / (max_score - min_score)) * (max_rating - min_rating) + min_rating | |
| return rating |