fix: align Freepik API paths with OpenAPI spec
Some checks failed
Build and Push Docker Image / build (push) Failing after 9s
Some checks failed
Build and Push Docker Image / build (push) Failing after 9s
The original implementation used guessed endpoint paths that don't match
the actual Freepik API. Key fixes based on their OpenAPI spec:
- Task polling is per-endpoint (e.g. GET /v1/ai/text-to-image/flux-dev/{task-id})
not a generic /v1/ai/tasks/{id}. freepik_client now returns TaskResult
with status_path, and task_tracker polls using that path.
- Fixed endpoint paths: flux-pro -> flux-pro-v1-1, upscale -> image-upscaler,
relight -> image-relight, style-transfer -> image-style-transfer,
expand -> image-expand/flux-pro, inpaint -> ideogram-image-edit,
remove-background -> beta/remove-background, classifier -> classifier/image,
audio-isolate -> audio-isolation, icon -> text-to-icon
- Fixed video paths: kling -> kling-o1-pro with kling-o1 status path,
minimax -> minimax-hailuo-02-1080p, seedance -> seedance-pro-1080p
- Fixed request schemas to match actual API params (e.g. scale_factor
not scale, reference_image not style_reference, image_url for bg removal)
- Fixed response parsing: status is uppercase (COMPLETED not completed),
results in data.generated[] array, classifier returns [{class_name, probability}]
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,8 @@ from app.schemas.image_generation import (
|
||||
MysticRequest,
|
||||
SeedreamRequest,
|
||||
)
|
||||
from app.services import freepik_client, task_tracker
|
||||
from app.services import task_tracker
|
||||
from app.services.freepik_client import TaskResult, _extract_task_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -19,17 +20,16 @@ router = APIRouter(prefix='/api/v1/generate/image', tags=['image-generation'])
|
||||
|
||||
|
||||
async def _submit_and_respond(
|
||||
result: dict,
|
||||
result: TaskResult,
|
||||
sync: bool,
|
||||
metadata: dict | None = None,
|
||||
) -> TaskResponse | TaskDetail:
|
||||
"""Extract task_id from Freepik response, track it, optionally wait."""
|
||||
data = result.get('data', result)
|
||||
freepik_task_id = str(data.get('task_id') or data.get('id', ''))
|
||||
freepik_task_id = _extract_task_id(result.data)
|
||||
if not freepik_task_id:
|
||||
raise HTTPException(status_code=502, detail='No task_id in Freepik response')
|
||||
|
||||
internal_id = task_tracker.submit(freepik_task_id, metadata)
|
||||
internal_id = task_tracker.submit(freepik_task_id, result.status_path, metadata)
|
||||
|
||||
if not sync:
|
||||
return TaskResponse(
|
||||
@@ -64,24 +64,27 @@ async def _submit_and_respond(
|
||||
|
||||
@router.post('/mystic', response_model=TaskResponse)
|
||||
async def generate_mystic(request: MysticRequest, sync: bool = Query(False)):
|
||||
from app.services import freepik_client
|
||||
result = await freepik_client.generate_mystic(
|
||||
prompt=request.prompt,
|
||||
negative_prompt=request.negative_prompt,
|
||||
resolution=request.resolution,
|
||||
styling=request.styling,
|
||||
aspect_ratio=request.aspect_ratio,
|
||||
model=request.model,
|
||||
seed=request.seed,
|
||||
num_images=request.num_images,
|
||||
styling=request.styling,
|
||||
structure_reference=request.structure_reference,
|
||||
style_reference=request.style_reference,
|
||||
)
|
||||
return await _submit_and_respond(result, sync, {'model': 'mystic'})
|
||||
|
||||
|
||||
@router.post('/flux-dev', response_model=TaskResponse)
|
||||
async def generate_flux_dev(request: FluxDevRequest, sync: bool = Query(False)):
|
||||
from app.services import freepik_client
|
||||
result = await freepik_client.generate_flux_dev(
|
||||
prompt=request.prompt,
|
||||
image=request.image,
|
||||
guidance_scale=request.guidance_scale,
|
||||
num_images=request.num_images,
|
||||
aspect_ratio=request.aspect_ratio,
|
||||
styling=request.styling,
|
||||
seed=request.seed,
|
||||
)
|
||||
return await _submit_and_respond(result, sync, {'model': 'flux-dev'})
|
||||
@@ -89,10 +92,11 @@ async def generate_flux_dev(request: FluxDevRequest, sync: bool = Query(False)):
|
||||
|
||||
@router.post('/flux-pro', response_model=TaskResponse)
|
||||
async def generate_flux_pro(request: FluxProRequest, sync: bool = Query(False)):
|
||||
from app.services import freepik_client
|
||||
result = await freepik_client.generate_flux_pro(
|
||||
prompt=request.prompt,
|
||||
image=request.image,
|
||||
guidance_scale=request.guidance_scale,
|
||||
aspect_ratio=request.aspect_ratio,
|
||||
styling=request.styling,
|
||||
seed=request.seed,
|
||||
)
|
||||
return await _submit_and_respond(result, sync, {'model': 'flux-pro'})
|
||||
@@ -100,11 +104,10 @@ async def generate_flux_pro(request: FluxProRequest, sync: bool = Query(False)):
|
||||
|
||||
@router.post('/seedream', response_model=TaskResponse)
|
||||
async def generate_seedream(request: SeedreamRequest, sync: bool = Query(False)):
|
||||
from app.services import freepik_client
|
||||
result = await freepik_client.generate_seedream(
|
||||
prompt=request.prompt,
|
||||
image=request.image,
|
||||
aspect_ratio=request.aspect_ratio,
|
||||
num_images=request.num_images,
|
||||
seed=request.seed,
|
||||
)
|
||||
return await _submit_and_respond(result, sync, {'model': 'seedream'})
|
||||
|
||||
Reference in New Issue
Block a user