-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
54 lines (44 loc) · 1.92 KB
/
app.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
53
54
# This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
from dotenv import load_dotenv
load_dotenv()
import requests, os, uuid, json
from flask import Flask, redirect, url_for, request, render_template, session
app = Flask(__name__)
language_key = os.getenv("LANGUAGE_KEY")
language_endpoint = os.getenv("LANGUAGE_ENDPOINT")
def authenticate_client():
ta_credential = AzureKeyCredential(language_key)
text_analytics_client = TextAnalyticsClient(
endpoint=language_endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/', methods=['POST'])
def index_post():
# Read the values from the form
original_text = request.form['text']
documents = [original_text]
result = client.analyze_sentiment(documents, show_opinion_mining=True)
doc_result = [doc for doc in result if not doc.is_error]
positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"]
negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"]
positive_mined_opinions = []
mixed_mined_opinions = []
negative_mined_opinions = []
for document in doc_result:
sentiment = document.sentiment
positive_confidence_scores = document.confidence_scores.positive
negative_confidence_scores = document.confidence_scores.negative
neutral_confidence_scores = document.confidence_scores.neutral
return render_template(
'results.html',
sentiment = sentiment,
positive_confidence_scores = positive_confidence_scores,
negative_confidence_scores = negative_confidence_scores,
neutral_confidence_scores = neutral_confidence_scores
)