This repository was archived by the owner on Jun 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.py
More file actions
52 lines (36 loc) · 1.24 KB
/
index.py
File metadata and controls
52 lines (36 loc) · 1.24 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
# -*- coding: utf-8 -*-
import sys
import six
import os
from flask import Flask, jsonify, request
from google.cloud import language_v1
from google.cloud.language_v1 import enums
app = Flask(__name__)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./credentials.json"
def analyze_sentiment(content):
client = language_v1.LanguageServiceClient()
if isinstance(content, six.binary_type):
content = content.decode("utf-8")
type_ = enums.Document.Type.PLAIN_TEXT
document = {
"language": "pt-BR",
"type": enums.Document.Type.PLAIN_TEXT,
"content": content,
}
response = client.analyze_sentiment(document)
sentiment = response.document_sentiment
return {
"Score": sentiment.score,
"Magnitude": sentiment.magnitude,
}
@app.route("/", methods=["GET", "POST"])
def hello():
if request.method == 'POST':
json_data = request.get_json(force=True)
return jsonify(analyze_sentiment(content=json_data['content']))
else:
return jsonify({"message": "POST at `/` with the following JSON object: `{content: string}`"})
def main():
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", "3000")), debug=False)
if __name__ == '__main__':
main()