reset to previous upload method
Browse files
main.py
CHANGED
@@ -29,75 +29,79 @@ model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
29 |
|
30 |
model.eval()
|
31 |
|
|
|
32 |
# Function to Predict Sentiment
|
33 |
def predict_sentiment(text):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
@app.route('/')
|
46 |
def upload_file():
|
47 |
return render_template('upload.html')
|
48 |
|
|
|
49 |
@app.route('/analyze_text', methods=['POST'])
|
50 |
def analyze_text():
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
return render_template('upload.html', sentiment=
|
55 |
|
56 |
-
sentiment = predict_sentiment(text)
|
57 |
-
return render_template('upload.html', sentiment=sentiment)
|
58 |
|
59 |
-
@app.route('/uploader', methods=['POST'])
|
60 |
def upload_file_post():
|
61 |
-
if '
|
62 |
-
|
63 |
-
|
64 |
-
f = request.files['file']
|
65 |
-
if f.filename == '':
|
66 |
-
return "Error: No file selected!", 400
|
67 |
-
|
68 |
-
try:
|
69 |
data = pd.read_csv(f)
|
70 |
|
71 |
-
# Ensure 'review' column exists
|
72 |
-
if 'review' not in data.columns:
|
73 |
-
return "Error: CSV file must contain a 'review' column!", 400
|
74 |
-
|
75 |
# Predict sentiment for each review
|
76 |
-
data['sentiment'] = data['review'].
|
77 |
|
78 |
-
#
|
79 |
sentiment_counts = data['sentiment'].value_counts().to_dict()
|
80 |
summary = f"Total Reviews: {len(data)}<br>" \
|
81 |
f"Positive: {sentiment_counts.get('Positive', 0)}<br>" \
|
82 |
f"Negative: {sentiment_counts.get('Negative', 0)}<br>"
|
83 |
|
84 |
-
# Generate
|
85 |
fig, ax = plt.subplots()
|
86 |
ax.bar(sentiment_counts.keys(), sentiment_counts.values(), color=['red', 'blue'])
|
87 |
ax.set_ylabel('Counts')
|
88 |
ax.set_title('Sentiment Analysis Summary')
|
89 |
-
|
90 |
-
# Save plot
|
91 |
img = BytesIO()
|
92 |
plt.savefig(img, format='png', bbox_inches='tight')
|
93 |
img.seek(0)
|
|
|
|
|
94 |
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
|
|
|
|
|
95 |
plt.close(fig)
|
96 |
|
97 |
return render_template('result.html', tables=[data.to_html(classes='data')], titles=data.columns.values, summary=summary, plot_url=plot_url)
|
98 |
|
99 |
-
|
100 |
-
return f"Error processing file: {str(e)}", 500
|
101 |
|
102 |
if __name__ == '__main__':
|
103 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
|
|
|
|
29 |
|
30 |
model.eval()
|
31 |
|
32 |
+
|
33 |
# Function to Predict Sentiment
|
34 |
def predict_sentiment(text):
|
35 |
+
# Split the text into chunks of 512 tokens
|
36 |
+
tokenized_text = tokenizer.encode(text, add_special_tokens=True)
|
37 |
+
chunks = [tokenized_text[i:i + 512] for i in range(0, len(tokenized_text), 512)]
|
38 |
+
|
39 |
+
# Predict sentiment for each chunk
|
40 |
+
sentiments = []
|
41 |
+
for chunk in chunks:
|
42 |
+
# inputs = tokenizer.decode(chunk, skip_special_tokens=True)
|
43 |
+
inputs = tokenizer.decode(chunk, skip_special_tokens=True, clean_up_tokenization_spaces=True) # Explicitly set clean_up_tokenization_spaces
|
44 |
+
inputs = tokenizer(inputs, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
45 |
+
with torch.no_grad():
|
46 |
+
outputs = model(**inputs)
|
47 |
+
sentiments.append(outputs.logits.argmax(dim=1).item())
|
48 |
+
|
49 |
+
# Aggregate the predictions (majority voting)
|
50 |
+
sentiment_counts = Counter(sentiments)
|
51 |
+
majority_sentiment = sentiment_counts.most_common(1)[0][0]
|
52 |
+
return 'Positive' if majority_sentiment == 1 else 'Negative'
|
53 |
|
54 |
@app.route('/')
|
55 |
def upload_file():
|
56 |
return render_template('upload.html')
|
57 |
|
58 |
+
|
59 |
@app.route('/analyze_text', methods=['POST'])
|
60 |
def analyze_text():
|
61 |
+
if request.method == 'POST':
|
62 |
+
text = request.form['text']
|
63 |
+
sentiment = predict_sentiment(text)
|
64 |
+
return render_template('upload.html', sentiment=sentiment)
|
65 |
|
|
|
|
|
66 |
|
67 |
+
@app.route('/uploader', methods=['GET', 'POST'])
|
68 |
def upload_file_post():
|
69 |
+
if request.method == 'POST':
|
70 |
+
f = request.files['file']
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
data = pd.read_csv(f)
|
72 |
|
|
|
|
|
|
|
|
|
73 |
# Predict sentiment for each review
|
74 |
+
data['sentiment'] = data['review'].apply(predict_sentiment)
|
75 |
|
76 |
+
# Sentiment Analysis Summary
|
77 |
sentiment_counts = data['sentiment'].value_counts().to_dict()
|
78 |
summary = f"Total Reviews: {len(data)}<br>" \
|
79 |
f"Positive: {sentiment_counts.get('Positive', 0)}<br>" \
|
80 |
f"Negative: {sentiment_counts.get('Negative', 0)}<br>"
|
81 |
|
82 |
+
# Generate plot
|
83 |
fig, ax = plt.subplots()
|
84 |
ax.bar(sentiment_counts.keys(), sentiment_counts.values(), color=['red', 'blue'])
|
85 |
ax.set_ylabel('Counts')
|
86 |
ax.set_title('Sentiment Analysis Summary')
|
87 |
+
|
88 |
+
# Save the plot to a BytesIO object
|
89 |
img = BytesIO()
|
90 |
plt.savefig(img, format='png', bbox_inches='tight')
|
91 |
img.seek(0)
|
92 |
+
|
93 |
+
# Encode the image in base64 and decode it to UTF-8
|
94 |
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
|
95 |
+
|
96 |
+
# Close the plot to free memory
|
97 |
plt.close(fig)
|
98 |
|
99 |
return render_template('result.html', tables=[data.to_html(classes='data')], titles=data.columns.values, summary=summary, plot_url=plot_url)
|
100 |
|
101 |
+
|
|
|
102 |
|
103 |
if __name__ == '__main__':
|
104 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
105 |
+
|
106 |
+
|
107 |
+
|