My project is following: we have some product shop and customers are going to choose any product and write a comment, we might have hundreds of comments per day and the manager of the shop decided to analyze those comments, determine their polarity and return whether it is positive or negative and also show those result graphically in order to keep his buisness active and effective.
The project consists of two parts:
Part 1: we have to build a webpage which shows possibility for users to write a comment
Part 2: using python and flask we should be able to connect form, retrieve those comments and using transformers determine their polarity
First part and second part was succesfully implemented using following codes.
First one is html part:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sell Weapons</title>
</head>
<body>
<form action="{{ url_for('predict')}}"method="post">
<label for="comment1">evaluate first product</label><br>
<input type="text" id="comment1" name="comment1" required="required"><br>
<label for="comment2">evaluate second product</label><br>
<input type="text" id="comment2" name="comment2" required="required"><br>
<label for="comment3">evaluate third product</label><br>
<input type="text" id="comment3" name="comment3" required="required"><br>
<input type="submit" value="Submit">
</form>
<h4 style="color:Violet;">
{{ prediction_text }} </h4>
</body>
</html>
Result is: filling form
Based on this form, I have created following app.py file:
import numpy as np
from flask import Flask,request,jsonify,render_template
import pickle
from transformers import pipeline
from collections import Counter
import matplotlib.pyplot as plt
sentiment_pipeline = pipeline("sentiment-analysis")
app =Flask(__name__)
@app.route('/')
def home():
return render_template("Commenting.html")
@app.route('/predict',methods=['POST'])
# @app.route('/')
def predict():
text = [x for x in request.form.values()]
print(text)
status = []
for sentence in text:
print(sentence)
status.append(sentiment_pipeline(sentence)[0]['label'])
print(status)
# data = {'Task': 'Hours per Day', 'Work': 22, 'Eat': 4, 'Commute': 6, 'Watching TV': 5, 'Sleeping': 15}
data = Counter(status)
print(data)
data ={"Positive":list(data.values())[0],"Negative":list(data.values())[1]}
positive_text =list(data .keys())[0]
positive_frequency =list(data .values())[0]
negative_text = list(data.keys())[1]
negative_frequency = list(data.values())[1]
total =positive_frequency+negative_frequency
text_sho =(f'{positive_text} takes {positive_frequency/(positive_frequency+negative_frequency)*100 } % '
f'and {negative_text} takes {negative_frequency/(positive_frequency+negative_frequency)*100} %')
# sentiment_pipeline = pipeline("sentiment-analysis")
# status = []
# for sentence in text:
# status.append(sentiment_pipeline(sentence)[0]['label'])
# # print(sentiment_pipeline(sentence)[0])
# emotion_counter = Counter(status)
# plt.pie([float(v) for v in emotion_counter.values()], labels=[k for k in emotion_counter],
# autopct="%2.3f%%")
# plt.savefig("templates/sentiment_distribution.png")
# plt.show()
# values =[float(v) for v in emotion_counter.values()]
# labels = [k for k in emotion_counter]
#
# text=list(text)
return render_template('Commenting.html',prediction_text=text_sho)
# sentiment_pipeline = pipeline("sentiment-analysis")
# result =sentiment_pipeline(text)[0]
# if result['label']=='POSITIVE':
# return render_template('Commenting.html',prediction_text=f'emotion of comment is positive')
# else:
# return render_template('Commenting.html', prediction_text=f'emotion of comment is not positive')
if __name__ =="__main__":
app.run()
When we run it, we get the following result: result
How can I generate a chart? I use several chart.js files, like this: chart
But how to apply it in my project?
Your question title is not very relevant to the question you are asking. Also flask has little to do with charts except serving the generated content through it.
Showing charts to the user could be accomplished by;
Depending on your choice, you have to adjust your app to serve the static content. One approach might be creating an image and saving that image in the static folder and then using flask's send_from_directory
function to show it in the html.
Js libraries all differ, so you'll need to read their documentation and integrate them with flask.