o
    پi$                     @   s   d dl Z d dlZd dlmZ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eZde jfd	d
ZdefddZdS )    N)DataTypeSamplingParams)post_process_sampleprepare_request)sync_scheduler_client)
ServerArgs)init_logger)envsparserc                 C   s   t | } t| } | S )z+Add the arguments for the generate command.)r   add_cli_argsr   )r
    r   Y/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/apps/webui/main.pyadd_webui_args   s   

r   server_argsc           "         s  dd l }dtdtfdd}| j}tj r1ddlm} | }||}|j	d d 
dd	}ndd
lm} ||j}t  |dv rHd}	n|dv rOd}	ntd| d j d|	dk}
|	dk} fdd}| d}|d |  |jd jd}|jd|d}W d    n1 sw   Y  | R |jdd |jddd}|jddd}W d    n1 sw   Y  |jdd |jddd d!}|jd"d#d$d%}W d    n1 sw   Y  W d    n1 sw   Y  |  | P |jd&dd'd!}|jd(dd)d!}|jdd*d+dd,d-}|jd.d/d0d1d2d-}|jdd3d4dd5|
d6}|jdd7d8dd9|
d6}|jd:d;d<}|jd=d>d}W d    n	1 sRw   Y  |  |jd?|d@dA}|jdB|
dC}W d    n	1 svw   Y  W d    n	1 sw   Y  |j||||||||||||g||gdD |j jdEdEdEdF\}}}dG} |pdH j }!tdI|  dJ|! dK|  dI |  W d    d S 1 sw   Y  d S )LNr   
model_pathreturnc                 S   s   ddl m} ddlm}m} z||  | W S  |y   Y nw ||  }|j}t|dk r5td|  |d  d|d  }|| |S )	Nr   )Path)HFValidationErrorvalidate_repo_id   zInvalid model_path: /)	pathlibr   huggingface_hub.utilsr   r   
expanduserpartslen
ValueError)r   r   r   r   pr   	candidater   r   r   resolve_model_repo_id    s   z6run_sgl_diffusion_webui.<locals>.resolve_model_repo_id)HubApiNamez
-synthesis )
model_info)ztext-to-videozimage-to-videozvideo-to-videovideo)ztext-to-imagezimage-to-imageimagezThe task name z
 of model z7 is not a valid task name. Please check the model path.c                    sV  |rd|v rt d|  |dd}dd |dD }nd}t| ||||||||	||
d}tj jfd i|}t |d	}t	
|g}ttj|j|j}|jdu rd
dd | D }d| }|jtjkrtj|r~t | d|fS |d| 7 }t|t|t|jd |j|j|j|}|jtjkrd|fS |d dfS )a%  
        NOTE: The input and output of function which is called by gradio button must be gradio components
        So we use global variable sampling_params_kwargs to avoid pass this param, because gradio does not support this.
        return [ np.ndarray, None ] | [None, np.ndarray]
        u   ，zkWarning: please use English comma to separate the reference image paths, and the reference image paths is: ,c                 S   s   g | ]}|  qS r   )strip).0pathr   r   r   
<listcomp>h   s    zDrun_sgl_diffusion_webui.<locals>.gradio_generate.<locals>.<listcomp>N)promptnegative_prompt
image_pathseed
num_framesfpswidthheightguidance_scalenum_inference_stepsenable_teacacher   )r   sampling_params
c                 S   s   g | ]\}}| d | qS )z: r   )r*   keyvaluer   r   r   r,      s    z@No output is generated by client, and their sampling params is: z0
And the expected output file was not found at: r   )loggerwarningreplacesplitdictr   from_user_sampling_params_argsr   r   r   forwardstrosr+   joinoutput_pathoutput_file_nameoutputitems	data_typer   VIDEOexistsr   r   r2   save_output)r-   r.   reference_image_paths_strr0   r1   frames_per_secondr3   r4   r6   r5   r7   r/   sampling_params_kwargsr8   batchresultsave_file_pathsampling_params_strno_output_msgframesr   r   r   gradio_generateP   sp   


z0run_sgl_diffusion_webui.<locals>.gradio_generateu#   # 🚀 SGLang Diffusion ApplicationModel)labelr;   z	Task name   )scalePromptzA curious raccoonNegative_prompta  Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards   r0   i  )rZ   	precisionr;   Generateprimarylg)variantsizer3   i  r4   i  2      r6   )minimummaximumr;   steprZ   g        
      g{Gz?r5      Q   r1   )rh   ri   r;   rj   rZ   visible<      rO   zreference imageszfExamples: 'image1.png, image2.png' or 'https://example.com/image1.png, https://example.com/image2.png')rZ   placeholderr7   FzGenerated Imagepng)rZ   ro   formatzGenerated Video)rZ   ro   )fninputsoutputsT)server_portquietprevent_thread_lock
show_errorzP================================================================================zhttp://localhost:r9   z7
[1mSGLang Diffusion WebUI available at:[0m [1;4;92mz[0m
) gradiorC   r   r	   SGLANG_USE_MODELSCOPEgetmodelscope.hub.apir"   r%   tasksr>   huggingface_hubpipeline_tagr   
initializer   BlocksMarkdownRowTextboxColumnNumberButtonSliderCheckboxImageVideoclicklaunch
webui_portprintblock_thread)"r   grr!   repo_idr"   apimodel_info_obj	task_namer%   	task_typevideo_visible_onlyimage_visible_onlyrX   demolaunched_model_boxtask_name_boxr-   r.   r0   run_btnr3   r4   r6   r5   r1   rO   rN   r7   	image_out	video_out_	local_url	delimiterurlr   rW   r   run_sgl_diffusion_webui   s   




Q







(

$r   )argparserD   4sglang.multimodal_gen.configs.sample.sampling_paramsr   r   /sglang.multimodal_gen.runtime.entrypoints.utilsr   r   .sglang.multimodal_gen.runtime.scheduler_clientr   )sglang.multimodal_gen.runtime.server_argsr   1sglang.multimodal_gen.runtime.utils.logging_utilsr   sglang.srt.environr	   __name__r<   ArgumentParserr   r   r   r   r   r   <module>   s    