o
    @Tj                     @   s  d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ ed
dgdZejdededededededeeeefdedB dedB deej dejdB dedB de
defddZdS )zEVTON (Virtual Try-On) router: composite garments onto a person image.    N)	APIRouterDependsFileFormHTTPException
UploadFile)AsyncSession)get_dbget_vton_service)
ImageModel)VtonGenerateResponse)VtonService)fetch_image_bytesz/api/v1/vtonvton)prefixtagsz	/generate)response_model.image	image_urlgarment_idsuser_idimage_modeldbvton_svcc              
      sr  t | duo| j}t |o| }||krtdddz|r'|  I dH }	n	t| I dH }	W n tyE }
 ztdt|
dd}
~
ww z|||	|||rR|j	ndI dH }W nN t
ym }
 ztdt|
dd}
~
w ty }
 ztdt|
dd}
~
w ty }
 ztdt|
dd}
~
w ty }
 z	tdd|
 dd}
~
ww td	|j |j|j|j|jd
S )u  Generate a virtual try-on image.

    Composites the selected garments (from the wardrobe) onto the person in
    the uploaded image. Requires a detectable face. Each selected garment
    must already have a ghost-mannequin image generated.

    Accepts the person image via either ``image`` (multipart file upload) or
    ``image_url`` (HTTP/HTTPS URL — downloaded server-side to avoid browser
    CORS restrictions). Exactly one of the two must be provided.

    ``image_model`` selects the OpenAI image-generation model
    (``gpt-image-2`` / ``gpt-image-1.5``); when omitted the server default is
    used. Ignored by the Gemini provider.

    If the person photo is an upper-body crop, the LLM will silently skip
    bottom-category garments; the response reports skipped IDs separately.
    Ni  uM   image 파일 또는 image_url 중 정확히 하나를 제공해야 합니다.)status_codedetaili  i  i  u   VTON 생성 실패: z	/storage/)result_image_urlresult_image_pathused_garment_idsskipped_garment_idsllm_note)boolfilenamestripr   readr   
ValueErrorstrgeneratevalueLookupErrorRuntimeError	Exceptionr   pathused_idsskipped_idsnote)r   r   r   r   r   r   r   has_filehas_urlimage_byteseresult r5   0/home/ubuntu/htdocs/ootd-api/app/routers/vton.pygenerate_vton   sZ   
r7   )__doc__uuidfastapir   r   r   r   r   r   sqlalchemy.ext.asyncior   app.dependenciesr	   r
   app.schemas.commonr   app.schemas.vtonr   app.services.vton_servicer   app.utils.imager   routerpostr&   listUUIDr7   r5   r5   r5   r6   <module>   sB     