-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (57 loc) · 2.27 KB
/
Copy pathapp.py
File metadata and controls
78 lines (57 loc) · 2.27 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
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
"""Flask web app exposing the student data analysis chatbot.
Run locally:
python app.py # then open http://localhost:5000
For PythonAnywhere, point the WSGI configuration at wsgi.py instead.
"""
import os
import secrets
from flask import Flask, jsonify, render_template, request
from chatbot.engine import MAX_INPUT_LENGTH, respond
app = Flask(__name__)
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", secrets.token_hex(32))
@app.after_request
def add_security_headers(response):
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["Referrer-Policy"] = "no-referrer"
return response
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/suggestions")
def suggestions():
"""Curated example questions shown as quick-reply chips in the UI."""
return jsonify(
{
"suggestions": [
"How many students are there?",
"Who has the highest total score?",
"Who has the lowest total score?",
"What is the average total score?",
"How many female students are there?",
"How many male students are there?",
"Who has the highest attendance?",
"Who has the lowest attendance?",
"What is the average quiz score?",
"How many students got grade A?",
"What percentage of students passed?",
"Show the grade distribution",
]
}
)
@app.route("/api/chat", methods=["POST"])
def chat():
"""Handle one chat message. Returns JSON: {"reply": "..."}."""
data = request.get_json(silent=True) or {}
message = data.get("message")
if not isinstance(message, str) or not message.strip():
return jsonify({"error": "Message must not be empty."}), 400
if len(message) > MAX_INPUT_LENGTH:
return jsonify({"error": f"Message too long (maximum {MAX_INPUT_LENGTH} characters)."}), 400
return jsonify({"reply": respond(message)})
@app.route("/health")
def health():
return jsonify({"status": "ok"})
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=False)