-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
37 lines (33 loc) · 1.37 KB
/
server.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
''' Executing this function initiates the application of sentiment
analysis to be executed over the Flask channel and deployed on
localhost:5000.
'''
# Import Flask, render_template, request from the flask pramework package : TODO
from flask import Flask, render_template, request
# Import the sentiment_analyzer function from the package created: TODO
from SentimentAnalysis.sentiment_analysis import sentiment_analyzer
#Initiate the flask app : TODO
app = Flask("Sentiment Analyzer")
@app.route("/sentimentAnalyzer")
def sent_analyzer():
''' This code receives the text from the HTML interface and
runs sentiment analysis over it using sentiment_analysis()
function. The output returned shows the label and its confidence
score for the provided text.
'''
text_to_analyze = request.args["textToAnalyze"]
response = sentiment_analyzer(text_to_analyze)
label = response['label']
score = response['score']
# Error handling
if label is None:
return "Invalid input ! Try again."
return f"The text is <b>{label.split('_')[1]}</b> with a score of <b>{score}</b>"
@app.route("/")
def render_index_page():
''' This function initiates the rendering of the main application
page over the Flask channel
'''
return render_template('index.html')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)