o
    ٷiA                     @   s   U d Z ddlZddlmZmZmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZ eeZdaee ed
< daeej ed< G dd deZG dd deZdede	fddZdS )zFastAPI HTTP Server for cache-dit.

Adapted from SGLang's HTTP server:
https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/http_server.py
    N)OptionalDictAnyListLiteral)FastAPIHTTPExceptionResponse)JSONResponse)	BaseModelField   )ModelManagerGenerateRequest)init_logger_global_model_manager_request_semaphorec                   @   s  e Zd ZU dZedddZeed< edddZe	e ed< ed	d
dddZ
eed< ed	ddddZeed< edddddZeed< edddddZeed< edddZe	ee  ed< edddZe	e ed< edd dd!dZeed"< edd#dZe	ee  ed$< edd%dddZe	e ed&< ed'd(dd)dZe	e ed*< ed+d,dZeed-< ed.d/dZed0 ed1< edd2dZe	e ed3< dS )4GenerateRequestAPIz-API request model for image/video generation..zText promptdescriptionprompt zNegative promptnegative_prompti   zImage/Video width@   i   )r   gelewidthzImage/Video heightheight2   zNumber of inference stepsr      num_inference_stepsg      @zGuidance scaleg        g      4@guidance_scaleNzdCustom sigma schedule (e.g. for turbo inference). Length should typically match num_inference_steps.sigmaszRandom seedseedzNumber of images to generate   
num_imageszInput images for image editing. Supports: URLs (http/https), local file paths, base64 strings (with or without data URI prefix)
image_urlsz%Number of frames for video generation
num_frames   z"Frames per second for video output<   fpsFzInclude stats field in responseinclude_statsbase64zOutput format: base64 or path)r,   pathoutput_formatz;Output directory when output_format=path (server-side path)
output_dir)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   intr   r    r!   floatr"   r   r#   r%   r&   r'   r*   r+   boolr.   r   r/    r9   r9   N/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/serve/api_server.pyr      s>   
 r   c                   @   s   e Zd ZU dZedddZeee  e	d< edddZ
ee e	d< edddZeeeef  e	d	< edd
dZee e	d< edddZee e	d< edddZee e	d< dS )GenerateResponseAPIz.API response model for image/video generation.Nz#Base64 encoded images or file pathsr   imagesz'Base64 encoded video (mp4) or file pathvideozCache statisticsstatszGeneration time in seconds	time_costz6Inference start time (local time with timezone offset)inference_start_timez4Inference end time (local time with timezone offset)inference_end_time)r0   r1   r2   r3   r   r<   r   listr4   r5   r=   r>   r   r   r?   r7   r@   rA   r9   r9   r9   r:   r;   8   s   
  r;   model_managerreturnc                 C   st   | a tdatdddd}|ddd }|d	d
d }|jdtdddtfdd}|ddd }|S )zCreate FastAPI application.r   zCache-DiT Serving APIz;Text-to-image model serving API with cache-dit accelerationz1.0.0)titler   versionz/healthc                      s,   t du s
t jdu rtdddS tdddS )zHealth check endpoint.N  Model not loaded)status_codecontentr   OK)r   piper	   r9   r9   r9   r:   healthS   s   zcreate_app.<locals>.healthz/get_model_infoc                      s$   t du rtdddtt  dS )zGet model information.NrG   Model manager not initializedrI   detailrJ   )r   r   r
   get_model_infor9   r9   r9   r:   rR   Z   s   z"create_app.<locals>.get_model_infoz	/generateT)response_modelresponse_model_exclude_nonerequestc                    s<  t du rtdddt jdu rtdddt4 I dH u zLt| j| j| j| j| j	| j
| j| j| j| j| j| j| j| j| jd}t }|dt j|I dH }t|j|j|j|j|j|jdW W  d  I dH  S  ty } zt !dt"|j# d	t$|  td
dt$| dd}~ww 1 I dH sw   Y  dS )z!Generate images from text prompt.NrG   rN   rO   rH   )r   r   r   r   r    r!   r"   r#   r%   r&   r'   r*   r+   r.   r/   )r<   r=   r>   r?   r@   rA   zError generating image: z:   zGeneration failed: )%r   r   rL   r   r   r   r   r   r   r    r!   r"   r#   r%   r&   r'   r*   r+   r.   r/   asyncioget_event_looprun_in_executorgenerater;   r<   r=   r>   r?   r@   rA   	Exceptionloggererrortyper0   r4   )rU   gen_requestloopresponseer9   r9   r:   rZ   b   sV   

" zcreate_app.<locals>.generatez/flush_cachec               
      st   t du s
t jdu rtdddztddidW S  ty9 }  ztdt|   td	d
t|  dd} ~ ww )zFlush cache.NrG   rH   rO   messagezCache flushed successfullyrQ   zError flushing cache: rV   zFailed to flush cache: )r   rL   r   r
   r[   r\   r]   r4   )rb   r9   r9   r:   flush_cache   s   zcreate_app.<locals>.flush_cache)	r   rW   	Semaphorer   r   getpostr;   r   )rC   apprM   rR   rZ   rd   r9   r9   r:   
create_appG   s    


.
ri   )r3   rW   typingr   r   r   r   r   fastapir   r   r	   fastapi.responsesr
   pydanticr   r   rC   r   r   cache_dit.loggerr   r0   r\   r   r5   r   re   r   r;   ri   r9   r9   r9   r:   <module>   s    "