-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis.py
More file actions
34 lines (26 loc) · 969 Bytes
/
Copy pathsentiment_analysis.py
File metadata and controls
34 lines (26 loc) · 969 Bytes
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
import urllib.request
import json
import ssl
from config import API_KEY, MODEL_URL
import os
def allow_self_signed_https(allowed):
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
ssl._create_default_https_context = ssl._create_unverified_context
def get_sentiment(text):
allow_self_signed_https(True)
data = {"inputs": text}
body = str.encode(json.dumps(data))
headers = {
'Content-Type': 'application/json',
'Authorization': ('Bearer ' + API_KEY),
'azureml-model-deployment': 'main'
}
req = urllib.request.Request(MODEL_URL, body, headers)
try:
response = urllib.request.urlopen(req)
result = response.read()
sentiment_output = json.loads(result)
sentiment = sentiment_output[0]['label']
return sentiment
except urllib.error.HTTPError as error:
return {"error": str(error.code)}