mahesh1209 commited on
Commit
1e76c8b
·
verified ·
1 Parent(s): 25b6ff0

Update backend/app.py

Browse files
Files changed (1) hide show
  1. backend/app.py +5 -17
backend/app.py CHANGED
@@ -6,30 +6,23 @@ app = Flask(__name__)
6
  # --- Serve frontend files ---
7
  @app.route("/")
8
  def index():
9
- # Serve the main page
10
  return send_from_directory("/app/frontend", "index.html")
11
 
12
  @app.route("/<path:filename>")
13
  def static_files(filename):
14
- # Serve any file from the frontend folder
15
  return send_from_directory("/app/frontend", filename)
16
 
17
 
18
  # --- Moderation + Chat logic ---
19
-
20
- # In-memory per-session violation tracking
21
  violations = {} # session_id -> count
22
 
23
- # Banned words list
24
  BANNED_KEYWORDS = {"sex", "sexual", "sexy", "porn", "xxx"}
25
-
26
  BANNED_REGEXES = [
27
- re.compile(r"\bs\s*e\s*x\b", re.IGNORECASE), # s e x with spaces
28
- re.compile(r"se[x\W]+", re.IGNORECASE), # sex with trailing non-letters
29
  ]
30
 
31
- # Immediate lockout after first violation
32
- MAX_VIOLATIONS = 1
33
 
34
  def is_banned(text: str) -> bool:
35
  t = text.lower()
@@ -40,13 +33,12 @@ def is_banned(text: str) -> bool:
40
  return True
41
  return False
42
 
43
-
44
  @app.route("/moderate", methods=["POST"])
45
  def moderate():
46
  data = request.get_json(force=True)
47
  text = (data.get("text") or "").strip()
48
-
49
  session_id = request.headers.get("X-Session-Id", "")
 
50
  if not session_id:
51
  return jsonify({"allowed": False, "banned": True,
52
  "reason": "Missing session.", "lock": True}), 200
@@ -63,13 +55,12 @@ def moderate():
63
 
64
  return jsonify({"allowed": True, "banned": False}), 200
65
 
66
-
67
  @app.route("/chat", methods=["POST"])
68
  def chat():
69
  data = request.get_json(force=True)
70
  text = (data.get("text") or "").strip()
71
-
72
  session_id = request.headers.get("X-Session-Id", "")
 
73
  if not session_id:
74
  return jsonify({"reply": "Missing session. Access denied."}), 200
75
 
@@ -77,15 +68,12 @@ def chat():
77
  if count >= MAX_VIOLATIONS:
78
  return jsonify({"reply": "banned"}), 200
79
 
80
- # Minimal echo bot
81
  reply = f"You said: {text}"
82
  return jsonify({"reply": reply}), 200
83
 
84
-
85
  @app.route("/health")
86
  def health():
87
  return "Backend is running.", 200
88
 
89
-
90
  if __name__ == "__main__":
91
  app.run(host="0.0.0.0", port=7860)
 
6
  # --- Serve frontend files ---
7
  @app.route("/")
8
  def index():
 
9
  return send_from_directory("/app/frontend", "index.html")
10
 
11
  @app.route("/<path:filename>")
12
  def static_files(filename):
 
13
  return send_from_directory("/app/frontend", filename)
14
 
15
 
16
  # --- Moderation + Chat logic ---
 
 
17
  violations = {} # session_id -> count
18
 
 
19
  BANNED_KEYWORDS = {"sex", "sexual", "sexy", "porn", "xxx"}
 
20
  BANNED_REGEXES = [
21
+ re.compile(r"\bs\s*e\s*x\b", re.IGNORECASE),
22
+ re.compile(r"se[x\W]+", re.IGNORECASE),
23
  ]
24
 
25
+ MAX_VIOLATIONS = 1 # immediate lockout
 
26
 
27
  def is_banned(text: str) -> bool:
28
  t = text.lower()
 
33
  return True
34
  return False
35
 
 
36
  @app.route("/moderate", methods=["POST"])
37
  def moderate():
38
  data = request.get_json(force=True)
39
  text = (data.get("text") or "").strip()
 
40
  session_id = request.headers.get("X-Session-Id", "")
41
+
42
  if not session_id:
43
  return jsonify({"allowed": False, "banned": True,
44
  "reason": "Missing session.", "lock": True}), 200
 
55
 
56
  return jsonify({"allowed": True, "banned": False}), 200
57
 
 
58
  @app.route("/chat", methods=["POST"])
59
  def chat():
60
  data = request.get_json(force=True)
61
  text = (data.get("text") or "").strip()
 
62
  session_id = request.headers.get("X-Session-Id", "")
63
+
64
  if not session_id:
65
  return jsonify({"reply": "Missing session. Access denied."}), 200
66
 
 
68
  if count >= MAX_VIOLATIONS:
69
  return jsonify({"reply": "banned"}), 200
70
 
 
71
  reply = f"You said: {text}"
72
  return jsonify({"reply": reply}), 200
73
 
 
74
  @app.route("/health")
75
  def health():
76
  return "Backend is running.", 200
77
 
 
78
  if __name__ == "__main__":
79
  app.run(host="0.0.0.0", port=7860)