File size: 5,527 Bytes
2cc6477
9e1bace
2cc6477
7ab20d4
9e1bace
2cc6477
d44ef31
9e1bace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ab20d4
 
9e1bace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import torch
from diffusers import StableDiffusionPipeline
import gradio as gr

# GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ํ™•์ธ
device = "cuda" if torch.cuda.is_available() else "cpu"

# ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋”ฉ
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16 if device == "cuda" else torch.float32
).to(device)

# ์ƒ์„ฑ ํ•จ์ˆ˜
def generate(prompt):
    image = pipe(prompt).images[0]
    return image

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
interface = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(label="ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", placeholder="์˜ˆ: a cute caricature of a cat in a hat"),
    outputs=gr.Image(type="pil"),
    title="Text to Image - Stable Diffusion",
    description="Stable Diffusion์„ ์‚ฌ์šฉํ•œ ํ…์ŠคํŠธ-์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ์ž…๋‹ˆ๋‹ค."
)

if __name__ == "__main__":
    interface.launch()


# import os
# import torch
# import random
# import importlib
# from PIL import Image
# from huggingface_hub import snapshot_download
# import gradio as gr
# from transformers import AutoProcessor, AutoModelForCausalLM, CLIPTextModel, CLIPTokenizer, CLIPFeatureExtractor
# from diffusers import StableDiffusionPipeline, DiffusionPipeline, EulerDiscreteScheduler, UNet2DConditionModel

# # ํ™˜๊ฒฝ ์„ค์ •
# os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
# REVISION = "ceaf371f01ef66192264811b390bccad475a4f02"

# # ๋กœ์ปฌ ๋‹ค์šด๋กœ๋“œ
# LOCAL_FLORENCE = snapshot_download("microsoft/Florence-2-base", revision=REVISION)
# LOCAL_TURBOX = snapshot_download("tensorart/stable-diffusion-3.5-large-TurboX")

# # ๋””๋ฐ”์ด์Šค ๋ฐ dtype ์„ค์ •
# device = "cuda" if torch.cuda.is_available() else "cpu"
# dtype = torch.float16 if torch.cuda.is_available() else torch.float32

# # ๋ชจ๋ธ ๋กœ๋”ฉ (๋ถ€๋ถ„๋ณ„ ๋กœ๋”ฉ + dtype ์ ์šฉ)
# scheduler = EulerDiscreteScheduler.from_pretrained(
#     LOCAL_TURBOX, subfolder="scheduler", torch_dtype=dtype
# )
# text_encoder = CLIPTextModel.from_pretrained(LOCAL_TURBOX, subfolder="text_encoder", torch_dtype=dtype)
# tokenizer = CLIPTokenizer.from_pretrained(LOCAL_TURBOX, subfolder="tokenizer")
# feature_extractor = CLIPFeatureExtractor.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="feature_extractor")
# unet = UNet2DConditionModel.from_pretrained(LOCAL_TURBOX, subfolder="unet", torch_dtype=dtype)

# florence_model = AutoModelForCausalLM.from_pretrained(
#     LOCAL_FLORENCE, trust_remote_code=True, torch_dtype=dtype
# )
# florence_model.to("cpu").eval()
# florence_processor = AutoProcessor.from_pretrained(LOCAL_FLORENCE, trust_remote_code=True)

# # Stable Diffusion ํŒŒ์ดํ”„๋ผ์ธ
# pipe = DiffusionPipeline.from_pretrained(
#     LOCAL_TURBOX,
#     torch_dtype=dtype,
#     trust_remote_code=True,
#     safety_checker=None,
#     feature_extractor=None
# )
# pipe = pipe.to(device)
# pipe.scheduler = scheduler
# pipe.enable_attention_slicing()  # ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ

# # ์ƒ์ˆ˜
# MAX_SEED = 2**31 - 1

# # ํ…์ŠคํŠธ ์Šคํƒ€์ผ๋Ÿฌ
# def pseudo_translate_to_korean_style(en_prompt: str) -> str:
#     return f"Cartoon styled {en_prompt} handsome or pretty people"

# # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
# def generate_prompt(image):
#     if not isinstance(image, Image.Image):
#         image = Image.fromarray(image)

#     inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to("cpu")
#     with torch.no_grad():
#         generated_ids = florence_model.generate(
#             input_ids=inputs["input_ids"],
#             pixel_values=inputs["pixel_values"],
#             max_new_tokens=256,
#             num_beams=3
#         )
#     generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
#     parsed_answer = florence_processor.post_process_generation(
#         generated_text,
#         task="<MORE_DETAILED_CAPTION>",
#         image_size=(image.width, image.height)
#     )
#     prompt_en = parsed_answer["<MORE_DETAILED_CAPTION>"]
#     cartoon_prompt = pseudo_translate_to_korean_style(prompt_en)
#     return cartoon_prompt

# # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ•จ์ˆ˜
# def generate_image(prompt, seed=42, randomize_seed=False):
#     if randomize_seed:
#         seed = random.randint(0, MAX_SEED)
#     generator = torch.Generator().manual_seed(seed)
#     image = pipe(
#         prompt=prompt,
#         guidance_scale=1.5,
#         num_inference_steps=6,  # ์ตœ์ ํ™”๋œ step ์ˆ˜
#         width=512,
#         height=512,
#         generator=generator
#     ).images[0]
#     return image, seed

# # Gradio UI
# with gr.Blocks() as demo:
#     gr.Markdown("# ๐Ÿ–ผ ์ด๋ฏธ์ง€ โ†’ ์„ค๋ช… ์ƒ์„ฑ โ†’ ์นดํˆฐ ์ด๋ฏธ์ง€ ์ž๋™ ์ƒ์„ฑ๊ธฐ")
#     gr.Markdown("**๐Ÿ“Œ ์‚ฌ์šฉ๋ฒ• ์•ˆ๋‚ด (ํ•œ๊ตญ์–ด)**\n"
#                 "- ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด AI๊ฐ€ ์„ค๋ช… โ†’ ์Šคํƒ€์ผ ๋ณ€ํ™˜ โ†’ ์นดํˆฐ ์ด๋ฏธ์ง€ ์ƒ์„ฑ๊นŒ์ง€ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.")

#     with gr.Row():
#         with gr.Column():
#             input_img = gr.Image(label="๐ŸŽจ ์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
#             run_button = gr.Button("โœจ ์ƒ์„ฑ ์‹œ์ž‘")

#         with gr.Column():
#             prompt_out = gr.Textbox(label="๐Ÿ“ ์Šคํƒ€์ผ ์ ์šฉ๋œ ํ”„๋กฌํ”„ํŠธ", lines=3, show_copy_button=True)
#             output_img = gr.Image(label="๐ŸŽ‰ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")

#     def full_process(img):
#         prompt = generate_prompt(img)
#         image, seed = generate_image(prompt, randomize_seed=True)
#         return prompt, image

#     run_button.click(fn=full_process, inputs=[input_img], outputs=[prompt_out, output_img])

# demo.launch()