Created
May 26, 2023 21:32
-
-
Save sisyphusSmiling/51901b38a049ae4f0e76d3f6f73507bf to your computer and use it in GitHub Desktop.
Analyzes tweet content sentiment using named model, providing sentiment label and score
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
# Add the model ID and your Hugging Face Token | |
HF_TOKEN = "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" | |
# MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest" | |
SENTIMENT_MODEL = "cardiffnlp/twitter-xlm-roberta-base-sentiment" | |
# Create the API request | |
API_BASE = "https://api-inference.huggingface.co/models/" | |
SENTIMENT_API_URL = API_BASE + SENTIMENT_MODEL | |
headers = {"Authorization": "Bearer %s" % (HF_TOKEN)} | |
# Preprocess text (username and link placeholders) | |
def preprocess(text): | |
new_text = [] | |
for t in text.split(" "): | |
t = '@user' if t.startswith('@') and len(t) > 1 else t | |
t = 'http' if t.startswith('http') else t | |
new_text.append(t) | |
return " ".join(new_text) | |
# Returns sentiment analysis on the given payload | |
def analysis(payload): | |
response = requests.post(SENTIMENT_API_URL, headers=headers, json=payload) | |
while "is currently loading" in str(response.json()): # retry request while model loads | |
print(response.json()) | |
time.sleep(30) | |
response = requests.post(SENTIMENT_API_URL, headers=headers, json=payload) | |
return response.json() | |
# Do the request to the Inference API and process the resutls | |
input = preprocess(str(input_data['input_data'])) | |
result = analysis({"inputs": input}) | |
prelim_scores = result[0] | |
# Get the scores | |
neutral = prelim_scores[0]['score'] | |
positive = prelim_scores[1]['score'] | |
negative = prelim_scores[2]['score'] | |
scores = [{'label': 'negative', 'score': negative}, {'label': 'neutral', 'score': neutral}, {'label': 'positive', 'score': positive}] | |
top_score = max(item['score'] for item in scores) | |
top_sentiment = next(item['label'] for item in scores if item["score"] == top_score) | |
# Define the output for Zapier | |
output = [{'sentiment_label': top_sentiment, 'sentiment_score': top_score}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment