-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathapp.py
More file actions
1247 lines (1117 loc) · 43.1 KB
/
app.py
File metadata and controls
1247 lines (1117 loc) · 43.1 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 os
import json
import logging
import time
import uuid
import threading
import traceback
import base64
import binascii
import io
from dataclasses import asdict
from pathlib import Path
from typing import Optional, Any, Callable
from urllib.parse import unquote_to_bytes
import requests
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from starlette.background import BackgroundTask
from starlette.middleware.sessions import SessionMiddleware
from api.routes.admin import build_admin_router
from api.routes.generation import build_generation_router
try:
from PIL import Image
except Exception:
Image = None
from core.adobe_client import (
AdobeClient,
AuthError,
QuotaExhaustedError,
UpstreamTemporaryError,
)
from core.token_mgr import token_manager
from core.config_mgr import config_manager
from core.refresh_mgr import refresh_manager
from core.stores import (
ErrorDetailRecord,
ErrorDetailStore,
JobStore,
LiveRequestStore,
RequestLogRecord,
RequestLogStore,
)
from core.models import (
MODEL_CATALOG,
SUPPORTED_RATIOS,
VIDEO_MODEL_CATALOG,
resolve_model,
resolve_ratio_and_resolution,
)
logger = logging.getLogger("adobe2api")
BASE_DIR = Path(__file__).resolve().parent
STATIC_DIR = BASE_DIR / "static"
DATA_DIR = BASE_DIR / "data"
GENERATED_DIR = DATA_DIR / "generated"
GENERATED_DIR.mkdir(parents=True, exist_ok=True)
_GENERATED_RECONCILE_INTERVAL_SEC = 300
_generated_storage_lock = threading.Lock()
_generated_prune_lock = threading.Lock()
_generated_usage_bytes = 0
_generated_file_count = 0
_generated_last_reconcile_ts = 0.0
def _drop_generated_file_cache(file_path: Path) -> None:
if not hasattr(os, "posix_fadvise"):
return
if not file_path.exists():
return
try:
flag = getattr(os, "POSIX_FADV_DONTNEED", 4)
with file_path.open("rb") as f:
os.posix_fadvise(f.fileno(), 0, 0, flag)
except Exception:
return
# 极简配置启动
app = FastAPI(
title="adobe2api",
version="0.1.0",
docs_url=None, # 关闭 swagger,节省资源
redoc_url=None,
)
session_secret = str(
os.getenv("ADOBE_ADMIN_SESSION_SECRET")
or config_manager.get("admin_session_secret")
or "adobe2api-dev-session-secret"
).strip()
app.add_middleware(
SessionMiddleware,
secret_key=session_secret,
session_cookie="adobe2api_session",
same_site="lax",
https_only=False,
)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
@app.get("/generated/{filename:path}", include_in_schema=False)
def serve_generated_file(filename: str):
raw = str(filename or "").strip()
safe_name = Path(raw).name
if not safe_name or safe_name != raw:
raise HTTPException(status_code=404, detail="file not found")
target = GENERATED_DIR / safe_name
if not target.exists() or not target.is_file():
raise HTTPException(status_code=404, detail="file not found")
background = BackgroundTask(_drop_generated_file_cache, target)
return FileResponse(path=target, filename=safe_name, background=background)
store = JobStore()
log_store = RequestLogStore(DATA_DIR / "request_logs.jsonl", max_items=5000)
error_store = ErrorDetailStore(DATA_DIR / "request_errors.jsonl", max_items=5000)
live_log_store = LiveRequestStore(max_items=2000)
client = AdobeClient()
refresh_manager.start()
def _extract_logging_fields(raw_body: bytes) -> dict[str, Optional[str]]:
if not raw_body:
return {"model": None, "prompt_preview": None}
try:
import json
data: Any = json.loads(raw_body.decode("utf-8"))
if not isinstance(data, dict):
return {"model": None, "prompt_preview": None}
model = str(data.get("model") or "").strip() or None
prompt = str(data.get("prompt") or "").strip()
if not prompt:
prompt = _extract_prompt_from_messages(data.get("messages") or [])
if prompt:
prompt = prompt.replace("\r", " ").replace("\n", " ").strip()
prompt = prompt[:180]
return {"model": model, "prompt_preview": prompt or None}
except Exception:
return {"model": None, "prompt_preview": None}
def _upsert_live_request(request: Request, patch: dict) -> None:
try:
log_id = str(getattr(request.state, "log_id", "") or "").strip()
if not log_id or not isinstance(patch, dict):
return
live_log_store.upsert(log_id, patch)
except Exception:
pass
def _set_request_preview(request: Request, url: str, kind: str = "image") -> None:
if not url:
return
try:
request.state.log_preview_url = url
request.state.log_preview_kind = kind
_upsert_live_request(
request,
{
"preview_url": url,
"preview_kind": kind,
"ts": time.time(),
},
)
except Exception:
pass
def _set_request_error_detail(
request: Request,
*,
error: Exception | str,
status_code: Optional[int] = None,
error_type: Optional[str] = None,
include_traceback: bool = False,
) -> str:
code = f"ERR-{uuid.uuid4().hex[:10].upper()}"
message = str(error or "Unknown error").strip() or "Unknown error"
trace_text = None
error_class = None
if isinstance(error, Exception):
error_class = type(error).__name__
if include_traceback:
trace_text = traceback.format_exc()
if not trace_text or trace_text.strip() == "NoneType: None":
trace_text = "".join(
traceback.format_exception(type(error), error, error.__traceback__)
)
elif include_traceback:
trace_text = traceback.format_exc()
if trace_text and trace_text.strip() == "NoneType: None":
trace_text = None
op_map = {
"/v1/chat/completions": "chat.completions",
"/v1/images/generations": "images.generations",
"/api/v1/generate": "api.generate",
}
path = str(getattr(getattr(request, "url", None), "path", "") or "")
operation = op_map.get(path, "")
record = ErrorDetailRecord(
code=code,
ts=time.time(),
message=message,
error_type=(str(error_type or "").strip() or None),
status_code=int(status_code) if status_code is not None else None,
operation=operation or None,
method=str(getattr(request, "method", "") or "").upper() or None,
path=path or None,
log_id=str(getattr(request.state, "log_id", "") or "") or None,
model=str(getattr(request.state, "log_model", "") or "") or None,
prompt_preview=(
str(getattr(request.state, "log_prompt_preview", "") or "") or None
),
task_status=str(getattr(request.state, "log_task_status", "") or "") or None,
task_progress=getattr(request.state, "log_task_progress", None),
upstream_job_id=(
str(getattr(request.state, "log_upstream_job_id", "") or "") or None
),
token_id=str(getattr(request.state, "log_token_id", "") or "") or None,
token_account_name=(
str(getattr(request.state, "log_token_account_name", "") or "") or None
),
token_account_email=(
str(getattr(request.state, "log_token_account_email", "") or "") or None
),
token_source=str(getattr(request.state, "log_token_source", "") or "") or None,
token_attempt=getattr(request.state, "log_token_attempt", None),
exception_class=error_class,
traceback=(str(trace_text or "") or None),
)
error_store.add(record)
request.state.log_error = message[:240]
request.state.log_error_code = code
_upsert_live_request(
request,
{
"error": message[:240],
"error_code": code,
"ts": time.time(),
},
)
return code
def _set_request_task_progress(
request: Request,
task_status: str,
task_progress: Optional[float] = None,
upstream_job_id: Optional[str] = None,
retry_after: Optional[int] = None,
error: Optional[str] = None,
) -> None:
patch: dict[str, Any] = {"task_status": str(task_status or "").upper()}
if task_progress is not None:
try:
progress_val = float(task_progress)
if progress_val < 0:
progress_val = 0.0
if progress_val > 100:
progress_val = 100.0
patch["task_progress"] = round(progress_val, 2)
except Exception:
pass
if upstream_job_id:
patch["upstream_job_id"] = str(upstream_job_id)
if retry_after is not None:
try:
patch["retry_after"] = int(retry_after)
except Exception:
pass
if error:
patch["error"] = str(error)[:240]
try:
request.state.log_task_status = patch.get("task_status")
request.state.log_task_progress = patch.get("task_progress")
request.state.log_upstream_job_id = patch.get("upstream_job_id")
request.state.log_retry_after = patch.get("retry_after")
if patch.get("error"):
request.state.log_error = patch.get("error")
_upsert_live_request(
request,
{
"task_status": patch.get("task_status"),
"task_progress": patch.get("task_progress"),
"upstream_job_id": patch.get("upstream_job_id"),
"retry_after": patch.get("retry_after"),
"error": patch.get("error"),
"error_code": getattr(request.state, "log_error_code", None),
"model": getattr(request.state, "log_model", None),
"prompt_preview": getattr(request.state, "log_prompt_preview", None),
"ts": time.time(),
},
)
except Exception:
pass
# Do not write partial records here.
# Final request logs are emitted either by per-attempt logging
# (_append_attempt_log) or by middleware finalization.
def _set_request_token_context(request: Request, token: str, attempt: int) -> dict:
meta = token_manager.get_meta_by_value(token)
try:
request.state.log_token_id = meta.get("token_id")
request.state.log_token_account_name = meta.get("token_account_name")
request.state.log_token_account_email = meta.get("token_account_email")
request.state.log_token_source = meta.get("token_source")
request.state.log_token_attempt = int(attempt)
_upsert_live_request(
request,
{
"token_id": meta.get("token_id"),
"token_account_name": meta.get("token_account_name"),
"token_account_email": meta.get("token_account_email"),
"token_source": meta.get("token_source"),
"token_attempt": int(attempt),
"ts": time.time(),
},
)
except Exception:
pass
return meta
def _append_attempt_log(
request: Request,
operation: str,
token_meta: dict,
attempt: int,
attempt_started: float,
status_code: int,
error: Optional[str] = None,
error_code: Optional[str] = None,
task_status_override: Optional[str] = None,
) -> None:
try:
root_log_id = str(getattr(request.state, "log_id", "") or uuid.uuid4().hex[:12])
attempt_id = f"{root_log_id}-a{attempt}"
method = str(getattr(request, "method", "POST") or "POST").upper()
path = str(getattr(getattr(request, "url", None), "path", "") or "")
model = getattr(request.state, "log_model", None)
prompt_preview = getattr(request.state, "log_prompt_preview", None)
preview_url = getattr(request.state, "log_preview_url", None)
preview_kind = getattr(request.state, "log_preview_kind", None)
task_status = task_status_override
if task_status is None:
task_status = getattr(request.state, "log_task_status", None)
task_status = str(task_status or "").upper() or None
task_progress = getattr(request.state, "log_task_progress", None)
upstream_job_id = getattr(request.state, "log_upstream_job_id", None)
retry_after = getattr(request.state, "log_retry_after", None)
duration_sec = int(max(0.0, time.time() - float(attempt_started)))
payload = asdict(
RequestLogRecord(
id=attempt_id,
ts=time.time(),
method=method,
path=path,
status_code=int(status_code),
duration_sec=duration_sec,
operation=operation,
preview_url=preview_url,
preview_kind=preview_kind,
model=model,
prompt_preview=prompt_preview,
error=(str(error)[:240] if error else None),
error_code=(str(error_code or "") or None),
task_status=task_status,
task_progress=task_progress,
upstream_job_id=upstream_job_id,
retry_after=retry_after,
token_id=str(token_meta.get("token_id") or "") or None,
token_account_name=(
str(token_meta.get("token_account_name") or "") or None
),
token_account_email=(
str(token_meta.get("token_account_email") or "") or None
),
token_source=str(token_meta.get("token_source") or "") or None,
token_attempt=int(attempt),
)
)
records = getattr(request.state, "log_attempt_records", None)
if not isinstance(records, list):
records = []
request.state.log_attempt_records = records
records.append(payload)
request.state.log_has_attempt_logs = True
except Exception:
pass
@app.middleware("http")
async def request_logger(request: Request, call_next):
started = time.time()
method = request.method.upper()
path = request.url.path
preview_url = None
preview_kind = None
raw_body = b""
body_meta = {"model": None, "prompt_preview": None}
error_text = None
status_code = 500
op_map = {
"/v1/chat/completions": "chat.completions",
"/v1/images/generations": "images.generations",
}
operation = op_map.get(path, "")
should_log = bool(operation)
if method in {"POST", "PUT", "PATCH"} and should_log:
try:
raw_body = await request.body()
request._body = raw_body
if path in {
"/v1/images/generations",
"/v1/chat/completions",
"/api/v1/generate",
}:
body_meta = _extract_logging_fields(raw_body)
request.state.log_model = body_meta.get("model")
request.state.log_prompt_preview = body_meta.get("prompt_preview")
request.state.log_id = uuid.uuid4().hex[:12]
log_id = str(getattr(request.state, "log_id", "") or "")
if log_id:
live_log_store.upsert(
log_id,
{
"id": log_id,
"ts": time.time(),
"method": method,
"path": path,
"status_code": 102,
"duration_sec": 0,
"operation": operation,
"model": body_meta.get("model"),
"prompt_preview": body_meta.get("prompt_preview"),
"task_status": "IN_PROGRESS",
"task_progress": 0.0,
},
)
except Exception:
pass
response = None
try:
response = await call_next(request)
status_code = response.status_code
except Exception as exc:
_set_request_error_detail(
request,
error=exc,
status_code=500,
error_type="server_error",
include_traceback=True,
)
error_text = f"{type(exc).__name__}: {str(exc)}"[:240]
logger.exception(
"Unhandled exception in request pipeline method=%s path=%s log_id=%s",
method,
path,
getattr(request.state, "log_id", ""),
)
raise
finally:
if should_log:
has_attempt_logs = bool(
getattr(request.state, "log_has_attempt_logs", False)
)
log_id = (
str(getattr(request.state, "log_id", "") or "") or uuid.uuid4().hex[:12]
)
live_log_store.remove(log_id)
attempt_records = getattr(request.state, "log_attempt_records", None)
if isinstance(attempt_records, list) and attempt_records:
for payload in attempt_records:
log_store.add_payload(payload)
if not has_attempt_logs:
duration_sec = int(time.time() - started)
preview_url = getattr(request.state, "log_preview_url", None)
preview_kind = getattr(request.state, "log_preview_kind", None)
task_status = getattr(request.state, "log_task_status", None)
task_progress = getattr(request.state, "log_task_progress", None)
upstream_job_id = getattr(request.state, "log_upstream_job_id", None)
retry_after = getattr(request.state, "log_retry_after", None)
error_final = getattr(request.state, "log_error", None) or error_text
error_code = getattr(request.state, "log_error_code", None)
if int(status_code or 0) >= 400 and not error_code:
generated_error_type = (
"invalid_request_error"
if 400 <= int(status_code or 0) < 500
else "server_error"
)
error_code = _set_request_error_detail(
request,
error=error_final or f"HTTP {status_code}",
status_code=int(status_code or 500),
error_type=generated_error_type,
include_traceback=False,
)
token_id = getattr(request.state, "log_token_id", None)
token_account_name = getattr(
request.state, "log_token_account_name", None
)
token_account_email = getattr(
request.state, "log_token_account_email", None
)
token_source = getattr(request.state, "log_token_source", None)
token_attempt = getattr(request.state, "log_token_attempt", None)
log_id = (
str(getattr(request.state, "log_id", "") or "")
or uuid.uuid4().hex[:12]
)
log_store.upsert(
log_id,
asdict(
RequestLogRecord(
id=log_id,
ts=time.time(),
method=method,
path=path,
status_code=status_code,
duration_sec=duration_sec,
operation=operation,
preview_url=preview_url,
preview_kind=preview_kind,
model=body_meta.get("model"),
prompt_preview=body_meta.get("prompt_preview"),
error=error_final,
error_code=error_code,
task_status=task_status,
task_progress=task_progress,
upstream_job_id=upstream_job_id,
retry_after=retry_after,
token_id=token_id,
token_account_name=token_account_name,
token_account_email=token_account_email,
token_source=token_source,
token_attempt=token_attempt,
)
),
)
return response
def _resolve_video_options(data: dict) -> tuple[bool, str, str]:
generate_audio = bool(data.get("generate_audio", data.get("generateAudio", True)))
negative_prompt = str(
data.get("negative_prompt") or data.get("negativePrompt") or ""
).strip()
reference_mode = (
str(
data.get("video_reference_mode")
or data.get("videoReferenceMode")
or data.get("reference_mode")
or data.get("referenceMode")
or "frame"
)
.strip()
.lower()
)
if reference_mode not in {"frame", "image"}:
reference_mode = "frame"
return generate_audio, negative_prompt, reference_mode
def _run_with_token_retries(
request: Request,
operation_name: str,
run_once: Callable[[str], Any],
set_request_error_detail: Optional[Callable[..., str]] = None,
) -> Any:
max_attempts = client.retry_max_attempts if client.retry_enabled else 1
max_attempts = max(1, int(max_attempts))
last_exc: Optional[Exception] = None
report_error = set_request_error_detail or _set_request_error_detail
for attempt in range(1, max_attempts + 1):
token = token_manager.get_available(strategy=client.token_rotation_strategy)
if not token:
break
token_meta = _set_request_token_context(request, token, attempt)
attempt_started = time.time()
try:
result = run_once(token)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=200,
task_status_override="COMPLETED",
)
return result
except QuotaExhaustedError as exc:
token_manager.report_exhausted(token)
last_exc = exc
retryable = attempt < max_attempts
retry_reason = "quota_exhausted"
err_code = report_error(
request,
error=exc,
status_code=429,
error_type="rate_limit_error",
include_traceback=False,
)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=429,
error=str(exc),
error_code=err_code,
task_status_override="FAILED",
)
except AuthError as exc:
token_manager.report_invalid(token)
last_exc = exc
retryable = attempt < max_attempts
retry_reason = "auth"
err_code = report_error(
request,
error=exc,
status_code=401,
error_type="authentication_error",
include_traceback=False,
)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=401,
error=str(exc),
error_code=err_code,
task_status_override="FAILED",
)
except UpstreamTemporaryError as exc:
last_exc = exc
retryable = attempt < max_attempts and client.should_retry_temporary_error(
exc
)
status_part = f"status={exc.status_code}" if exc.status_code else "status=?"
type_part = f"type={exc.error_type or 'temporary'}"
retry_reason = f"upstream_temporary {status_part} {type_part}"
err_code = report_error(
request,
error=exc,
status_code=int(exc.status_code or 503),
error_type=str(exc.error_type or "server_error"),
include_traceback=False,
)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=int(exc.status_code or 503),
error=str(exc),
error_code=err_code,
task_status_override="FAILED",
)
except HTTPException as exc:
err_code = report_error(
request,
error=str(exc.detail),
status_code=int(exc.status_code or 500),
error_type=(
"invalid_request_error"
if 400 <= int(exc.status_code or 500) < 500
else "server_error"
),
include_traceback=False,
)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=int(exc.status_code or 500),
error=str(exc.detail),
error_code=err_code,
task_status_override="FAILED",
)
raise
except Exception as exc:
err_code = report_error(
request,
error=exc,
status_code=500,
error_type="server_error",
include_traceback=True,
)
_append_attempt_log(
request=request,
operation=operation_name,
token_meta=token_meta,
attempt=attempt,
attempt_started=attempt_started,
status_code=500,
error="Unhandled runtime error",
error_code=err_code,
task_status_override="FAILED",
)
raise
if retryable:
delay = client._retry_delay_for_attempt(attempt)
logger.warning(
"retrying operation=%s attempt=%s/%s reason=%s delay=%.2fs strategy=%s",
operation_name,
attempt,
max_attempts,
retry_reason,
delay,
client.token_rotation_strategy,
)
_set_request_task_progress(
request,
task_status="IN_PROGRESS",
error=f"retry {attempt}/{max_attempts}: {retry_reason}",
)
if delay > 0:
time.sleep(delay)
continue
break
if last_exc is not None:
raise last_exc
raise HTTPException(
status_code=503, detail="No active tokens available in the pool"
)
def _extract_prompt_from_messages(messages) -> str:
if not isinstance(messages, list):
return ""
for msg in reversed(messages):
if not isinstance(msg, dict):
continue
if msg.get("role") != "user":
continue
chunks = []
content = msg.get("content")
if isinstance(content, str):
if content.strip():
chunks.append(content.strip())
elif isinstance(content, list):
for part in content:
if isinstance(part, dict) and part.get("type") == "text":
txt = str(part.get("text") or "").strip()
if txt:
chunks.append(txt)
return "\n".join(chunks).strip()
return ""
def _data_url_to_bytes(url: str) -> tuple[bytes, str]:
raw = str(url or "").strip()
if not raw.startswith("data:"):
raise ValueError("not a data url")
head, sep, body = raw.partition(",")
if not sep:
raise ValueError("invalid data url")
mime_type = "image/jpeg"
mime_part = head[5:]
if ";" in mime_part:
mime_type = (mime_part.split(";", 1)[0] or "image/jpeg").strip()
elif mime_part:
mime_type = mime_part.strip()
if ";base64" in head:
try:
return base64.b64decode(body, validate=True), mime_type
except binascii.Error:
raise ValueError("invalid base64 image data")
return unquote_to_bytes(body), mime_type
def _extract_image_urls_from_messages(messages, max_items: int = 6) -> list[str]:
urls: list[str] = []
if not isinstance(messages, list):
return urls
for msg in reversed(messages):
if not isinstance(msg, dict):
continue
if msg.get("role") != "user":
continue
content = msg.get("content")
if not isinstance(content, list):
return urls
for part in content:
if not isinstance(part, dict):
continue
if part.get("type") != "image_url":
continue
image_url = part.get("image_url")
if isinstance(image_url, str):
image_url = image_url.strip()
elif isinstance(image_url, dict):
image_url = str(image_url.get("url") or "").strip()
else:
image_url = ""
if image_url:
urls.append(image_url)
if len(urls) >= max_items:
return urls
return urls
return urls
def _normalize_image_mime(mime_type: str) -> str:
allowed = {"image/jpeg", "image/jpg", "image/png", "image/webp"}
normalized = str(mime_type or "").lower()
if normalized == "image/jpg":
normalized = "image/jpeg"
if normalized not in allowed:
normalized = "image/jpeg"
return normalized
def _load_input_images(messages) -> list[tuple[bytes, str]]:
image_urls = _extract_image_urls_from_messages(messages, max_items=6)
if not image_urls:
return []
loaded: list[tuple[bytes, str]] = []
for image_url in image_urls:
if image_url.startswith("data:"):
try:
image_bytes, mime_type = _data_url_to_bytes(image_url)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
else:
if not image_url.lower().startswith(("http://", "https://")):
raise HTTPException(
status_code=400,
detail="Only http/https or data URL images are supported",
)
resp = requests.get(image_url, timeout=30)
if resp.status_code != 200:
raise HTTPException(
status_code=400,
detail=f"Failed to fetch image_url: {resp.status_code}",
)
image_bytes = resp.content
mime_type = (resp.headers.get("content-type") or "image/jpeg").split(";")[
0
].strip() or "image/jpeg"
if not image_bytes:
raise HTTPException(status_code=400, detail="image_url is empty")
if len(image_bytes) > 10 * 1024 * 1024:
raise HTTPException(status_code=400, detail="image too large, max 10MB")
loaded.append((image_bytes, _normalize_image_mime(mime_type)))
return loaded
def _prepare_video_source_image(
image_bytes: bytes, aspect_ratio: str, resolution: str = "720p"
) -> tuple[bytes, str]:
if not image_bytes:
raise HTTPException(status_code=400, detail="image_url is empty")
if Image is None:
raise HTTPException(
status_code=500,
detail="Pillow is required for video image preprocessing (resize/crop)",
)
res = str(resolution or "720p").lower()
if res == "1080p":
target_size = (1920, 1080) if aspect_ratio == "16:9" else (1080, 1920)
else:
target_size = (1280, 720) if aspect_ratio == "16:9" else (720, 1280)
try:
with Image.open(io.BytesIO(image_bytes)) as src:
src = src.convert("RGB")
src_ratio = src.width / max(1, src.height)
tgt_ratio = target_size[0] / target_size[1]
if src_ratio > tgt_ratio:
new_h = target_size[1]
new_w = int(new_h * src_ratio)
else:
new_w = target_size[0]
new_h = int(new_w / max(src_ratio, 1e-6))
resized = src.resize((new_w, new_h), Image.Resampling.LANCZOS)
left = max(0, (new_w - target_size[0]) // 2)
top = max(0, (new_h - target_size[1]) // 2)
cropped = resized.crop(
(left, top, left + target_size[0], top + target_size[1])
)
out = io.BytesIO()
cropped.save(out, format="PNG")
return out.getvalue(), "image/png"
except HTTPException:
raise
except Exception as exc:
raise HTTPException(status_code=400, detail=f"invalid image for video: {exc}")
def _extract_access_key(request: Request) -> str:
auth = (request.headers.get("authorization") or "").strip()
if auth.lower().startswith("bearer "):
return auth[7:].strip()
return (request.headers.get("x-api-key") or "").strip()
def _require_service_api_key(request: Request) -> None:
required = str(config_manager.get("api_key", "")).strip()
if not required:
return
provided = _extract_access_key(request)
if provided != required:
raise HTTPException(status_code=401, detail="Invalid API key")
def _is_admin_authenticated(request: Request) -> bool:
sess = request.session or {}
if not bool(sess.get("admin_auth")):
return False
username = str(sess.get("username") or "").strip()
required_username = str(
config_manager.get("admin_username", "admin") or "admin"
).strip()
return bool(username) and username == required_username
def _require_admin_auth(request: Request) -> None:
if not _is_admin_authenticated(request):
raise HTTPException(status_code=401, detail="Unauthorized")
def _apply_client_config() -> None:
client.apply_config(config_manager.get_all())
def _public_image_url(request: Request, job_id: str) -> str:
return _public_generated_url(request, f"{job_id}.png")
def _public_generated_url(request: Request, filename: str) -> str:
safe_name = str(filename or "").lstrip("/")
path = f"/generated/{safe_name}"
config_base = str(config_manager.get("public_base_url", "") or "").strip()
if config_base:
return f"{config_base.rstrip('/')}{path}"
override = str(
os.getenv("ADOBE_PUBLIC_BASE_URL") or os.getenv("PUBLIC_BASE_URL") or ""
).strip()
if override:
return f"{override.rstrip('/')}{path}"
forwarded_host = str(request.headers.get("x-forwarded-host") or "").strip()
if forwarded_host:
forwarded_proto = str(
request.headers.get("x-forwarded-proto") or "http"
).strip()
forwarded_prefix = str(request.headers.get("x-forwarded-prefix") or "").strip()
if forwarded_prefix and not forwarded_prefix.startswith("/"):
forwarded_prefix = f"/{forwarded_prefix}"
forwarded_prefix = forwarded_prefix.rstrip("/")
return f"{forwarded_proto}://{forwarded_host}{forwarded_prefix}{path}"
return f"{str(request.base_url).rstrip('/')}{path}"
def _scan_generated_dir() -> tuple[list[tuple[Path, int, float]], int, int]: