forked from Mobile-and-Wearable-Sensing-Lab/ISL_Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_3.py
More file actions
1222 lines (960 loc) · 32.8 KB
/
app_3.py
File metadata and controls
1222 lines (960 loc) · 32.8 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import sqlite3
import hashlib
import subprocess
import time
import string
import math
from pathlib import Path
from typing import Optional, List, Iterator, Tuple, Dict
from fastapi.responses import RedirectResponse
from starlette.middleware.sessions import SessionMiddleware
from fastapi import FastAPI, HTTPException, Query, Request
from fastapi.responses import HTMLResponse, StreamingResponse, FileResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
APP_DIR = Path(__file__).parent.resolve()
DB_PATH = APP_DIR / "annotations.db"
MAPPING_CSV = APP_DIR / "mapping.csv"
# ✅ Reference folder (source of truth for all 500 words)
REFERENCE_DIR = Path(r"/home/antpc/Desktop/5000_RTH_Videos (Copy)").resolve()
# SAFE caches (originals untouched)
PREVIEW_DIR = APP_DIR / "previews"
FRAMES_DIR = APP_DIR / "frames_cache"
PREVIEW_DIR.mkdir(exist_ok=True)
FRAMES_DIR.mkdir(exist_ok=True)
# IMPORTANT: restrict file serving to these base folders
ALLOWED_BASE_DIRS = [
REFERENCE_DIR,
Path(r"/mnt/9a528fe4-4fe8-4dff-9a0c-8b1a3cf3d7ba").resolve(),
]
COLLECTED_BASE = Path(r"/mnt/9a528fe4-4fe8-4dff-9a0c-8b1a3cf3d7ba").resolve()
VIDEO_EXTS = {".mp4", ".mpg", ".mov", ".mkv", ".avi", ".webm"}
CHUNK_SIZE = 1024 * 1024 # 1MB
# Frames settings (moderate quality for viewing)
FRAMES_FPS = 12
FRAMES_MAX = 240
FRAMES_HEIGHT = 360
JPG_Q = 5
# ✅ in-memory reference map: word -> reference video path
REFERENCE_MAP: Dict[str, str] = {}
REFERENCE_WORDS: List[str] = []
app = FastAPI(title="ISL Annotation Tool (User-wise, Option A: all reference words)")
app.add_middleware(
SessionMiddleware,
secret_key="isl_annotation_secret_987654",
# ✅ Explicit cookie name
session_cookie="isl_session",
# ✅ Works well for normal browser navigation
same_site="lax",
# ✅ Set True only if you use HTTPS
https_only=False,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------- DB ----------------
def db() -> sqlite3.Connection:
conn = sqlite3.connect(
DB_PATH,
timeout=30,
check_same_thread=False
)
conn.row_factory = sqlite3.Row
# ✅ Enable WAL to prevent write locks
conn.execute("PRAGMA journal_mode=WAL;")
conn.execute("PRAGMA synchronous=NORMAL;")
return conn
def init_db():
conn = db()
cur = conn.cursor()
# ✅ videos table
cur.execute("""
CREATE TABLE IF NOT EXISTS videos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
word TEXT NOT NULL,
reference_path TEXT NOT NULL,
collected_path TEXT NOT NULL,
user_id TEXT NOT NULL
)
""")
# ✅ annotations table (double annotation)
cur.execute("""
CREATE TABLE IF NOT EXISTS annotations (
video_id INTEGER,
annotator TEXT NOT NULL,
label TEXT NOT NULL CHECK(label IN ('correct','comment')),
note TEXT,
updated_at TEXT DEFAULT (datetime('now')),
PRIMARY KEY (video_id, annotator)
)
""")
# indexes
cur.execute("CREATE INDEX IF NOT EXISTS idx_videos_word_user ON videos(word, user_id)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_videos_user ON videos(user_id)")
conn.commit()
conn.close()
def load_mapping_if_needed():
if not MAPPING_CSV.exists():
raise RuntimeError(f"mapping.csv not found at {MAPPING_CSV}")
conn = db()
cur = conn.cursor()
cur.execute("SELECT COUNT(*) AS c FROM videos")
count = cur.fetchone()["c"]
if count > 0:
conn.close()
return
with MAPPING_CSV.open("r", encoding="utf-8") as f:
reader = csv.DictReader(f)
rows = [(r["word"], r["reference_path"], r["collected_path"], r["user_id"]) for r in reader]
cur.executemany(
"INSERT INTO videos(word, reference_path, collected_path, user_id) VALUES (?,?,?,?)",
rows
)
conn.commit()
conn.close()
# ---------------- PATH SAFETY ----------------
def is_allowed_path(p: Path) -> bool:
try:
rp = p.resolve()
except Exception:
return False
for base in ALLOWED_BASE_DIRS:
if str(rp).startswith(str(base)):
return True
return False
def is_collected_path(p: Path) -> bool:
try:
rp = p.resolve()
except Exception:
return False
return str(rp).startswith(str(COLLECTED_BASE))
# ---------------- OPTION A (REFERENCE WORDS FROM FOLDER) ----------------
def scan_reference_folder() -> None:
"""
Build REFERENCE_MAP from filenames in REFERENCE_DIR.
Word is filename without extension, lowercased.
"""
global REFERENCE_MAP, REFERENCE_WORDS
if not REFERENCE_DIR.exists():
raise RuntimeError(f"REFERENCE_DIR not found: {REFERENCE_DIR}")
ref_map: Dict[str, str] = {}
for p in REFERENCE_DIR.iterdir():
if p.is_file() and p.suffix.lower() in VIDEO_EXTS:
word = p.stem.strip().lower()
if word:
ref_map[word] = str(p.resolve())
if not ref_map:
raise RuntimeError(f"No reference videos found in: {REFERENCE_DIR}")
REFERENCE_MAP = ref_map
REFERENCE_WORDS = sorted(ref_map.keys())
def get_reference_words() -> List[str]:
return REFERENCE_WORDS
def get_reference_path(word: str) -> str:
w = word.strip().lower()
if w not in REFERENCE_MAP:
raise HTTPException(status_code=404, detail=f"Reference video not found for word: {word}")
return REFERENCE_MAP[w]
# ---------------- PREVIEW (SAFE, browser-compatible) ----------------
def ffprobe_video_codec(p: Path) -> Optional[str]:
try:
out = subprocess.check_output(
[
"ffprobe", "-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=codec_name",
"-of", "default=nw=1",
str(p)
],
text=True
)
for line in out.splitlines():
if line.startswith("codec_name="):
return line.split("=", 1)[1].strip()
except Exception:
return None
return None
def preview_path_for(original: Path) -> Path:
st = original.stat()
key = f"{original.resolve()}|{st.st_mtime_ns}|{st.st_size}"
h = hashlib.md5(key.encode("utf-8")).hexdigest()
return PREVIEW_DIR / f"{h}.mp4"
def run_ffmpeg(cmd: List[str]) -> None:
p = subprocess.run(cmd, text=True, capture_output=True)
if p.returncode != 0:
log_file = APP_DIR / "ffmpeg_errors.log"
with log_file.open("a", encoding="utf-8") as f:
f.write("\n\n=== FFMPEG FAILED ===\n")
f.write("CMD: " + " ".join(cmd) + "\n")
f.write("STDERR:\n" + (p.stderr or "") + "\n")
f.write("STDOUT:\n" + (p.stdout or "") + "\n")
tail = "\n".join((p.stderr or "").splitlines()[-25:])
raise RuntimeError(tail if tail.strip() else "ffmpeg failed with no stderr output")
def ensure_h264_preview(original: Path) -> Path:
"""
Preview-only, original untouched.
Outputs strict browser-friendly: H.264 + yuv420p + faststart.
"""
out = preview_path_for(original)
if out.exists():
try:
if out.stat().st_size == 0:
out.unlink()
except Exception:
pass
if out.exists():
try:
if out.stat().st_size > 0:
return out
except Exception:
pass
lock = out.with_suffix(".lock")
if lock.exists():
for _ in range(240):
if out.exists():
try:
if out.stat().st_size > 0:
return out
except Exception:
pass
time.sleep(0.5)
lock.write_text("1", encoding="utf-8")
tmp = out.with_suffix(".tmp.mp4")
vf = "scale=-2:480"
common_in = [
"-hide_banner",
"-fflags", "+genpts",
"-analyzeduration", "100M",
"-probesize", "100M",
"-i", str(original),
]
common_out = [
"-vf", vf,
"-c:v", "libx264",
"-preset", "veryfast",
"-crf", "23",
"-pix_fmt", "yuv420p",
"-profile:v", "main",
"-level", "3.1",
"-movflags", "+faststart",
str(tmp)
]
cmd1 = ["ffmpeg", "-y", *common_in,
"-map", "0:v:0", "-map", "0:a:0?",
"-c:a", "aac", "-b:a", "128k",
*common_out]
cmd2 = ["ffmpeg", "-y", *common_in,
"-map", "0:v:0", "-an",
*common_out]
cmd3 = ["ffmpeg", "-y",
"-hide_banner",
"-fflags", "+genpts",
"-err_detect", "ignore_err",
"-analyzeduration", "100M",
"-probesize", "100M",
"-i", str(original),
"-map", "0:v:0", "-map", "0:a:0?",
"-c:a", "aac", "-b:a", "128k",
*common_out]
try:
if tmp.exists():
try: tmp.unlink()
except Exception: pass
try:
run_ffmpeg(cmd1)
except Exception:
if tmp.exists():
try: tmp.unlink()
except Exception: pass
try:
run_ffmpeg(cmd2)
except Exception:
if tmp.exists():
try: tmp.unlink()
except Exception: pass
run_ffmpeg(cmd3)
if (not tmp.exists()) or tmp.stat().st_size == 0:
raise RuntimeError("Preview temp file missing or empty after ffmpeg.")
tmp.replace(out)
if out.stat().st_size == 0:
raise RuntimeError("Preview output is empty after rename.")
return out
finally:
if tmp.exists():
try: tmp.unlink()
except Exception: pass
if lock.exists():
try: lock.unlink()
except Exception: pass
# ---------------- SAFE RANGE STREAMING (NEVER 416) ----------------
def parse_range(range_header: str, file_size: int) -> Optional[Tuple[int, int]]:
if not range_header:
return None
if not range_header.startswith("bytes="):
return None
spec = range_header.replace("bytes=", "", 1).strip()
if "," in spec or "-" not in spec:
return None
start_s, end_s = spec.split("-", 1)
start_s, end_s = start_s.strip(), end_s.strip()
try:
if start_s == "":
length = int(end_s)
if length <= 0:
return None
start = max(file_size - length, 0)
end = file_size - 1
return start, end
start = int(start_s)
end = file_size - 1 if end_s == "" else int(end_s)
if start < 0 or end < 0 or start > end or start >= file_size:
return None
end = min(end, file_size - 1)
return start, end
except Exception:
return None
def file_iterator(path: Path, start: int, end: int) -> Iterator[bytes]:
with path.open("rb") as f:
f.seek(start)
remaining = end - start + 1
while remaining > 0:
chunk = f.read(min(CHUNK_SIZE, remaining))
if not chunk:
break
remaining -= len(chunk)
yield chunk
# ---------------- FRAMES FALLBACK (SAFE) ----------------
def frames_key_for(original: Path) -> str:
st = original.stat()
key = f"{original.resolve()}|{st.st_mtime_ns}|{st.st_size}|frames|{FRAMES_FPS}|{FRAMES_HEIGHT}|{JPG_Q}"
return hashlib.md5(key.encode("utf-8")).hexdigest()
def ffprobe_duration_seconds(p: Path) -> Optional[float]:
try:
out = subprocess.check_output(
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=nw=1", str(p)],
text=True
)
for line in out.splitlines():
if line.startswith("duration="):
return float(line.split("=", 1)[1].strip())
except Exception:
return None
return None
def ensure_frames(original: Path) -> Tuple[str, int]:
key = frames_key_for(original)
out_dir = FRAMES_DIR / key
out_dir.mkdir(exist_ok=True)
existing = sorted(out_dir.glob("frame_*.jpg"))
if existing:
return key, len(existing)
lock = out_dir / ".lock"
if lock.exists():
for _ in range(240):
existing = sorted(out_dir.glob("frame_*.jpg"))
if existing:
return key, len(existing)
time.sleep(0.5)
lock.write_text("1", encoding="utf-8")
try:
dur = ffprobe_duration_seconds(original)
if dur is not None and dur > 0:
target = int(dur * FRAMES_FPS)
target = max(1, min(target, FRAMES_MAX))
else:
target = FRAMES_MAX
vf = f"fps={FRAMES_FPS},scale=-2:{FRAMES_HEIGHT}"
pattern = str(out_dir / "frame_%06d.jpg")
cmd = [
"ffmpeg", "-y",
"-hide_banner",
"-fflags", "+genpts",
"-analyzeduration", "100M",
"-probesize", "100M",
"-i", str(original),
"-vf", vf,
"-frames:v", str(target),
"-q:v", str(JPG_Q),
pattern
]
run_ffmpeg(cmd)
frames = sorted(out_dir.glob("frame_*.jpg"))
if not frames:
raise RuntimeError("No frames extracted (file may be corrupted).")
return key, len(frames)
finally:
if lock.exists():
try: lock.unlink()
except Exception: pass
ROLE_RANGES = {}
def build_role_ranges():
global ROLE_RANGES
ROLE_RANGES = {}
VIDEOS_PER_ROLE = 25
total_words = len(REFERENCE_WORDS)
num_groups = math.ceil(total_words / VIDEOS_PER_ROLE)
base_letters = string.ascii_uppercase[:num_groups]
for i, c in enumerate(base_letters):
start = i * VIDEOS_PER_ROLE
end = min(start + VIDEOS_PER_ROLE, total_words)
ROLE_RANGES[f"{c}1"] = (start, end)
ROLE_RANGES[f"{c}2"] = (start, end)
print("✅ ROLE_RANGES built:", ROLE_RANGES.keys())
# ---------------- STARTUP ----------------
@app.on_event("startup")
def startup():
init_db()
load_mapping_if_needed()
scan_reference_folder()
build_role_ranges()
# ✅ this is what makes words = 500
# ---------------- UI ----------------
@app.get("/isl/", response_class=HTMLResponse)
def index(request: Request):
# ✅ If not logged in → go to login page
return RedirectResponse(url="/isl/login")
# ✅ If logged in → load annotation tool
html = APP_DIR / "index_5.html"
if not html.exists():
return HTMLResponse("<h3>index.html not found</h3>", status_code=404)
return HTMLResponse(html.read_text(encoding="utf-8"))
@app.get("/isl/admin", response_class=HTMLResponse)
def admin_page(request: Request):
role = request.session.get("role")
if role != "Z":
raise HTTPException(status_code=403, detail="Admin only")
html = APP_DIR / "admin_review.html"
if not html.exists():
return HTMLResponse("<h3>admin_review.html not found</h3>", status_code=404)
return HTMLResponse(html.read_text(encoding="utf-8"))
@app.get("/isl/tool", response_class=HTMLResponse)
def tool(request: Request):
# must be logged in
if not request.session.get("role"):
return RedirectResponse(url="/isl/login")
html = APP_DIR / "index_5.html"
return HTMLResponse(html.read_text(encoding="utf-8"))
@app.get("/isl/login", response_class=HTMLResponse)
def login_page():
return HTMLResponse("""
<html>
<head>
<title>ISL Login</title>
<style>
body{
font-family: Arial;
background:#0b0f17;
color:white;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
.box{
background:#121a27;
padding:35px;
border-radius:14px;
text-align:center;
width:320px;
}
input{
padding:12px;
font-size:16px;
width:90%;
border-radius:8px;
border:none;
outline:none;
text-align:center;
}
button{
padding:10px 20px;
margin-top:15px;
border:none;
border-radius:10px;
cursor:pointer;
font-weight:bold;
}
</style>
</head>
<body>
<div class="box">
<h2>Annotation Tool Login</h2>
<p>Enter key: <b>user_A1 ... user_U2</b> or <b>user_Z</b></p>
<input id="keyBox" placeholder="user_A"/>
<br/>
<button onclick="doLogin()">Sign In</button>
<p id="msg"></p>
</div>
<script>
async function doLogin(){
const key = document.getElementById("keyBox").value.trim();
// ✅ Correct API path (must include /isl/)
const res = await fetch("/isl/api/login", {
method:"POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({key})
});
if(res.ok){
// ✅ Redirect into annotation tool
window.location.href = "/isl/tool";
} else {
document.getElementById("msg").innerText =
"❌ Invalid Key!";
}
}
</script>
</body>
</html>
""")
# ------------------------------------
@app.post("/isl/api/login")
async def login(data: dict, request: Request):
key = data.get("key", "").strip()
if not key.startswith("user_"):
raise HTTPException(status_code=401, detail="Invalid login key")
role = key.replace("user_", "").upper()
# Admin
if role == "Z":
request.session["role"] = "Z"
return {"ok": True, "role": "Z"}
# Normal roles A–T
if role not in ROLE_RANGES:
raise HTTPException(status_code=401, detail="Invalid login key")
request.session["role"] = role
return {"ok": True, "role": role}
# ---------------- WHO AM I (ROLE CHECK) ----------------
@app.get("/isl/api/me")
def me(request: Request):
role = request.session.get("role")
if not role:
raise HTTPException(status_code=401, detail="Not logged in")
return {"role": role}
# ---------------- USER API ----------------
@app.get("/isl/api/users")
def list_users():
conn = db()
cur = conn.cursor()
cur.execute("SELECT DISTINCT user_id FROM videos ORDER BY user_id")
users = [r["user_id"] for r in cur.fetchall()]
cur.execute("""
SELECT COUNT(*) AS total,
SUM(CASE WHEN a.video_id IS NOT NULL THEN 1 ELSE 0 END) AS labeled
FROM videos v
LEFT JOIN annotations a ON a.video_id = v.id
""")
overall = cur.fetchone()
conn.close()
return {
"users": users,
"total_videos": int(overall["total"]),
"labeled_videos": int(overall["labeled"] or 0),
"reference_words": len(REFERENCE_WORDS),
}
@app.get("/isl/api/user/{user_id}/words")
def user_words(user_id: str, request: Request):
role = request.session.get("role")
if not role:
raise HTTPException(status_code=401, detail="Not logged in")
all_words = get_reference_words()
# Admin sees all words
if role == "Z":
allowed_words = all_words
# Normal users see slice
elif role in ROLE_RANGES:
start, end = ROLE_RANGES[role]
allowed_words = all_words[start:end]
else:
raise HTTPException(status_code=403, detail="Invalid role")
return {
"user_id": user_id,
"words": allowed_words,
"total_words": len(allowed_words),
"role": role
}
@app.get("/isl/api/user/{user_id}/word/{word}")
def user_word_detail(user_id: str, word: str, request: Request):
role = request.session.get("role")
if not role:
raise HTTPException(status_code=401, detail="Not logged in")
words = get_reference_words()
# ✅ Restrict access for normal users
if role != "Z":
start, end = ROLE_RANGES[role]
allowed_words = words[start:end]
if word.strip().lower() not in allowed_words:
raise HTTPException(status_code=403, detail="Word not allowed")
word_l = word.strip().lower()
# Reference video path
reference_path = get_reference_path(word_l)
# ✅ Fetch collected clips + ONLY this annotator’s label
conn = db()
cur = conn.cursor()
cur.execute("""
SELECT v.id,
v.collected_path,
v.user_id,
a.label,
a.note,
a.updated_at
FROM videos v
LEFT JOIN annotations a
ON a.video_id = v.id
AND a.annotator = ?
WHERE v.user_id = ?
AND v.word = ?
ORDER BY v.collected_path
""", (role, user_id, word_l))
vids = [dict(r) for r in cur.fetchall()]
conn.close()
idx = words.index(word_l)
return {
"user_id": user_id,
"word": word_l,
"reference_path": reference_path,
"videos": vids,
"index": idx,
"total_words": len(words),
"prev_word": words[idx - 1] if idx > 0 else None,
"next_word": words[idx + 1] if idx < len(words) - 1 else None,
}
# ---------------- ANNOTATION + EXPORT ----------------
@app.post("/isl/api/annotate")
def annotate(video_id: int, label: str, request: Request, note: Optional[str] = None):
role = request.session.get("role")
# 1. Must be logged in
if not role:
raise HTTPException(status_code=401, detail="Not logged in")
# 2. ❌ Admin Z must not annotate
if role == "Z":
raise HTTPException(
status_code=403,
detail="Admin cannot annotate videos"
)
# 3. Validate label
if label not in ("correct", "comment"):
raise HTTPException(status_code=400, detail="Invalid label")
annotator = role # example: A1, A2
conn = db()
cur = conn.cursor()
# 4. Insert or update annotation
cur.execute("""
INSERT INTO annotations(video_id, annotator, label, note)
VALUES (?,?,?,?)
ON CONFLICT(video_id, annotator) DO UPDATE SET
label=excluded.label,
note=excluded.note,
updated_at=datetime('now')
""", (video_id, annotator, label, note))
conn.commit()
conn.close()
return {"ok": True, "annotator": annotator}
@app.get("/isl/api/export.csv")
def export_csv(request: Request):
role = request.session.get("role")
if not role:
raise HTTPException(401, "Not logged in")
conn = db()
cur = conn.cursor()
cur.execute("""
SELECT v.id AS video_id,
v.word,
v.user_id,
v.collected_path,
a.label,
a.note,
a.updated_at
FROM videos v
LEFT JOIN annotations a
ON a.video_id = v.id
AND a.annotator = ?
ORDER BY v.word, v.user_id
""", (role,))
rows = cur.fetchall()
conn.close()
def gen():
import io
out = io.StringIO()
writer = csv.writer(out)
writer.writerow([
"video_id","word","user_id",
"collected_path",
"label","note","updated_at"
])
yield out.getvalue()
out.seek(0); out.truncate(0)
for r in rows:
writer.writerow([
r["video_id"],
r["word"],
r["user_id"],
r["collected_path"],
r["label"] or "",
r["note"] or "",
r["updated_at"] or ""
])
yield out.getvalue()
out.seek(0); out.truncate(0)
return StreamingResponse(
gen(),
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename=export_{role}.csv"}
)
@app.get("/api/admin/export_all.csv")
def export_all(request: Request):
role = request.session.get("role")
if role != "Z":
raise HTTPException(403, "Admin only")
conn = db()
cur = conn.cursor()
# ---------------------------------------------------------
# Load all videos
# ---------------------------------------------------------
cur.execute("""
SELECT id, word, user_id, collected_path
FROM videos
ORDER BY id
""")
videos = cur.fetchall()
# ---------------------------------------------------------
# Load all annotations
# ---------------------------------------------------------
cur.execute("""
SELECT video_id, annotator, label, note
FROM annotations
ORDER BY video_id, annotator
""")
anns = cur.fetchall()
conn.close()
# ---------------------------------------------------------
# Build map: video_id -> list of annotations
# ---------------------------------------------------------
ann_map = {}
for a in anns:
ann_map.setdefault(a["video_id"], []).append(dict(a))
# ---------------------------------------------------------
# CSV Generator
# ---------------------------------------------------------
def gen():
import io
out = io.StringIO()
writer = csv.writer(out)
# ✅ Final Columns
writer.writerow([
"video_id",
"word",
"user_id",
"collected_path",
"label_1",
"note_1",
"label_2",
"note_2",
"review_status"
])
yield out.getvalue()
out.seek(0)
out.truncate(0)
# ---------------------------------------------------------
# Process each video row
# ---------------------------------------------------------
for v in videos:
vid = v["id"]
word = v["word"]
user_id = v["user_id"]
path = v["collected_path"]
annotations = ann_map.get(vid, [])
# Defaults
label_1 = ""
note_1 = ""
label_2 = ""
note_2 = ""
# -------------------------------------------------
# Fill annotator 1 and 2
# -------------------------------------------------
if len(annotations) >= 1:
label_1 = annotations[0]["label"]
note_1 = annotations[0]["note"] or ""
if len(annotations) >= 2:
label_2 = annotations[1]["label"]
note_2 = annotations[1]["note"] or ""
# -------------------------------------------------
# Determine review_status
# -------------------------------------------------
if len(annotations) == 0:
status = "not_annotated"