Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import random
|
4 |
from PIL import Image
|
5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# Stable Diffusion TurboX λ‘λ
|
14 |
model_repo = "tensorart/stable-diffusion-3.5-large-TurboX"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
pipe = DiffusionPipeline.from_pretrained(
|
16 |
model_repo,
|
17 |
-
torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
-
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo, subfolder="scheduler", shift=5)
|
20 |
-
pipe = pipe.to(device)
|
21 |
|
22 |
MAX_SEED = 2**31 - 1
|
23 |
|
24 |
def pseudo_translate_to_korean_style(en_prompt: str) -> str:
|
25 |
-
|
26 |
-
return f"μ΄ μ₯λ©΄μ {en_prompt} μ₯λ©΄μ
λλ€. λ°κ³ κ·μ¬μ΄ μΉ΄ν° μ€νμΌλ‘ κ·Έλ €μ£ΌμΈμ. λμ§νΈ μΌλ¬μ€νΈ λλμΌλ‘ λ¬μ¬ν΄ μ£ΌμΈμ."
|
27 |
|
28 |
def generate_prompt(image):
|
29 |
-
"""μ΄λ―Έμ§ β μμ΄ μ€λͺ
β νκ΅μ΄ ν둬ννΈ μ€νμΌλ‘ λ³ν"""
|
30 |
if not isinstance(image, Image.Image):
|
31 |
image = Image.fromarray(image)
|
32 |
|
@@ -44,19 +136,15 @@ def generate_prompt(image):
|
|
44 |
image_size=(image.width, image.height)
|
45 |
)
|
46 |
prompt_en = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
47 |
-
|
48 |
-
# λ²μκΈ° μμ΄ μ€νμΌ μ μ©
|
49 |
cartoon_prompt = pseudo_translate_to_korean_style(prompt_en)
|
50 |
return cartoon_prompt
|
51 |
|
52 |
def generate_image(prompt, seed=42, randomize_seed=False):
|
53 |
-
"""ν
μ€νΈ ν둬ννΈ β μ΄λ―Έμ§ μμ±"""
|
54 |
if randomize_seed:
|
55 |
seed = random.randint(0, MAX_SEED)
|
56 |
generator = torch.Generator().manual_seed(seed)
|
57 |
image = pipe(
|
58 |
prompt=prompt,
|
59 |
-
negative_prompt="μ곑λ μ, νλ¦Ό, μ΄μν μΌκ΅΄",
|
60 |
guidance_scale=1.5,
|
61 |
num_inference_steps=8,
|
62 |
width=768,
|
@@ -65,10 +153,9 @@ def generate_image(prompt, seed=42, randomize_seed=False):
|
|
65 |
).images[0]
|
66 |
return image, seed
|
67 |
|
68 |
-
# Gradio UI
|
69 |
with gr.Blocks() as demo:
|
70 |
gr.Markdown("# πΌ μ΄λ―Έμ§ β μ€λͺ
μμ± β μΉ΄ν° μ΄λ―Έμ§ μλ μμ±κΈ°")
|
71 |
-
|
72 |
gr.Markdown("**π μ¬μ©λ² μλ΄ (νκ΅μ΄)**\n"
|
73 |
"- μΌμͺ½μ μ΄λ―Έμ§λ₯Ό μ
λ‘λνμΈμ.\n"
|
74 |
"- AIκ° μμ΄ μ€λͺ
μ λ§λ€κ³ , λ΄λΆμμ νκ΅μ΄ μ€νμΌ ν둬ννΈλ‘ μ¬κ΅¬μ±ν©λλ€.\n"
|
@@ -78,7 +165,6 @@ with gr.Blocks() as demo:
|
|
78 |
with gr.Column():
|
79 |
input_img = gr.Image(label="π¨ μλ³Έ μ΄λ―Έμ§ μ
λ‘λ")
|
80 |
run_button = gr.Button("β¨ μμ± μμ")
|
81 |
-
|
82 |
with gr.Column():
|
83 |
prompt_out = gr.Textbox(label="π μ€νμΌ μ μ©λ ν둬ννΈ", lines=3, show_copy_button=True)
|
84 |
output_img = gr.Image(label="π μμ±λ μ΄λ―Έμ§")
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import snapshot_download
|
3 |
+
|
4 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
|
5 |
+
REVISION = "ceaf371f01ef66192264811b390bccad475a4f02"
|
6 |
+
|
7 |
+
LOCAL_FLORENCE = snapshot_download(
|
8 |
+
repo_id="microsoft/Florence-2-base",
|
9 |
+
revision=REVISION
|
10 |
+
)
|
11 |
+
|
12 |
+
LOCAL_TURBOX = snapshot_download(
|
13 |
+
repo_id="tensorart/stable-diffusion-3.5-large-TurboX"
|
14 |
+
)
|
15 |
+
|
16 |
+
LOCAL_FLORENCE_DIR = snapshot_download(
|
17 |
+
repo_id="microsoft/Florence-2-base",
|
18 |
+
revision=REVISION,
|
19 |
+
local_files_only=False
|
20 |
+
)
|
21 |
+
|
22 |
+
import sys, types, importlib.machinery, importlib
|
23 |
+
|
24 |
+
# flash_attn 무ν¨ν μ²λ¦¬
|
25 |
+
spec = importlib.machinery.ModuleSpec('flash_attn', loader=None)
|
26 |
+
mod = types.ModuleType('flash_attn')
|
27 |
+
mod.__spec__ = spec
|
28 |
+
sys.modules['flash_attn'] = mod
|
29 |
+
|
30 |
+
import huggingface_hub as _hf_hub
|
31 |
+
_hf_hub.cached_download = _hf_hub.hf_hub_download
|
32 |
+
|
33 |
import gradio as gr
|
34 |
import torch
|
35 |
import random
|
36 |
from PIL import Image
|
37 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
38 |
+
from transformers import (
|
39 |
+
CLIPTextModel,
|
40 |
+
CLIPTokenizer,
|
41 |
+
CLIPFeatureExtractor,
|
42 |
+
)
|
43 |
+
import diffusers
|
44 |
+
from diffusers import StableDiffusionPipeline
|
45 |
+
from diffusers import DiffusionPipeline
|
46 |
+
from diffusers import EulerDiscreteScheduler as FlowMatchEulerDiscreteScheduler
|
47 |
+
from diffusers import UNet2DConditionModel
|
48 |
|
49 |
+
# flash attention κ΄λ ¨ import μ°ν
|
50 |
+
import transformers.utils.import_utils as _import_utils
|
51 |
+
from transformers.utils import is_flash_attn_2_available
|
52 |
+
_import_utils._is_package_available = lambda pkg: False
|
53 |
+
_import_utils.is_flash_attn_2_available = lambda: False
|
54 |
+
|
55 |
+
hf_utils = importlib.import_module('transformers.utils')
|
56 |
+
hf_utils.is_flash_attn_2_available = lambda *a, **k: False
|
57 |
+
hf_utils.is_flash_attn_greater_or_equal_2_10 = lambda *a, **k: False
|
58 |
+
|
59 |
+
mask_utils = importlib.import_module("transformers.modeling_attn_mask_utils")
|
60 |
+
for fn in ("_prepare_4d_attention_mask_for_sdpa", "_prepare_4d_causal_attention_mask_for_sdpa"):
|
61 |
+
if not hasattr(mask_utils, fn):
|
62 |
+
setattr(mask_utils, fn, lambda *a, **k: None)
|
63 |
+
|
64 |
+
cfg_mod = importlib.import_module("transformers.configuration_utils")
|
65 |
+
_PrC = cfg_mod.PretrainedConfig
|
66 |
+
_orig_getattr = _PrC.__getattribute__
|
67 |
+
def _getattr(self, name):
|
68 |
+
if name == "_attn_implementation":
|
69 |
+
return "sdpa"
|
70 |
+
return _orig_getattr(self, name)
|
71 |
+
_PrC.__getattribute__ = _getattr
|
72 |
|
|
|
73 |
model_repo = "tensorart/stable-diffusion-3.5-large-TurboX"
|
74 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
75 |
+
|
76 |
+
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
77 |
+
model_repo,
|
78 |
+
subfolder="scheduler",
|
79 |
+
torch_dtype=torch.float16,
|
80 |
+
)
|
81 |
+
|
82 |
+
text_encoder = CLIPTextModel.from_pretrained(
|
83 |
+
model_repo, subfolder="text_encoder", torch_dtype=torch.float16
|
84 |
+
)
|
85 |
+
tokenizer = CLIPTokenizer.from_pretrained(model_repo, subfolder="tokenizer")
|
86 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="feature_extractor")
|
87 |
+
|
88 |
+
unet = UNet2DConditionModel.from_pretrained(
|
89 |
+
model_repo, subfolder="unet", torch_dtype=torch.float16
|
90 |
+
)
|
91 |
+
|
92 |
+
florence_model = AutoModelForCausalLM.from_pretrained(
|
93 |
+
LOCAL_FLORENCE, trust_remote_code=True, torch_dtype=torch.float16
|
94 |
+
)
|
95 |
+
florence_model.to("cpu")
|
96 |
+
florence_model.eval()
|
97 |
+
florence_processor = AutoProcessor.from_pretrained(
|
98 |
+
LOCAL_FLORENCE, trust_remote_code=True
|
99 |
+
)
|
100 |
+
|
101 |
+
diffusers.StableDiffusion3Pipeline = StableDiffusionPipeline
|
102 |
pipe = DiffusionPipeline.from_pretrained(
|
103 |
model_repo,
|
104 |
+
torch_dtype=torch.float16,
|
105 |
+
trust_remote_code=True,
|
106 |
+
safety_checker=None,
|
107 |
+
feature_extractor=None
|
108 |
+
)
|
109 |
+
pipe = pipe.to("cuda")
|
110 |
+
|
111 |
+
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
112 |
+
model_repo, subfolder="scheduler", local_files_only=True,
|
113 |
+
trust_remote_code=True, shift=5
|
114 |
)
|
|
|
|
|
115 |
|
116 |
MAX_SEED = 2**31 - 1
|
117 |
|
118 |
def pseudo_translate_to_korean_style(en_prompt: str) -> str:
|
119 |
+
return f"Cartoon styled {en_prompt} handsome or pretty people"
|
|
|
120 |
|
121 |
def generate_prompt(image):
|
|
|
122 |
if not isinstance(image, Image.Image):
|
123 |
image = Image.fromarray(image)
|
124 |
|
|
|
136 |
image_size=(image.width, image.height)
|
137 |
)
|
138 |
prompt_en = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
|
|
|
|
139 |
cartoon_prompt = pseudo_translate_to_korean_style(prompt_en)
|
140 |
return cartoon_prompt
|
141 |
|
142 |
def generate_image(prompt, seed=42, randomize_seed=False):
|
|
|
143 |
if randomize_seed:
|
144 |
seed = random.randint(0, MAX_SEED)
|
145 |
generator = torch.Generator().manual_seed(seed)
|
146 |
image = pipe(
|
147 |
prompt=prompt,
|
|
|
148 |
guidance_scale=1.5,
|
149 |
num_inference_steps=8,
|
150 |
width=768,
|
|
|
153 |
).images[0]
|
154 |
return image, seed
|
155 |
|
156 |
+
# Gradio UI
|
157 |
with gr.Blocks() as demo:
|
158 |
gr.Markdown("# πΌ μ΄λ―Έμ§ β μ€λͺ
μμ± β μΉ΄ν° μ΄λ―Έμ§ μλ μμ±κΈ°")
|
|
|
159 |
gr.Markdown("**π μ¬μ©λ² μλ΄ (νκ΅μ΄)**\n"
|
160 |
"- μΌμͺ½μ μ΄λ―Έμ§λ₯Ό μ
λ‘λνμΈμ.\n"
|
161 |
"- AIκ° μμ΄ μ€λͺ
μ λ§λ€κ³ , λ΄λΆμμ νκ΅μ΄ μ€νμΌ ν둬ννΈλ‘ μ¬κ΅¬μ±ν©λλ€.\n"
|
|
|
165 |
with gr.Column():
|
166 |
input_img = gr.Image(label="π¨ μλ³Έ μ΄λ―Έμ§ μ
λ‘λ")
|
167 |
run_button = gr.Button("β¨ μμ± μμ")
|
|
|
168 |
with gr.Column():
|
169 |
prompt_out = gr.Textbox(label="π μ€νμΌ μ μ©λ ν둬ννΈ", lines=3, show_copy_button=True)
|
170 |
output_img = gr.Image(label="π μμ±λ μ΄λ―Έμ§")
|