-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
52 lines (46 loc) · 1.85 KB
/
sentiment.py
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
import os, requests, uuid, json
from dotenv import load_dotenv
def load_variables():
"""Load up env variables of the API key & location"""
env_var=load_dotenv('./variables.env')
auth_dict = {"text_analytics_key":os.environ['TEXT_ANALYTICS_KEY'],
"text_analytics_location":os.environ['TEXT_ANALYTICS_LOCATION'],
"text_analytics_name":os.environ['TEXT_ANALYTICS_NAME'],
}
return auth_dict
env_variables_dict = load_variables()
subscription_key = env_variables_dict['text_analytics_key']
location = env_variables_dict['text_analytics_location']
resource_name = env_variables_dict['text_analytics_name']
# Our Flask route will supply four arguments: input_text, input_language,
# output_text, output_language.
# When the run sentiment analysis button is pressed in our Flask app,
# the Ajax request will grab these values from our web app, and use them
# in the request. See main.js for Ajax calls.
def get_sentiment(input_text, input_language, output_text, output_language):
#base_url = 'https://westus.api.cognitive.microsoft.com/text/analytics'
base_url = f'https://{resource_name}.cognitiveservices.azure.com/text/analytics'
path = '/v3.1/sentiment'
constructed_url = base_url + path
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# You can pass more than one object in body.
body = {
'documents': [
{
'language': input_language,
'id': '1',
'text': input_text
},
{
'language': output_language,
'id': '2',
'text': output_text
}
]
}
response = requests.post(constructed_url, headers=headers, json=body)
return response.json()