o
    j
                     @   sl  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d	Zd
Ze je jeZe jedZe jedZdZe	 Zejedgdgdgd e jedd ejdeeddd ed ejeej ed!eZ"e"j#dd dej$j%j&_'dej$j(_'dej$j(_)ed e*dededfde+de+fdd Z,dS )!    N)Image)DiffusionPipeline)Body)FastAPI)HTTPException)CORSMiddleware)StaticFilesz!black-forest-labs/FLUX.2-klein-9Bcudacacheoutputi   *POSTzContent-Type)allow_originsallow_methodsallow_headersT)exist_okz/output)	directory)namezLoading model...)torch_dtype	cache_dir)disablezModel loaded!z	/generate.imagepromptc              
   C   s
  | }t d|  dd tj|stjt|}tj|s'tdd|  d| s1tdddt	
|d	}|ttf}t  t||ttd
tjtdddjd }W d    n1 scw   Y  t  d}tjt|}|| dtj|tdS )Nzimage: T)flushi  u*   입력 파일을 찾을 수 없습니다: )status_codedetaili  u'   prompt가 입력되지 않았습니다.RGB   )devicei  )r   r   heightwidthnum_inference_steps	generatorr   z.pngsuccess)statusr   )printospathisabsjoin
SCRIPT_DIRisfiler   stripr   openconvertresize
IMAGE_SIZEtorchinference_modepipe	GeneratorDEVICEmanual_seedimagesuuiduuid4
OUTPUT_DIRsaverelpath)r   r   
image_pathinput_imageresultfilename	save_path rB   *htdocs/OOTD2/inpainting/flux2/flux2_api.pygenerate?   s8   


rD   )-r&   r8   r1   PILr   	diffusersr   fastapir   r   r   fastapi.middleware.corsr   fastapi.staticfilesr   MODEL_IDr5   r'   dirnameabspath__file__r*   r)   	CACHE_DIRr:   r0   appadd_middlewaremakedirsmountr%   from_pretrainedbfloat16tor3   set_progress_bar_configbackendsr	   matmul
allow_tf32cudnn	benchmarkpoststrrD   rB   rB   rB   rC   <module>   sP    

&