forked from Chloe22204/pawpingphils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
223 lines (195 loc) Β· 9.97 KB
/
app.py
File metadata and controls
223 lines (195 loc) Β· 9.97 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
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
import os
import json
import glob
import re
from flask import Flask, request, jsonify, session, render_template, send_from_directory
from pathlib import Path
from datetime import datetime
app = Flask(__name__)
app.secret_key = "pab_secret_key_2026"
PROFILE_PATH = Path("user_profile.json")
UPLOAD_FOLDER = Path("recordings")
UPLOAD_FOLDER.mkdir(exist_ok=True)
# ββ single source of truth for the dispatch lock status string β
DISPATCH_LOCK_STATUS = "dispatched_scdf"
def load_users() -> list:
with open(PROFILE_PATH, "r", encoding="utf-8") as f:
return json.load(f)["users"]
def find_user(user_id: str) -> dict:
for user in load_users():
if user["id"] == user_id:
return user
return {}
def is_dispatch_locked(user_id: str) -> bool:
"""Returns True if any of this user's alerts has dispatched_scdf status."""
for status_file in UPLOAD_FOLDER.glob(f"PAB_Alert_{user_id}_*.status"):
if status_file.read_text(encoding="utf-8").strip() == DISPATCH_LOCK_STATUS:
return True
return False
# ββ parse a risk report .txt into a structured dict ββββββββββ
def parse_risk_report(report_path: Path):
try:
text = report_path.read_text(encoding="utf-8")
data = {}
for line in text.splitlines():
if line.strip().startswith("Priority"):
raw = line.split(":", 1)[-1].strip()
if "CRITICAL" in raw: data["urgency"] = "HIGH"
elif "HIGH" in raw: data["urgency"] = "HIGH"
elif "MEDIUM" in raw: data["urgency"] = "MODERATE"
elif "LOW" in raw: data["urgency"] = "LOW"
else: data["urgency"] = "LOW"
data["priority_raw"] = raw
elif line.strip().startswith("Timestamp"):
data["timestamp"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("File"):
data["audioFile"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Name"):
data["name"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Age"):
try: data["age"] = int(line.split(":", 1)[-1].strip())
except: data["age"] = 0
elif line.strip().startswith("Address"):
data["address"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Contact"):
data["contact"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Emergency Contact"):
data["emergency_contact"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Medical History"):
data["conditions"] = [c.strip() for c in line.split(":", 1)[-1].split(",")]
elif line.strip().startswith("CRITICAL :"):
data["keywords_critical"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("HIGH :"):
data["keywords_high"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("MEDIUM :"):
data["keywords_medium"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("LLM Reasoning"):
data["llm_note"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Final Priority"):
data["triage_source"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Decision Source"):
data["decision_source"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Rule Priority"):
data["rule_priority"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("LLM Priority"):
data["llm_priority"] = line.split(":", 1)[-1].strip()
elif line.strip().startswith("Flags"):
data["triage_flags"] = line.split(":", 1)[-1].strip()
# Fallback parsing for triage fields (handles spacing/label variants).
def pull(label_regex: str) -> str | None:
m = re.search(label_regex, text, flags=re.IGNORECASE | re.MULTILINE)
return m.group(1).strip() if m else None
data["llm_note"] = data.get("llm_note") or pull(r"^\s*LLM\s+Reasoning\s*:\s*(.+)$") or pull(r"^\s*LLM\s+Note\s*:\s*(.+)$")
data["decision_source"] = data.get("decision_source") or pull(r"^\s*Decision\s+Source\s*:\s*(.+)$")
data["rule_priority"] = data.get("rule_priority") or pull(r"^\s*Rule\s+Priority\s*:\s*(.+)$")
data["llm_priority"] = data.get("llm_priority") or pull(r"^\s*LLM\s+Priority\s*:\s*(.+)$")
data["triage_flags"] = data.get("triage_flags") or pull(r"^\s*Flags\s*:\s*(.+)$")
if "TRANSCRIPT" in text and "KEYWORDS" in text:
transcript_block = text.split("TRANSCRIPT")[1].split("KEYWORDS")[0]
lines = [l.strip() for l in transcript_block.splitlines() if l.strip() and "---" not in l]
data["transcript"] = " ".join(lines)
kw_parts = []
if data.get("keywords_critical"): kw_parts.append(f"π΄ {data['keywords_critical']}")
if data.get("keywords_high"): kw_parts.append(f"π {data['keywords_high']}")
if data.get("keywords_medium"): kw_parts.append(f"π‘ {data['keywords_medium']}")
data["keywords_summary"] = " | ".join(kw_parts) if kw_parts else "None detected"
stem = report_path.stem.replace("_risk_report", "")
data["id"] = stem[-8:].upper()
status_path = report_path.with_suffix(".status")
if status_path.exists():
data["status"] = status_path.read_text(encoding="utf-8").strip() or "pending"
else:
data["status"] = "pending"
data["report_file"] = str(report_path)
ts = data.get("timestamp", "")
try:
dt = datetime.strptime(ts, "%Y-%m-%d %H:%M:%S")
data["time"] = dt.strftime("%I:%M %p")
data["elapsed_seconds"] = int((datetime.now() - dt).total_seconds())
except:
data["time"] = ts
data["elapsed_seconds"] = 0
return data if data.get("name") else None
except Exception as e:
print(f"Error parsing {report_path}: {e}")
return None
# ββ API: get all alerts βββββββββββββββββββββββββββββββββββββββ
@app.route("/api/alerts", methods=["GET"])
def get_alerts():
reports = sorted(UPLOAD_FOLDER.glob("*_risk_report.txt"), reverse=True)
alerts = []
for r in reports:
parsed = parse_risk_report(r)
if parsed:
alerts.append(parsed)
return jsonify(alerts)
# ββ API: update alert status ββββββββββββββββββββββββββββββββββ
@app.route("/api/alerts/status", methods=["POST"])
def update_status():
data = request.json
report_file = data.get("report_file")
new_status = data.get("status")
status_path = Path(report_file).with_suffix(".status")
status_path.write_text(new_status)
return jsonify({"success": True})
# ββ API: check if SCDF is dispatched for logged-in user βββββββ
@app.route("/api/check-dispatch", methods=["GET"])
def check_dispatch():
user_id = session.get("user_id")
if not user_id:
return jsonify({"blocked": False})
return jsonify({"blocked": is_dispatch_locked(user_id)})
# ββ API: upload audio (blocked if SCDF dispatched) ββββββββββββ
@app.route("/api/upload", methods=["POST"])
def upload_audio():
user_id = session.get("user_id")
if not user_id:
return jsonify({"error": "Not logged in"}), 401
# block recording if SCDF already dispatched for this user
if is_dispatch_locked(user_id):
return jsonify({"error": "SCDF dispatched", "blocked_by_dispatch": True}), 423
audio = request.files.get("audio")
if not audio:
return jsonify({"error": "No audio file received"}), 400
filename = f"PAB_Alert_{user_id}_{audio.filename}"
save_path = UPLOAD_FOLDER / filename
audio.save(save_path)
meta_path = save_path.with_suffix(".meta.json")
user = find_user(user_id)
with open(meta_path, "w", encoding="utf-8") as f:
json.dump({k: v for k, v in user.items() if k != "password"}, f, indent=2)
return jsonify({"success": True, "file": filename})
# ββ serve pages βββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/responder")
def responder():
return render_template("responder.html")
@app.route("/")
def index():
return render_template("pab-emergency.html")
# ββ auth routes βββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/api/login", methods=["POST"])
def login():
data = request.json
name = data.get("name", "").strip().lower()
password = data.get("password", "").strip()
for user in load_users():
if user["name"].lower() == name and user["password"] == password:
session["user_id"] = user["id"]
return jsonify({"success": True, "name": user["name"], "id": user["id"]})
return jsonify({"success": False, "message": "Name or password incorrect"}), 401
@app.route("/api/logout", methods=["POST"])
def logout():
session.clear()
return jsonify({"success": True})
@app.route("/api/profile", methods=["GET"])
def get_profile():
user_id = session.get("user_id")
if not user_id:
return jsonify({"error": "Not logged in"}), 401
user = find_user(user_id)
if not user:
return jsonify({"error": "User not found"}), 404
return jsonify({k: v for k, v in user.items() if k != "password"})
# ββ run βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
app.run(debug=True, port=8080)