-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment.py
More file actions
62 lines (40 loc) · 1.74 KB
/
Copy pathsentiment.py
File metadata and controls
62 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
import json
def query(payload, model_id, api_token):
headers = {"Authorization": f"Bearer {api_token}"}
API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def perform_sentiment_analysis(text):
# Replace with the sentiment analysis model ID ------------------------------------
# model_id = "distilbert-base-uncased-finetuned-sst-2-english" # Positive/Negative
model_id = "tae898/emoberta-large" # Joy/Surprise/Neutral/Fear/Disgust/Sadness/Anger
# API Token Security
with open('api_tokens.json') as f:
data = json.load(f)
api_token = data["SENTIMENT_API_TOKEN"]
payload = {"inputs": text}
response = query(payload, model_id, api_token)
if isinstance(response, list) and len(response) > 0:
sentiments = response[0] # Extract sentiments from the response
highest_score = 0.0
predicted_sentiment = None
for sentiment in sentiments:
label = sentiment['label']
score = sentiment['score']
# For two sentiments
# if score > highest_score:
# highest_score = score
# predicted_sentiment = label
# For more sentiments
max_sentiment = max(sentiments, key=lambda x: x['score'])
predicted_sentiment = max_sentiment['label']
highest_score = max_sentiment['score']
return predicted_sentiment if predicted_sentiment else "neutral"
else:
return "Error: Sentiment analysis failed."
# Example usage:
# input_text = "ewwwww"
# temp = perform_sentiment_analysis(input_text)
# result = temp.lower()
# print(result)