File size: 6,006 Bytes
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a3f26
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a3f26
29bfc1f
 
 
 
 
 
 
 
90a3f26
 
29bfc1f
90a3f26
 
 
 
 
 
 
 
 
 
 
 
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
90a3f26
29bfc1f
 
 
 
 
 
 
 
90a3f26
 
29bfc1f
 
 
 
55a16c0
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a3f26
29bfc1f
 
 
90a3f26
29bfc1f
 
 
 
 
90a3f26
 
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
src/api/people.py — Phase 3: People View endpoints

GET  /api/people                → list all identity clusters
GET  /api/people/{cluster_id}   → all images in that cluster
PATCH /api/people/{cluster_id}  → rename a cluster
POST /api/reindex-clusters      → trigger full re-cluster

All endpoints require the standard pinecone/cloudinary auth headers
(via get_verified_keys). user_id is derived from the Pinecone key hash
so different users don't see each other's clusters even though they share
the same Supabase table.
"""

import hashlib

from fastapi import APIRouter, Body, Depends, Form, HTTPException, Request

from src.core.config import USE_CLUSTER_AWARE_SEARCH
from src.core.security import get_verified_keys
from src.core.logging import log
from src.services.clustering import (
    get_people,
    get_person_images,
    rename_cluster,
    run_clustering,
)
from src.services.db_client import pinecone_pool, ensure_indexes
from src.common.utils import get_ip

import asyncio

router = APIRouter()


def _user_id_from_key(pinecone_key: str) -> str:
    """
    Derives a stable, opaque user_id from the Pinecone API key.
    Users bring their own key, so this is the closest we have to an identity.
    Short SHA256 prefix is enough for row isolation — not a security measure.
    """
    return hashlib.sha256(pinecone_key.encode()).hexdigest()[:16]


@router.post("/api/people")
async def list_people(
    request: Request,
    keys: dict = Depends(get_verified_keys),
):
    """
    Returns all identity clusters for the authenticated user, ordered by
    face_count descending (most-seen people first).

    Request: FormData with user_pinecone_key + user_cloudinary_url

    Response shape:
    {
      "people": [
        {
          "cluster_id": "uuid",
          "name": "Mom" | null,
          "face_count": 42,
          "representative_face_crop": "<base64 jpg>"
        },
        ...
      ],
      "total": 3
    }
    """
    ip = get_ip(request)
    user_id = _user_id_from_key(keys["pinecone_key"])

    try:
        people = await get_people(user_id)
        log("INFO", "people.list", ip=ip, user_id=user_id, count=len(people))
        return {"people": people, "total": len(people)}
    except Exception as e:
        log("ERROR", "people.list.error", ip=ip, user_id=user_id, error=str(e))
        raise HTTPException(500, f"Failed to fetch people: {e}")


@router.post("/api/people/{cluster_id}")
async def get_cluster_images(
    cluster_id: str,
    request: Request,
    keys: dict = Depends(get_verified_keys),
):
    """
    Returns all images belonging to a specific identity cluster.

    Request: FormData with user_pinecone_key + user_cloudinary_url

    Response shape:
    {
      "cluster_id": "uuid",
      "images": [
        {"url": "...", "thumb_url": "...", "folder": "...", "face_crop": "<base64>"},
        ...
      ],
      "total": 12
    }
    """
    ip = get_ip(request)
    user_id = _user_id_from_key(keys["pinecone_key"])

    try:
        images = await get_person_images(cluster_id, user_id)
        log("INFO", "people.cluster_images",
            ip=ip, user_id=user_id, cluster_id=cluster_id, count=len(images))
        return {
            "cluster_id": cluster_id,
            "images": images,
            "total": len(images),
        }
    except Exception as e:
        log("ERROR", "people.cluster_images.error",
            ip=ip, user_id=user_id, cluster_id=cluster_id, error=str(e))
        raise HTTPException(500, f"Failed to fetch cluster images: {e}")


@router.post("/api/people/{cluster_id}/rename")
async def update_cluster_name(
    cluster_id: str,
    request: Request,
    name: str = Form(...),
    keys: dict = Depends(get_verified_keys),
):
    """
    Assigns a human-readable name to a cluster.

    Request: FormData with user_pinecone_key + user_cloudinary_url + name
    Response: {"cluster_id": "uuid", "name": "Mom", "ok": true}
    """
    ip = get_ip(request)
    user_id = _user_id_from_key(keys["pinecone_key"])

    if not name or len(name.strip()) == 0:
        raise HTTPException(400, "name must be a non-empty string")
    if len(name) > 100:
        raise HTTPException(400, "name must be 100 characters or fewer")

    try:
        await rename_cluster(cluster_id, name.strip(), user_id)
        log("INFO", "people.rename",
            ip=ip, user_id=user_id, cluster_id=cluster_id, name=name)
        return {"cluster_id": cluster_id, "name": name.strip(), "ok": True}
    except Exception as e:
        log("ERROR", "people.rename.error",
            ip=ip, user_id=user_id, cluster_id=cluster_id, error=str(e))
        raise HTTPException(500, f"Failed to rename cluster: {e}")


@router.post("/api/reindex-clusters")
async def reindex_clusters(
    request: Request,
    keys: dict = Depends(get_verified_keys),
):
    """
    Triggers a full HDBSCAN re-cluster of the user's face vectors.

    This is a synchronous (blocking) endpoint — clustering typically takes
    5-30 seconds depending on library size. For large libraries, consider
    running this in a background task (Phase 4).

    Response:
    {
      "status": "ok",
      "total_vectors": 3200,
      "clusters_found": 14,
      "noise_vectors": 80
    }
    """
    ip = get_ip(request)
    user_id = _user_id_from_key(keys["pinecone_key"])

    log("INFO", "people.reindex_start", ip=ip, user_id=user_id)

    try:
        pc = pinecone_pool.get(keys["pinecone_key"])

        # Ensure indexes exist before fetching vectors
        await asyncio.to_thread(ensure_indexes, pc)

        result = await run_clustering(pc, user_id)
        log("INFO", "people.reindex_done", ip=ip, user_id=user_id, **result)
        return result

    except RuntimeError as e:
        # e.g. hdbscan not installed
        raise HTTPException(503, str(e))
    except Exception as e:
        log("ERROR", "people.reindex_error", ip=ip, user_id=user_id, error=str(e))
        raise HTTPException(500, f"Clustering failed: {e}")