File size: 25,104 Bytes
b179032
6790d17
b179032
956be74
e894ef0
b179032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6790d17
0b29256
8aa0d3e
6790d17
 
0b29256
6790d17
 
 
 
0b29256
6790d17
 
 
 
 
 
 
 
0b29256
6790d17
 
 
 
 
 
 
 
 
 
 
0b29256
 
 
 
 
 
6790d17
b179032
0b29256
 
 
 
 
 
 
 
 
 
 
 
 
 
b179032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7055890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b179032
 
 
 
 
 
 
 
 
 
 
956be74
 
 
 
 
 
 
 
b179032
 
 
0b29256
6790d17
 
8aa0d3e
6790d17
 
 
8aa0d3e
6790d17
b179032
 
 
 
 
 
 
0b29256
b179032
 
 
956be74
62d0284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b29256
62d0284
 
8aa0d3e
62d0284
 
 
8aa0d3e
62d0284
 
 
 
 
 
 
0b29256
62d0284
 
 
 
e894ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38d3ceb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e894ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
956be74
 
 
 
 
 
 
 
0b29256
6790d17
 
8aa0d3e
6790d17
 
8aa0d3e
6790d17
956be74
 
 
 
 
 
0b29256
956be74
 
 
 
c9843ee
 
 
38d3ceb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9843ee
 
 
 
 
 
 
 
 
 
38d3ceb
c9843ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38d3ceb
 
c9843ee
 
bffefa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
956be74
c9843ee
956be74
 
 
 
 
c9843ee
 
 
 
 
 
956be74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664c9a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53bccd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b179032
956be74
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
import os
import time
import requests
from fastapi import FastAPI, UploadFile, File, Form, Query, Request, Body
from fastapi.responses import Response, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.concurrency import run_in_threadpool # <--- FIX: Added Threadpool
import uvicorn

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

HF_TOKEN = os.getenv("HF_TOKEN")

DEV_URL = "https://PPAL-SongLab-UGA-fruit-analyzer-dev.hf.space"
PROD_URL = "https://PPAL-SongLab-UGA-fruit-analyzer.hf.space"
HF_RATE_LIMIT_STATUS = 429
HF_TRANSIENT_STATUS = {429, 502, 503, 504}
PROCESS_PROXY_TIMEOUT_SECONDS = 40


def request_with_hf_backoff(method, url, *, max_retries=3, **kwargs):
    last_response = None
    for attempt in range(max_retries + 1):
        response = method(url, **kwargs)
        last_response = response
        if response.status_code not in HF_TRANSIENT_STATUS:
            return response

        retry_after = response.headers.get("retry-after")
        try:
            wait_s = float(retry_after) if retry_after else None
        except ValueError:
            wait_s = None
        if wait_s is None:
            wait_s = [2.0, 5.0, 10.0, 15.0][min(attempt, 3)] if response.status_code == HF_RATE_LIMIT_STATUS else [3.0, 8.0, 15.0, 20.0][min(attempt, 3)]
        if attempt < max_retries:
            time.sleep(min(max(wait_s, 0.5), 20.0))
    return last_response


def hf_error_message(exc):
    response = getattr(exc, "response", None)
    if response is not None and response.status_code == HF_RATE_LIMIT_STATUS:
        retry_after = response.headers.get("retry-after")
        suffix = f" Retry after about {retry_after} seconds." if retry_after else " Please wait a minute and try again."
        return f"Hugging Face is rate limiting this Space after several retry attempts.{suffix}"
    if response is not None and response.status_code in {502, 503, 504}:
        return "Hugging Face Space is temporarily unavailable or waking up after several retry attempts. Please wait a moment and try again."
    if isinstance(exc, requests.exceptions.Timeout):
        return "Hugging Face Space did not respond before the proxy timeout. Please try again."
    if isinstance(exc, requests.exceptions.ConnectionError):
        return "Could not connect to the Hugging Face Space. Please try again."
    return str(exc)


def is_retryable_hf_exception(exc):
    response = getattr(exc, "response", None)
    return bool(response is not None and response.status_code in HF_TRANSIENT_STATUS)


def proxy_error_payload(prefix, exc, **extra):
    return {
        "success": False,
        "message": f"{prefix}: {hf_error_message(exc)}",
        "retryable": is_retryable_hf_exception(exc),
        **extra,
    }

@app.get("/")
def read_root():
    return {"status": "Render Proxy is awake!"}

@app.get("/proxy_status")
async def proxy_status(username: str = Query("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    status_url = f"{base_url}/queue_status"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}
    
    # FIX: Define the blocking request
    def fetch_status():
        return requests.get(status_url, headers=headers, timeout=5)
        
    try:
        # FIX: Run it in a background thread so the server doesn't freeze
        response = await run_in_threadpool(fetch_status)
        response.raise_for_status()
        return response.json()
    except Exception as e:
        return {"active_requests": 0, "max_concurrent": 2}


@app.post("/proxy_warmup")
async def proxy_warmup(payload: dict = Body(...)):
    """Warm the production backend only.

    This deliberately does not route devtest to the dev Space; it exists to
    keep the production user path responsive without burning analysis jobs.
    """
    target_url = f"{PROD_URL}/warmup"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_post():
        return request_with_hf_backoff(
            requests.post,
            target_url,
            max_retries=0,
            headers=headers,
            json=payload,
            timeout=PROCESS_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Warmup Error (Hugging Face)", e)
    except Exception as e:
        return {"success": False, "message": f"Proxy Warmup Error (Internal): {str(e)}"}


@app.post("/proxy_process")
async def proxy_process(request: Request, file: UploadFile = File(...), password: str = Form(""), username: str = Form("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/process_single"

    query_str = request.url.query
    if query_str: target_url += f"?{query_str}"

    file_bytes = await file.read()
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}
    files = {"file": (file.filename, file_bytes, file.content_type)}
    form = await request.form()
    data = {}
    for key, value in form.multi_items():
        if key == "file" or hasattr(value, "filename"):
            continue
        data[key] = str(value)
    data.setdefault("password", password)
    data.setdefault("username", username)

    # FIX: Define the blocking request
    def make_post():
        return request_with_hf_backoff(
            requests.post,
            target_url,
            max_retries=0,
            headers=headers,
            files=files,
            data=data,
            timeout=PROCESS_PROXY_TIMEOUT_SECONDS,
        )

    try:
        # FIX: Run it in a background thread so other users can check the status!
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e)
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}


@app.post("/proxy_compatibility")
async def proxy_compatibility(request: Request, password: str = Form(""), username: str = Form("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/compatibility_check"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    form = await request.form()
    files = []
    data = {}
    for key, value in form.multi_items():
        if hasattr(value, "filename"):
            files.append(("files", (value.filename, await value.read(), value.content_type)))
        else:
            data[key] = str(value)
    data.setdefault("password", password)
    data.setdefault("username", username)

    def make_post():
        return request_with_hf_backoff(
            requests.post,
            target_url,
            max_retries=0,
            headers=headers,
            files=files,
            data=data,
            timeout=PROCESS_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e, results=[])
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "results": []}


@app.get("/proxy_experts")
async def proxy_experts(username: str = Query(""), password: str = Query("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/experts"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_get():
        return requests.get(target_url, headers=headers, params={"username": username, "password": password}, timeout=20)

    try:
        response = await run_in_threadpool(make_get)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e, experts=[])
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "experts": []}


@app.get("/proxy_training_model_options")
async def proxy_training_model_options(username: str = Query(""), password: str = Query("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/training_model_options"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_get():
        return requests.get(target_url, headers=headers, params={"username": username, "password": password}, timeout=20)

    try:
        response = await run_in_threadpool(make_get)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e, options=[])
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "options": []}


@app.post("/proxy_finetune")
async def proxy_finetune(payload: dict = Body(...)):
    username = str(payload.get("username", ""))
    expert_id = str(payload.get("expert_id", "")).strip()
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/experts/{expert_id}/finetune"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_post():
        return request_with_hf_backoff(requests.post, target_url, max_retries=0, headers=headers, json=payload, timeout=60)

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e)
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}


@app.get("/proxy_train_job")
async def proxy_train_job(job_id: str = Query(...), username: str = Query(""), password: str = Query("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/train_jobs/status/{job_id}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_get():
        return requests.get(target_url, headers=headers, params={"username": username, "password": password}, timeout=20)

    try:
        response = await run_in_threadpool(make_get)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e)
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}


@app.get("/proxy_train_jobs")
async def proxy_train_jobs(username: str = Query(""), password: str = Query("")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/train_jobs"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_get():
        return requests.get(target_url, headers=headers, params={"username": username, "password": password}, timeout=20)

    try:
        response = await run_in_threadpool(make_get)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e, jobs=[])
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "jobs": []}


DATASET_PROXY_TIMEOUT_SECONDS = 180


@app.api_route("/proxy_datasets{rest:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy_datasets(rest: str, request: Request):
    """Generic forwarder for all Labeling Studio dataset endpoints.

    Mirrors /proxy_datasets{rest} -> {base}/datasets{rest}, preserving the query
    string, body (multipart / JSON), and returning either JSON or raw binary
    (image/zip) responses. Routes dev vs prod by username (query, form, or JSON).
    """
    username = request.query_params.get("username", "")
    content_type = request.headers.get("content-type", "")

    files = []
    data = {}
    json_body = None
    if request.method in ("POST", "PUT", "DELETE"):
        if "multipart/form-data" in content_type:
            form = await request.form()
            for key, value in form.multi_items():
                if hasattr(value, "filename"):
                    files.append((key, (value.filename, await value.read(), value.content_type)))
                else:
                    data[key] = str(value)
            username = username or data.get("username", "")
        elif "application/json" in content_type:
            try:
                json_body = await request.json()
            except Exception:
                json_body = None
            if isinstance(json_body, dict):
                username = username or str(json_body.get("username", ""))

    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/datasets{rest}"
    if request.url.query:
        target_url += f"?{request.url.query}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_request():
        return request_with_hf_backoff(
            getattr(requests, request.method.lower()),
            target_url,
            max_retries=0,
            headers=headers,
            files=files or None,
            data=data or None,
            json=json_body if json_body is not None else None,
            timeout=DATASET_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_request)
    except requests.exceptions.RequestException as e:
        return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", e))
    except Exception as e:
        return JSONResponse(status_code=500, content={"success": False, "message": f"Proxy Error (Internal): {str(e)}"})

    resp_ct = response.headers.get("content-type", "application/octet-stream")
    passthrough_headers = {}
    disposition = response.headers.get("content-disposition")
    if disposition:
        passthrough_headers["content-disposition"] = disposition
    return Response(
        content=response.content,
        status_code=response.status_code,
        media_type=resp_ct,
        headers=passthrough_headers,
    )

@app.post("/proxy_batch_stage")
async def proxy_batch_stage(payload: dict = Body(...)):
    username = str(payload.get("username", ""))
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/batch_stage"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_post():
        return request_with_hf_backoff(
            requests.post,
            target_url,
            max_retries=0,
            headers=headers,
            json=payload,
            timeout=PROCESS_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e, rows=[])
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "rows": []}


@app.api_route("/proxy_process_jobs{rest:path}", methods=["GET", "POST"])
async def proxy_process_jobs(rest: str, request: Request):
    """Forward persistent processing-job uploads, polling, stop, and resume."""
    try:
        username = request.query_params.get("username", "")
        content_type = request.headers.get("content-type", "")
        files = []
        data = {}
        json_body = None
        if request.method == "POST":
            if "multipart/form-data" in content_type:
                form = await request.form()
                for key, value in form.multi_items():
                    if hasattr(value, "filename"):
                        files.append((key, (value.filename, await value.read(), value.content_type)))
                    else:
                        data[key] = str(value)
                username = username or data.get("username", "")
            elif "application/json" in content_type:
                json_body = await request.json()
                if isinstance(json_body, dict):
                    username = username or str(json_body.get("username", ""))
    except Exception as exc:
        return JSONResponse(status_code=400, content={"success": False, "message": f"Proxy Process Job Error (Request): {str(exc)}"})
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/process_jobs{rest}"
    if request.url.query:
        target_url += f"?{request.url.query}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_request():
        return request_with_hf_backoff(
            getattr(requests, request.method.lower()),
            target_url,
            max_retries=2,
            headers=headers,
            files=files or None,
            data=data or None,
            json=json_body,
            timeout=DATASET_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_request)
        return Response(
            content=response.content,
            status_code=response.status_code,
            media_type=response.headers.get("content-type", "application/json"),
        )
    except requests.exceptions.RequestException as exc:
        return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "message": f"Proxy Process Job Error (Internal): {str(exc)}"})


@app.post("/proxy_flush_queue")
async def proxy_flush_queue(payload: dict = Body(...)):
    username = str(payload.get("username", ""))
    if username.strip().lower() != "devtest":
        return {"success": False, "message": "Queue flush is only available for devtest."}
    target_url = f"{DEV_URL}/flush_queue"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_post():
        return requests.post(target_url, headers=headers, json=payload, timeout=20)

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return proxy_error_payload("Proxy Error (Hugging Face)", e)
    except Exception as e:
        return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}


@app.get("/proxy_preview/{session_id}/{row_id}/{preview_type}")
async def proxy_preview(session_id: str, row_id: str, preview_type: str, username: str = Query(""), password: str = Query(""), size: str = Query("thumb")):
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/preview/{session_id}/{row_id}/{preview_type}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_get():
        return requests.get(
            target_url,
            headers=headers,
            params={"size": size, "username": username, "password": password},
            timeout=30,
        )

    response = await run_in_threadpool(make_get)
    if response.status_code >= 400:
        return Response(content=response.content, status_code=response.status_code, media_type=response.headers.get("content-type", "text/plain"))
    return Response(content=response.content, media_type=response.headers.get("content-type", "image/jpeg"))


@app.post("/proxy_preview_session_clear")
async def proxy_preview_session_clear(payload: dict = Body(...)):
    username = str(payload.get("username", ""))
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/preview_session/clear"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_post():
        return requests.post(target_url, headers=headers, json=payload, timeout=20)

    try:
        response = await run_in_threadpool(make_post)
        response.raise_for_status()
        return response.json()
    except Exception as e:
        return {"success": False, "message": f"Proxy Error: {str(e)}"}


@app.api_route("/proxy_adjustments{rest:path}", methods=["GET", "POST", "PUT"])
async def proxy_adjustments(rest: str, request: Request):
    """Forward preview-mask and traditional-feature adjustment requests."""
    username = request.query_params.get("username", "")
    content_type = request.headers.get("content-type", "")
    files = []
    data = {}
    json_body = None
    if request.method in {"POST", "PUT"}:
        if "multipart/form-data" in content_type:
            form = await request.form()
            for key, value in form.multi_items():
                if hasattr(value, "filename"):
                    files.append((key, (value.filename, await value.read(), value.content_type)))
                else:
                    data[key] = str(value)
            username = username or data.get("username", "")
        elif "application/json" in content_type:
            json_body = await request.json()
            if isinstance(json_body, dict):
                username = username or str(json_body.get("username", ""))
    base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
    target_url = f"{base_url}/adjustments{rest}"
    if request.url.query:
        target_url += f"?{request.url.query}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    def make_request():
        return request_with_hf_backoff(
            getattr(requests, request.method.lower()),
            target_url,
            max_retries=0,
            headers=headers,
            files=files or None,
            data=data or None,
            json=json_body,
            timeout=DATASET_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_request)
        return Response(
            content=response.content,
            status_code=response.status_code,
            media_type=response.headers.get("content-type", "application/json"),
        )
    except requests.exceptions.RequestException as exc:
        return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))


SYNC_PROXY_TIMEOUT_SECONDS = 300


@app.api_route("/proxy_sync{rest:path}", methods=["GET", "POST"])
async def proxy_sync(rest: str, request: Request):
    """Forward admin sync requests to the dev backend only.

    Dev remains the controller for dev<->prod sync. The browser never receives
    production sync tokens, and this proxy never routes sync calls to prod based
    on username.
    """
    target_url = f"{DEV_URL}/sync{rest}"
    if request.url.query:
        target_url += f"?{request.url.query}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}
    content_type = request.headers.get("content-type", "")
    files = []
    data = {}
    json_body = None
    if request.method == "POST":
        if "multipart/form-data" in content_type:
            form = await request.form()
            for key, value in form.multi_items():
                if hasattr(value, "filename"):
                    files.append((key, (value.filename, await value.read(), value.content_type)))
                else:
                    data[key] = str(value)
        elif "application/json" in content_type:
            try:
                json_body = await request.json()
            except Exception:
                json_body = None

    def make_request():
        return request_with_hf_backoff(
            getattr(requests, request.method.lower()),
            target_url,
            max_retries=0,
            headers=headers,
            files=files or None,
            data=data or None,
            json=json_body,
            timeout=SYNC_PROXY_TIMEOUT_SECONDS,
        )

    try:
        response = await run_in_threadpool(make_request)
    except requests.exceptions.RequestException as exc:
        return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "message": f"Proxy Error (Internal): {str(exc)}"})

    media_type = response.headers.get("content-type", "application/json")
    passthrough_headers = {}
    disposition = response.headers.get("content-disposition")
    if disposition:
        passthrough_headers["content-disposition"] = disposition
    return Response(content=response.content, status_code=response.status_code, media_type=media_type, headers=passthrough_headers)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))