File size: 9,864 Bytes
5af9683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b6a97
5af9683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da39393
5af9683
 
 
 
 
 
da39393
 
5af9683
 
 
 
 
 
 
da39393
5af9683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from flask import Flask, render_template, request, redirect, url_for, session, jsonify
from werkzeug.security import check_password_hash, generate_password_hash
from database import init_db, get_db
from ml_model import MLModel
from functools import wraps
from werkzeug.middleware.proxy_fix import ProxyFix
import os
import pandas as pd

app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY", "aims-secret-key")
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
app.config.update(
    PREFERRED_URL_SCHEME="https",
    SESSION_COOKIE_HTTPONLY=True,
    SESSION_COOKIE_SAMESITE="None" if os.environ.get("SPACE_ID") else "Lax",
    SESSION_COOKIE_SECURE=bool(os.environ.get("SPACE_ID")),
)

ml = MLModel()

TOPICS = [
    'Course', 'Internet', 'Food', 'Accommodation', 'Library',
    'Sports', 'Transportation', 'Administration', 'Health',
    'Security', 'Events', 'General', 'Other'
]

# INIT DB
init_db()

# ---------------- AUTH ----------------
def login_required(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        if "user_id" not in session:
            return redirect(url_for("login"))
        return f(*args, **kwargs)
    return wrapper

# ---------------- HOME ----------------
@app.route("/")
def index():
    return render_template("index.html")

# ---------------- LOGIN ----------------
@app.route("/login", methods=["GET", "POST"])
def login():
    error = None

    if "user_id" in session:
        return redirect(url_for("dashboard"))

    if request.method == "POST":
        username = request.form.get("username", "").strip()
        password = request.form.get("password", "")

        db = get_db()
        user = db.execute(
            "SELECT * FROM users WHERE username=?",
            (username,)
        ).fetchone()
        db.close()

        if user and check_password_hash(user["password"], password):
            session["user_id"] = user["id"]
            session["username"] = user["username"]
            return redirect(url_for("dashboard"))

        error = "Wrong credentials"

    return render_template("login.html", error=error)

# ---------------- REGISTER ----------------
@app.route("/register", methods=["GET", "POST"])
def register():
    error = None

    if request.method == "POST":
        username = request.form["username"]
        email = request.form["email"]
        password = request.form["password"]
        confirm_password = request.form.get("confirm_password", "")

        # Validate password confirmation
        if password != confirm_password:
            return render_template("register.html", error="Passwords do not match")

        # Validate password length
        if len(password) < 6:
            return render_template("register.html", error="Password must be at least 6 characters")

        db = get_db()

        exists = db.execute(
            "SELECT * FROM users WHERE username=? OR email=?",
            (username, email)
        ).fetchone()

        if exists:
            return render_template("register.html", error="User already exists")

        db.execute(
            "INSERT INTO users (username,email,password) VALUES (?,?,?)",
            (username, email, generate_password_hash(password))
        )
        db.commit()
        db.close()

        return redirect(url_for("login"))

    return render_template("register.html", error=error)

# ---------------- LOGOUT ----------------
@app.route("/logout")
def logout():
    session.clear()
    return redirect(url_for("index"))

# ---------------- DASHBOARD ----------------
@app.route("/dashboard")
@login_required
def dashboard():
    return render_template(
        "dashboard.html",
        topics=TOPICS,
        username=session.get("username")
    )

# ---------------- PREDICTION ----------------
@app.route("/prediction")
@login_required
def prediction():
    return render_template(
        "prediction.html",
        username=session.get("username")
    )

# ---------------- HISTORY ----------------
@app.route("/history")
@login_required
def history():
    db = get_db()
    feedbacks = db.execute(
        "SELECT * FROM feedbacks ORDER BY created_at DESC"
    ).fetchall()
    db.close()

    return render_template(
        "history.html",
        feedbacks=feedbacks,
        username=session.get("username")
    )

# Backward-compatible old profile URL
@app.route("/profile")
@login_required
def profile():
    return redirect(url_for("history"))

# ---------------- FEEDBACK ----------------
@app.route("/feedback", methods=["GET", "POST"])
def feedback():
    success = False

    if request.method == "POST":
        comment = request.form["comment"]
        submitted_topic = request.form.get("topic", "").strip()

        try:
            result = ml.predict(comment)
        except Exception as exc:
            return render_template("feedback.html", topics=TOPICS, success=False, error=str(exc))

        topic = submitted_topic if submitted_topic in TOPICS else result["topic"]

        db = get_db()
        db.execute("""
            INSERT INTO feedbacks
            (comment_text, topic, sentiment_label, sentiment_score)
            VALUES (?, ?, ?, ?)
        """, (
            comment,
            topic,
            result["sentiment_label"],
            result["sentiment_score"]
        ))
        db.commit()
        db.close()

        success = True

    return render_template("feedback.html", topics=TOPICS, success=success)

# ---------------- PREDICTION API ----------------
@app.route("/api/predict", methods=["POST"])
@login_required
def api_predict():
    payload = request.get_json(silent=True) or {}
    text = payload.get("text", "").strip()

    if not text:
        return jsonify({"error": "Text is required"}), 400

    try:
        result = ml.predict(text)
    except Exception as exc:
        return jsonify({"error": str(exc)}), 500

    db = get_db()
    cursor = db.execute("""
        INSERT INTO feedbacks
        (comment_text, topic, sentiment_label, sentiment_score)
        VALUES (?, ?, ?, ?)
    """, (
        text,
        result["topic"],
        result["sentiment_label"],
        result["sentiment_score"]
    ))
    db.commit()
    prediction_id = cursor.lastrowid
    db.close()

    result["id"] = prediction_id
    return jsonify(result)

# ---------------- CSV UPLOAD API ----------------
@app.route("/api/upload-csv", methods=["POST"])
@login_required
def api_upload_csv():
    uploaded = request.files.get("file")

    if not uploaded:
        return jsonify({"error": "CSV file is required"}), 400

    if not uploaded.filename.lower().endswith(".csv"):
        return jsonify({"error": "Only CSV files are supported"}), 400

    try:
        df = pd.read_csv(uploaded)
    except Exception:
        return jsonify({"error": "Could not read the CSV file"}), 400

    required_columns = {"comment"}
    if not required_columns.issubset(df.columns):
        return jsonify({"error": "CSV must contain a comment column"}), 400

    rows = []
    for _, row in df.iterrows():
        comment = str(row.get("comment", "")).strip()

        if not comment:
            continue

        try:
            result = ml.predict(comment)
        except Exception as exc:
            return jsonify({"error": str(exc)}), 500

        rows.append((
            comment,
            result["topic"],
            result["sentiment_label"],
            result["sentiment_score"]
        ))

    if rows:
        db = get_db()
        db.executemany("""
            INSERT INTO feedbacks
            (comment_text, topic, sentiment_label, sentiment_score)
            VALUES (?, ?, ?, ?)
        """, rows)
        db.commit()
        db.close()

    return jsonify({"processed": len(rows)})

# ---------------- DELETE FEEDBACK API ----------------
@app.route("/api/delete/<int:feedback_id>", methods=["POST"])
@login_required
def api_delete_feedback(feedback_id):
    db = get_db()
    db.execute("DELETE FROM feedbacks WHERE id=?", (feedback_id,))
    db.commit()
    db.close()

    return jsonify({"success": True})

# ---------------- DASHBOARD API ----------------
@app.route("/api/dashboard-data")
@login_required
def api_dashboard_data():
    db = get_db()
    query = "SELECT * FROM feedbacks WHERE topic IS NOT NULL AND topic != ''"
    params = []

    start_date = request.args.get("start_date", "").strip()
    end_date = request.args.get("end_date", "").strip()
    topic = request.args.get("topic", "").strip()
    sentiment = request.args.get("sentiment", "").strip()

    if start_date:
        query += " AND date(created_at) >= date(?)"
        params.append(start_date)

    if end_date:
        query += " AND date(created_at) <= date(?)"
        params.append(end_date)

    if topic and topic != "all":
        query += " AND topic = ?"
        params.append(topic)

    if sentiment and sentiment != "all":
        query += " AND sentiment_label = ?"
        params.append(sentiment)

    query += " ORDER BY created_at DESC"

    data = db.execute(query, params).fetchall()
    db.close()

    data = [dict(d) for d in data]

    total = len(data)
    pos = sum(1 for d in data if d["sentiment_label"] == "Positive")
    neu = sum(1 for d in data if d["sentiment_label"] == "Neutral")
    neg = sum(1 for d in data if d["sentiment_label"] == "Negative")

    topic_counts = {}
    for d in data:
        topic_counts[d["topic"]] = topic_counts.get(d["topic"], 0) + 1

    return jsonify({
        "total": total,
        "satisfaction": round(pos / total * 100, 1) if total else 0,
        "sentiment": {"Positive": pos, "Neutral": neu, "Negative": neg},
        "topics": topic_counts,
        "recent": data[:20]
    })

# ---------------- RUN ----------------
if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    app.run(host="0.0.0.0", port=port, debug=False)