o
    jh                     @   s^   d dl Z d dlZd dlZd dlZdZejdd Zdd Zdd Ze	d	kr-e
e  dS dS )
    Nz(stabilityai/stable-diffusion-xl-base-1.0c                  c   s^   t j  t j  ttjd} td}td}z_t| 	 d t| 	 d t
|   t
|  d V  W d    n1 sFw   Y  W d    n1 sUw   Y  W t j  t j  t|d t|d t| t| n!t j  t j  t|d t|d t| t| w W d    d S 1 sw   Y  d S )Nw      )sysstdoutflushstderropenosdevnulldupdup2fileno
contextlibredirect_stdoutredirect_stderrclose)r   	stdout_fd	stderr_fd r   6htdocs/OOTD2/inpainting/stable_diffusion_xl_img2img.pysuppress_output	   s8   



 





"r   c                 C   s8   d}	 t j| d|dd}t j|s|S |d7 }q)Nr   Toutput_05dz.pngr   )r
   pathjoinexists)
output_dirindexoutput_pathr   r   r   get_next_output_path   s   r    c               
   C   s  zt tjdk rtdtjd } tj| std|  tjtj	t
}tj|}tjt d}tj|d}tj|d}tj|d}tj|d	}tj|d
}tj|d}	|||||||	fD ]}
tj|
dd zt|
d W qk ty   Y qkw |tjd< |tjd< |tjd< |tjd< |tjd< |tjd< |tjd< |tjd< |tjd< |	tjd< dtjd< dtjd< dtjd< dtjd< tj|d }tj|dd t|}t  d!d l}d!d"lm} d!d#lm} d!d$lm} d!d$lm} |  |  |  t|d%r|  |j  rd&nd'}|!| "d(}d)}|#d(||fd*}|$d+ ||j% d }||j& d }|'|||f |j(t)|d&krW|j*n|j+|d&kr`d,nd d|d-,|}|j-dd. |.  |d&kr|j/j,|j+d/ d0}d1}|j0|d21d3}||||d4d5d6|d7j2d! }|3| W d    n	1 sw   Y  t4tj5|dd8 W d!S  t6y } zt4d9|  W Y d }~dS d }~ww ):Nr   ug   입력 파일을 파라메터로 전달하세요. 예: python3 stable_diffusion_xl_img2img.py test1.pngr   u*   입력 파일을 찾을 수 없습니다: z
ootd-cachehuggingfacehubxetlogstorch
matplotlibT)exist_oki  HOMEXDG_CACHE_HOMEHF_HOMEHUGGINGFACE_HUB_CACHEHF_HUB_CACHETRANSFORMERS_CACHEDIFFUSERS_CACHEHF_XET_CACHE
TORCH_HOMEMPLCONFIGDIR1HF_HUB_DISABLE_PROGRESS_BARSerrorTRANSFORMERS_VERBOSITYDIFFUSERS_VERBOSITYfalseTOKENIZERS_PARALLELISMoutputr   )Image) StableDiffusionXLImg2ImgPipeline)loggingdisable_progress_barcudacpuRGBi   )   rA   rA   )  rB   fp16)torch_dtypevariantuse_safetensors	cache_dir)disable)dtypea  
        perfectly symmetrical flat lay clothing, single layer fabric,
        completely unfolded and spread out, no overlapping parts,
        straight-on orthographic view, dead center,
        strictly parallel vertical lines, perfectly horizontal hem,
        smooth ironed fabric, no wrinkles, professional studio photography,
        white background, high quality catalog photo
        z
        overlapping fabric, folded layers, crumpled, double layers,
        tilted, asymmetrical, distorted, curved lines, warped,
        mannequin, body, person, blurry, low quality
        )device*   g333333?g      &@2   )promptnegative_promptimagestrengthguidance_scalenum_inference_steps	generator)r   zERROR: )7lenr   argv
ValueErrorr
   r   isfileFileNotFoundErrordirnameabspath__file__r   tempfile
gettempdirmakedirschmodPermissionErrorenvironr    r   r%   PILr:   	diffusersr;   diffusers.utilsr<   transformers.utilsset_verbosity_errorr=   hasattrr>   is_availabler	   convertnew	thumbnailwidthheightpastefrom_pretrainedMODEL_IDfloat16float32toset_progress_bar_configenable_attention_slicingvae	Generatormanual_seedimagessaveprintbasename	Exception)
input_path
script_dir
parent_dirrG   huggingface_dirhuggingface_hub_dirhuggingface_xet_dirhuggingface_xet_logs_dir	torch_dirmatplotlib_dir	directoryr   r   r%   r:   r;   diffusers_loggingtransformers_loggingrJ   rO   canvas_sizecanvasxypiperM   rN   rS   resultexcr   r   r   main'   s   
















		Ur   __main__)r
   r   r\   r   rp   contextmanagerr   r    r   __name__exitr   r   r   r   <module>   s    
	 