add progress bar
Browse files
main.py
CHANGED
@@ -9,6 +9,13 @@ matplotlib.use('Agg') # Prevents GUI issues for Matplotlib
|
|
9 |
import matplotlib.pyplot as plt
|
10 |
import base64
|
11 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Fix Permission Issues: Set Writable Directories for Hugging Face & Matplotlib
|
14 |
os.environ["HF_HOME"] = "/tmp"
|
@@ -29,79 +36,82 @@ model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
29 |
|
30 |
model.eval()
|
31 |
|
32 |
-
|
33 |
# Function to Predict Sentiment
|
34 |
def predict_sentiment(text):
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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 |
-
|
62 |
-
|
63 |
-
|
64 |
-
return render_template('upload.html', sentiment=
|
65 |
|
|
|
|
|
66 |
|
67 |
-
@app.route('/uploader', methods=['
|
68 |
def upload_file_post():
|
69 |
-
if request.
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
data = pd.read_csv(f)
|
72 |
|
|
|
|
|
|
|
|
|
73 |
# Predict sentiment for each review
|
74 |
-
data['sentiment'] = data['review'].apply(predict_sentiment)
|
75 |
|
76 |
-
#
|
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
|
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 |
-
|
|
|
9 |
import matplotlib.pyplot as plt
|
10 |
import base64
|
11 |
from io import BytesIO
|
12 |
+
from flask import send_file
|
13 |
+
|
14 |
+
|
15 |
+
# Ensure the file exists in the current directory
|
16 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
17 |
+
FILE_PATH = os.path.join(BASE_DIR, "Student_Feedback_Dataset__20_Rows_.csv")
|
18 |
+
|
19 |
|
20 |
# Fix Permission Issues: Set Writable Directories for Hugging Face & Matplotlib
|
21 |
os.environ["HF_HOME"] = "/tmp"
|
|
|
36 |
|
37 |
model.eval()
|
38 |
|
|
|
39 |
# Function to Predict Sentiment
|
40 |
def predict_sentiment(text):
|
41 |
+
if not text.strip():
|
42 |
+
return "Neutral" # Avoid processing empty text
|
43 |
+
|
44 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
45 |
+
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = model(**inputs)
|
48 |
+
|
49 |
+
sentiment = outputs.logits.argmax(dim=1).item()
|
50 |
+
return "Positive" if sentiment == 1 else "Negative"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
@app.route('/')
|
53 |
def upload_file():
|
54 |
return render_template('upload.html')
|
55 |
|
56 |
+
@app.route('/download-sample')
|
57 |
+
def download_sample():
|
58 |
+
if os.path.exists(FILE_PATH):
|
59 |
+
return send_file(FILE_PATH, as_attachment=True)
|
60 |
+
else:
|
61 |
+
return "Error: File not found!", 404
|
62 |
|
63 |
@app.route('/analyze_text', methods=['POST'])
|
64 |
def analyze_text():
|
65 |
+
text = request.form.get('text', '').strip()
|
66 |
+
|
67 |
+
if not text:
|
68 |
+
return render_template('upload.html', sentiment="Error: No text provided!")
|
69 |
|
70 |
+
sentiment = predict_sentiment(text)
|
71 |
+
return render_template('upload.html', sentiment=sentiment)
|
72 |
|
73 |
+
@app.route('/uploader', methods=['POST'])
|
74 |
def upload_file_post():
|
75 |
+
if 'file' not in request.files:
|
76 |
+
return "Error: No file uploaded!", 400
|
77 |
+
|
78 |
+
f = request.files['file']
|
79 |
+
if f.filename == '':
|
80 |
+
return "Error: No file selected!", 400
|
81 |
+
|
82 |
+
try:
|
83 |
data = pd.read_csv(f)
|
84 |
|
85 |
+
# Ensure 'review' column exists
|
86 |
+
if 'review' not in data.columns:
|
87 |
+
return "Error: CSV file must contain a 'review' column!", 400
|
88 |
+
|
89 |
# Predict sentiment for each review
|
90 |
+
data['sentiment'] = data['review'].astype(str).apply(predict_sentiment)
|
91 |
|
92 |
+
# Generate summary
|
93 |
sentiment_counts = data['sentiment'].value_counts().to_dict()
|
94 |
summary = f"Total Reviews: {len(data)}<br>" \
|
95 |
f"Positive: {sentiment_counts.get('Positive', 0)}<br>" \
|
96 |
f"Negative: {sentiment_counts.get('Negative', 0)}<br>"
|
97 |
|
98 |
+
# Generate sentiment plot
|
99 |
fig, ax = plt.subplots()
|
100 |
ax.bar(sentiment_counts.keys(), sentiment_counts.values(), color=['red', 'blue'])
|
101 |
ax.set_ylabel('Counts')
|
102 |
ax.set_title('Sentiment Analysis Summary')
|
103 |
+
|
104 |
+
# Save plot as an image
|
105 |
img = BytesIO()
|
106 |
plt.savefig(img, format='png', bbox_inches='tight')
|
107 |
img.seek(0)
|
|
|
|
|
108 |
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
|
|
|
|
|
109 |
plt.close(fig)
|
110 |
|
111 |
return render_template('result.html', tables=[data.to_html(classes='data')], titles=data.columns.values, summary=summary, plot_url=plot_url)
|
112 |
|
113 |
+
except Exception as e:
|
114 |
+
return f"Error processing file: {str(e)}", 500
|
115 |
|
116 |
if __name__ == '__main__':
|
117 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
|
|