-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (39 loc) · 1.78 KB
/
Copy pathserver.py
File metadata and controls
43 lines (39 loc) · 1.78 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
''' 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 :
from flask import Flask, request, render_template
# Import the sentiment_analyzer function from the package created:
from SentimentAnalysis.sentiment_analysis import sentiment_analyzer
#Initiate the flask app :
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.
'''
# Retrieve the text to analyze from the request arguments
text_to_analyze = request.args.get('textToAnalyze')
# Pass the text to the sentiment_analyzer function and store the response
response = sentiment_analyzer(text_to_analyze)
# Extract the label and score from the response
label = response['label']
score = response['score']
# Check if the label is None, indicating an error or invalid input
if label is None:
return "Invalid input! Try again."
else:
# Return a formatted string with the sentiment label and score
return "The given text has been identified as {} with a score of {}.".format(label.split('_')[1], score)
@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__":
''' This functions executes the flask app and deploys it on localhost:5000'''
app.run(host="0.0.0.0", port=5000)