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
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
# src/api.py
# FastAPI application using an async task queue.
#
# Important:
# The API does NOT receive the full orthomosaic.
# It receives a small georeferenced crop GeoTIFF uploaded as multipart/form-data.

import asyncio
import logging
import os
import tempfile
import time
import uuid
from contextlib import asynccontextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Dict, Any

from fastapi import (
    FastAPI,
    HTTPException,
    status,
    UploadFile,
    File,
    Form,
)
from fastapi.responses import FileResponse
from starlette.background import BackgroundTask

from src.infer import run_geoglyph_sam2_on_crop

logger = logging.getLogger("api")


# ---------------------------------------------------------------------------
# Global state
# ---------------------------------------------------------------------------

RESULTS_DIR = Path(tempfile.gettempdir()) / "geoglyph_sam2_api"
RESULTS_DIR.mkdir(parents=True, exist_ok=True)

TASKS: Dict[str, Dict[str, Any]] = {}

# Single queue. One worker means one SAM2 inference at a time.
# This avoids GPU OOM when several users submit jobs.
QUEUE: asyncio.Queue = asyncio.Queue(maxsize=20)


# ---------------------------------------------------------------------------
# Internal job object
# ---------------------------------------------------------------------------

@dataclass
class InferenceJob:
    crop_path: str
    output_gpkg: str

    device: Optional[str] = None

    use_clahe: bool = True
    clahe_clip: float = 4.0
    clahe_grid: int = 6

    sam2_points_per_side: int = 32
    sam2_points_per_batch: int = 32
    sam2_pred_iou_thresh: float = 0.35
    sam2_stability_score_thresh: float = 0.65

    filter_min_area_px: int = 1000
    filter_max_area_frac: float = 0.20
    filter_min_iou: float = 0.35
    filter_min_stability: float = 0.65
    filter_border_margin: int = 10

    max_crop_side: int = 4096


# ---------------------------------------------------------------------------
# Worker
# ---------------------------------------------------------------------------

async def queue_worker():
    """
    Background worker.

    It processes tasks sequentially to avoid GPU/CPU contention and GPU OOM.
    Heavy SAM2 inference runs in a separate thread.
    """

    logger.info("Task queue worker started.")

    while True:
        try:
            task_id, job = await QUEUE.get()

        except asyncio.CancelledError:
            logger.info("Task queue worker cancelled.")
            break

        except Exception as exc:
            logger.error("Error retrieving task from queue: %s", exc, exc_info=True)
            continue

        try:
            if task_id not in TASKS:
                continue

            TASKS[task_id]["status"] = "processing"
            TASKS[task_id]["started_at"] = time.time()

            logger.info("Worker started task_id=%s", task_id)

            result = await asyncio.to_thread(
                run_geoglyph_sam2_on_crop,
                crop_tif_path=job.crop_path,
                output_gpkg=job.output_gpkg,
                device=job.device,
                use_clahe=job.use_clahe,
                clahe_clip=job.clahe_clip,
                clahe_grid=job.clahe_grid,
                sam2_points_per_side=job.sam2_points_per_side,
                sam2_points_per_batch=job.sam2_points_per_batch,
                sam2_pred_iou_thresh=job.sam2_pred_iou_thresh,
                sam2_stability_score_thresh=job.sam2_stability_score_thresh,
                filter_min_area_px=job.filter_min_area_px,
                filter_max_area_frac=job.filter_max_area_frac,
                filter_min_iou=job.filter_min_iou,
                filter_min_stability=job.filter_min_stability,
                filter_border_margin=job.filter_border_margin,
                max_crop_side=job.max_crop_side,
            )

            TASKS[task_id].update(
                {
                    "status": "completed",
                    "finished_at": time.time(),
                    "n_masks": result["n_masks"],
                    "output_exists": result["output_exists"],
                    "result": result,
                    "download_url": f"/download/{task_id}"
                    if result["output_exists"]
                    else None,
                }
            )

            logger.info(
                "Worker completed task_id=%s  n_masks=%d  output_exists=%s",
                task_id,
                result["n_masks"],
                result["output_exists"],
            )

        except Exception as exc:
            logger.error(
                "Worker failed task_id=%s: %s",
                task_id,
                exc,
                exc_info=True,
            )

            if task_id in TASKS:
                TASKS[task_id].update(
                    {
                        "status": "failed",
                        "finished_at": time.time(),
                        "error": str(exc),
                    }
                )

            try:
                import torch

                if torch.cuda.is_available():
                    torch.cuda.empty_cache()
                    logger.info("Cleared CUDA cache after task failure.")

            except Exception:
                pass

        finally:
            # Crop is no longer needed after processing.
            try:
                if os.path.exists(job.crop_path):
                    os.remove(job.crop_path)
                    logger.info("Deleted temporary crop for task_id=%s", task_id)
            except Exception as exc:
                logger.warning(
                    "Could not delete temporary crop for task_id=%s: %s",
                    task_id,
                    exc,
                )

            QUEUE.task_done()


# ---------------------------------------------------------------------------
# Lifespan
# ---------------------------------------------------------------------------

@asynccontextmanager
async def lifespan(app: FastAPI):
    worker_task = asyncio.create_task(queue_worker())

    yield

    worker_task.cancel()

    try:
        await worker_task
    except asyncio.CancelledError:
        pass


# ---------------------------------------------------------------------------
# FastAPI app
# ---------------------------------------------------------------------------

app = FastAPI(
    title="GeoGlyph SAM2 API",
    description=(
        "Backend API for geoglyph detection using SAM2. "
        "Receives small georeferenced crop GeoTIFFs, not full orthomosaics."
    ),
    version="3.0.0",
    lifespan=lifespan,
)


# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------

@app.get("/health", status_code=status.HTTP_200_OK)
async def health_check():
    return {
        "status": "ok",
        "message": "GeoGlyph SAM2 API is running.",
        "queue_size": QUEUE.qsize(),
        "results_dir": str(RESULTS_DIR),
    }


@app.post("/process", status_code=status.HTTP_202_ACCEPTED)
async def process_geoglyphs(
    crop: UploadFile = File(...),
    device: Optional[str] = Form(None),

    use_clahe: bool = Form(True),
    clahe_clip: float = Form(4.0),
    clahe_grid: int = Form(6),

    sam2_points_per_side: int = Form(32),
    sam2_points_per_batch: int = Form(32),
    sam2_pred_iou_thresh: float = Form(0.35),
    sam2_stability_score_thresh: float = Form(0.65),

    filter_min_area_px: int = Form(1000),
    filter_max_area_frac: float = Form(0.20),
    filter_min_iou: float = Form(0.35),
    filter_min_stability: float = Form(0.65),
    filter_border_margin: int = Form(10),

    max_crop_side: int = Form(4096),
):
    """
    Submit a SAM2 inference job.

    The client uploads a georeferenced crop GeoTIFF.
    The API never receives the original orthomosaic.
    """

    if QUEUE.full():
        raise HTTPException(
            status_code=status.HTTP_429_TOO_MANY_REQUESTS,
            detail="Task queue is full. Try again later.",
        )

    if device not in {None, "cuda", "cpu"}:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Invalid device. Expected 'cuda', 'cpu', or omitted.",
        )

    task_id = uuid.uuid4().hex[:12]

    crop_path = RESULTS_DIR / f"{task_id}_crop.tif"
    output_gpkg = RESULTS_DIR / f"{task_id}.gpkg"

    try:
        with open(crop_path, "wb") as f:
            while True:
                chunk = await crop.read(1024 * 1024)
                if not chunk:
                    break
                f.write(chunk)

        await crop.close()

    except Exception as exc:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Could not save uploaded crop: {exc}",
        )

    if not crop_path.is_file() or crop_path.stat().st_size == 0:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Uploaded crop is empty or could not be saved.",
        )

    job = InferenceJob(
        crop_path=str(crop_path),
        output_gpkg=str(output_gpkg),
        device=device,
        use_clahe=use_clahe,
        clahe_clip=clahe_clip,
        clahe_grid=clahe_grid,
        sam2_points_per_side=sam2_points_per_side,
        sam2_points_per_batch=sam2_points_per_batch,
        sam2_pred_iou_thresh=sam2_pred_iou_thresh,
        sam2_stability_score_thresh=sam2_stability_score_thresh,
        filter_min_area_px=filter_min_area_px,
        filter_max_area_frac=filter_max_area_frac,
        filter_min_iou=filter_min_iou,
        filter_min_stability=filter_min_stability,
        filter_border_margin=filter_border_margin,
        max_crop_side=max_crop_side,
    )

    TASKS[task_id] = {
        "status": "pending",
        "created_at": time.time(),
        "original_filename": crop.filename,
        "crop_path": str(crop_path),
        "output_gpkg": str(output_gpkg),
        "n_masks": None,
        "output_exists": None,
        "error": None,
    }

    await QUEUE.put((task_id, job))

    logger.info(
        "Enqueued task_id=%s  filename=%s  queue_size=%d",
        task_id,
        crop.filename,
        QUEUE.qsize(),
    )

    return {
        "task_id": task_id,
        "status": "pending",
        "queue_size": QUEUE.qsize(),
    }


@app.get("/status/{task_id}", status_code=status.HTTP_200_OK)
async def get_status(task_id: str):
    """
    Check task status.

    Possible statuses:
    - pending
    - processing
    - completed
    - failed
    """

    if task_id not in TASKS:
        raise HTTPException(status_code=404, detail="Task not found")

    task_info = TASKS[task_id]

    if task_info["status"] == "pending":
        pending_ids = [
            tid
            for tid, data in TASKS.items()
            if data["status"] == "pending"
        ]

        try:
            position = pending_ids.index(task_id) + 1
        except ValueError:
            position = 0

        return {
            "task_id": task_id,
            "status": "pending",
            "queue_position": position,
        }

    if task_info["status"] == "processing":
        return {
            "task_id": task_id,
            "status": "processing",
            "started_at": task_info.get("started_at"),
        }

    if task_info["status"] == "completed":
        return {
            "task_id": task_id,
            "status": "completed",
            "n_masks": task_info.get("n_masks"),
            "output_exists": task_info.get("output_exists"),
            "download_url": task_info.get("download_url"),
            "result": task_info.get("result"),
        }

    if task_info["status"] == "failed":
        return {
            "task_id": task_id,
            "status": "failed",
            "error": task_info.get("error"),
        }

    return task_info


def cleanup_task(task_id: str):
    """
    Delete generated files and remove task metadata.
    Called after successful download.
    """

    task = TASKS.get(task_id, {})

    paths_to_delete = [
        task.get("output_gpkg"),
        task.get("crop_path"),
    ]

    for path_str in paths_to_delete:
        if not path_str:
            continue

        path = Path(path_str)

        if path.exists():
            try:
                path.unlink()
                logger.info("Deleted file for task_id=%s: %s", task_id, path)
            except Exception as exc:
                logger.warning(
                    "Could not delete file for task_id=%s: %s",
                    task_id,
                    exc,
                )

    if task_id in TASKS:
        del TASKS[task_id]


@app.get("/download/{task_id}")
async def download_result(task_id: str):
    """
    Download the generated GeoPackage.

    The task is cleaned after transfer.
    """

    if task_id not in TASKS:
        raise HTTPException(status_code=404, detail="Task not found")

    task = TASKS[task_id]

    if task["status"] != "completed":
        raise HTTPException(
            status_code=400,
            detail="Task is not completed yet.",
        )

    if not task.get("output_exists"):
        raise HTTPException(
            status_code=404,
            detail="Task completed but no GeoPackage was created. No masks passed the filters.",
        )

    gpkg_path = Path(task["output_gpkg"])

    if not gpkg_path.is_file():
        raise HTTPException(
            status_code=404,
            detail="Result file is missing.",
        )

    logger.info("Serving GPKG download  |  task_id=%s", task_id)

    return FileResponse(
        path=str(gpkg_path),
        media_type="application/geopackage+sqlite3",
        filename=f"sam2_result_{task_id}.gpkg",
        background=BackgroundTask(cleanup_task, task_id),
    )