| import os |
| import gc |
| import torch |
| import gradio as gr |
| from PIL import Image |
| from diffusers import DiffusionPipeline |
|
|
| |
| |
| os.environ["SAFETENSORS_FAST_GPU"] = "0" |
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" |
| print(f"๐ฅ๏ธ [System] ํ์ฌ ๊ฐ์ง๋ ์คํ ๋๋ฐ์ด์ค: {DEVICE.upper()}") |
|
|
| def clean_memory(): |
| """๊ฐ๋น์ง ์ปฌ๋ ์
์ ์์ฃผ ๊ฐ๋ ฅํ๊ฒ ์๋ ํธ๋ฆฌ๊ฑฐ""" |
| gc.collect() |
| if torch.cuda.is_available(): |
| torch.cuda.empty_cache() |
|
|
| |
| print("๐ [System] ์คํ๋์ค ํ์ดํ๋ผ์ธ ์ต์ ํ ์ด๊ธฐํ ์์...") |
|
|
| |
| print("๐ฆ [1/2] FireRed Image Edit ๋ชจ๋ธ ๋ก๋ ์ค...") |
| pipe_edit = DiffusionPipeline.from_pretrained( |
| "FireRedTeam/FireRed-Image-Edit-1.1", |
| torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32, |
| low_cpu_mem_usage=True, |
| ) |
|
|
| if DEVICE == "cuda": |
| pipe_edit.to("cuda") |
| else: |
| |
| pipe_edit.enable_model_cpu_offload() |
|
|
| print("โ
[System] FireRed Image Edit ๋ก๋ ์๋ฃ.") |
| clean_memory() |
|
|
| |
| print("๐ฆ [2/2] Wan 2.2 14B ๋ชจ๋ธ ๋ก๋ ์ค...") |
| |
| wan_kwargs = { |
| "torch_dtype": torch.bfloat16 if DEVICE == "cuda" else torch.float32, |
| "low_cpu_mem_usage": True, |
| "device_map": "auto" |
| } |
| if DEVICE == "cuda": |
| wan_kwargs["variant"] = "fp8" |
|
|
| pipe_wan = DiffusionPipeline.from_pretrained( |
| "Wan-Video/Wan2.2-I2V-14B", |
| **wan_kwargs |
| ) |
|
|
| if DEVICE == "cuda": |
| pipe_wan.enable_model_cpu_offload() |
| else: |
| |
| |
| pipe_wan.to("cpu") |
|
|
| print("โ
[System] Wan 2.2 14B ๋ก๋ ์๋ฃ.") |
| clean_memory() |
|
|
|
|
| |
| def process_studio_pipeline(input_image, edit_prompt, video_prompt, num_frames=81, progress=gr.Progress()): |
| if input_image is None: |
| raise gr.Error("๋จผ์ ์๋ณธ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์!") |
| |
| |
| progress(0.1, desc="FireRed๋ก ์ด๋ฏธ์ง ํธ์ง ์ค...") |
| pil_img = Image.fromarray(input_image).convert("RGB") |
| |
| edited_image = pipe_edit( |
| prompt=edit_prompt, |
| image=pil_img, |
| num_inference_steps=20 if DEVICE == "cuda" else 3 |
| ).images[0] |
| |
| clean_memory() |
| |
| |
| progress(0.5, desc="Wan 2.2๋ก ๋น๋์ค ์์ฑ ์ค...") |
| |
| video_frames = pipe_wan( |
| prompt=video_prompt, |
| image=edited_image, |
| num_frames=int(num_frames) if DEVICE == "cuda" else 16, |
| num_inference_steps=30 if DEVICE == "cuda" else 3, |
| guidance_scale=6.0 |
| ).frames[0] |
| |
| clean_memory() |
| |
| |
| progress(0.9, desc="๋น๋์ค ๋ ๋๋ง ์ค...") |
| output_video_path = "studio_output.mp4" |
| |
| from diffusers.utils import export_to_video |
| export_to_video(video_frames, output_video_path, fps=16) |
| |
| return edited_image, output_video_path |
|
|
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# ๐ฌ iE2V Fast Studio (RAM Dynamic Build)") |
| if DEVICE == "cpu": |
| gr.Warning("โ ๏ธ ์ธํ๋ผ๊ฐ CPU ๋ชจ๋๋ก ์ ํ๋์ด RAM 96GB ํฌ๋์ ๋ฐฉ์ด ๋ชจ๋๊ฐ ์๋ ์ค์
๋๋ค. ์ ์ ์ถ๋ก ์ ์ํด์๋ Space Settings์์ GPU ํ ๋น์ ๊ผญ ํ๋ณดํด ์ฃผ์ธ์.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| input_img_slot = gr.Image(label="1. ์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋", type="numpy") |
| edit_prompt_slot = gr.Textbox(label="2. ์ด๋ฏธ์ง ํธ์ง ํ๋กฌํํธ (FireRed)", placeholder="๋ฐฐ๊ฒฝ์ ๋ฐค๊ฑฐ๋ฆฌ๋ก ๋ณ๊ฒฝ...") |
| video_prompt_slot = gr.Textbox(label="3. ๋น๋์ค ์ฐ์ถ ํ๋กฌํํธ (Wan 2.2)", placeholder="์นด๋ฉ๋ผ๊ฐ ์์ํ ์ค์ธ๋๋ฉฐ...") |
| |
| with gr.Accordion("๐ฅ ๊ณ ๊ธ ์ธํ
", open=False): |
| frames_slider = gr.Slider(minimum=16, maximum=81, step=4, value=81 if DEVICE == "cuda" else 16, label="ํ๋ ์ ์") |
| |
| submit_btn = gr.Button("โก ์ตํฉ ์์ฑ ์์", variant="primary") |
| |
| with gr.Column(): |
| edited_img_output = gr.Image(label="ํธ์ง๋ ์ด๋ฏธ์ง ๊ฒฐ๊ณผ") |
| video_output = gr.Video(label="์ต์ข
์์ฑ ๋น๋์ค") |
| |
| submit_btn.click( |
| fn=process_studio_pipeline, |
| inputs=[input_img_slot, edit_prompt_slot, video_prompt_slot, frames_slider], |
| outputs=[edited_img_output, video_output] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.queue().launch(server_name="0.0.0.0", server_port=7860) |