Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,27 +6,25 @@ import random
|
|
6 |
import numpy as np
|
7 |
import os
|
8 |
|
9 |
-
|
10 |
if torch.cuda.is_available():
|
11 |
device = "cuda"
|
12 |
-
print("
|
13 |
else:
|
14 |
device = "cpu"
|
15 |
-
print("
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
20 |
|
21 |
-
|
22 |
MAX_SEED = np.iinfo(np.int32).max
|
23 |
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
24 |
|
25 |
-
#
|
26 |
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
|
27 |
pipe.to(device)
|
28 |
|
29 |
-
#
|
30 |
@spaces.GPU(duration=160)
|
31 |
def generate_image(prompt, num_inference_steps, height, width, guidance_scale, seed, num_images_per_prompt, progress=gr.Progress(track_tqdm=True)):
|
32 |
if seed == 0:
|
@@ -34,7 +32,6 @@ def generate_image(prompt, num_inference_steps, height, width, guidance_scale, s
|
|
34 |
|
35 |
generator = torch.Generator().manual_seed(seed)
|
36 |
|
37 |
-
|
38 |
with torch.inference_mode():
|
39 |
output = pipe(
|
40 |
prompt=prompt,
|
@@ -48,51 +45,140 @@ def generate_image(prompt, num_inference_steps, height, width, guidance_scale, s
|
|
48 |
|
49 |
return output
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
# Create the Gradio interface
|
54 |
-
|
55 |
examples = [
|
56 |
-
["
|
57 |
-
["
|
58 |
-
["
|
59 |
]
|
60 |
|
|
|
61 |
css = '''
|
62 |
-
.gradio-container{
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
'''
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
68 |
gr.HTML(
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
"""
|
74 |
-
)
|
75 |
gr.HTML(
|
76 |
"""
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
79 |
"""
|
80 |
-
|
|
|
81 |
with gr.Group():
|
82 |
with gr.Column():
|
83 |
-
prompt = gr.Textbox(
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
with gr.Row():
|
88 |
-
num_inference_steps = gr.Slider(
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
with gr.Row():
|
91 |
-
width = gr.Slider(
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
with gr.Row():
|
94 |
-
seed = gr.Slider(
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
gr.Examples(
|
98 |
examples=examples,
|
|
|
6 |
import numpy as np
|
7 |
import os
|
8 |
|
9 |
+
# GPU 사용 가능 여부 확인
|
10 |
if torch.cuda.is_available():
|
11 |
device = "cuda"
|
12 |
+
print("GPU를 사용합니다")
|
13 |
else:
|
14 |
device = "cpu"
|
15 |
+
print("CPU를 사용합니다")
|
|
|
16 |
|
17 |
+
# HuggingFace 토큰 로그인
|
18 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
19 |
|
|
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
22 |
|
23 |
+
# 파이프라인 초기화 및 모델 다운로드
|
24 |
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
|
25 |
pipe.to(device)
|
26 |
|
27 |
+
# 이미지 생성 함수 정의
|
28 |
@spaces.GPU(duration=160)
|
29 |
def generate_image(prompt, num_inference_steps, height, width, guidance_scale, seed, num_images_per_prompt, progress=gr.Progress(track_tqdm=True)):
|
30 |
if seed == 0:
|
|
|
32 |
|
33 |
generator = torch.Generator().manual_seed(seed)
|
34 |
|
|
|
35 |
with torch.inference_mode():
|
36 |
output = pipe(
|
37 |
prompt=prompt,
|
|
|
45 |
|
46 |
return output
|
47 |
|
48 |
+
# 예제 프롬프트
|
|
|
|
|
|
|
49 |
examples = [
|
50 |
+
["안녕하는 팻말을 들고 있는 고양이"],
|
51 |
+
["달에서 알을 깨고 나오는 작은 우주인"],
|
52 |
+
["미래적인 사이보그 슈트를 입고 화성에 있는 우주인"],
|
53 |
]
|
54 |
|
55 |
+
# 커스텀 CSS
|
56 |
css = '''
|
57 |
+
.gradio-container {
|
58 |
+
max-width: 1000px !important;
|
59 |
+
margin: auto;
|
60 |
+
}
|
61 |
+
h1 {
|
62 |
+
text-align: center;
|
63 |
+
font-family: 'Pretendard', sans-serif;
|
64 |
+
color: #EA580C;
|
65 |
+
}
|
66 |
+
.gr-button-primary {
|
67 |
+
background-color: #F97316 !important;
|
68 |
+
}
|
69 |
+
.gr-button-primary:hover {
|
70 |
+
background-color: #EA580C !important;
|
71 |
+
}
|
72 |
'''
|
73 |
+
|
74 |
+
# Gradio 인터페이스 생성
|
75 |
+
with gr.Blocks(
|
76 |
+
theme=gr.themes.Soft(
|
77 |
+
primary_hue=gr.themes.Color(
|
78 |
+
c50="#FFF7ED",
|
79 |
+
c100="#FFEDD5",
|
80 |
+
c200="#FED7AA",
|
81 |
+
c300="#FDBA74",
|
82 |
+
c400="#FB923C",
|
83 |
+
c500="#F97316",
|
84 |
+
c600="#EA580C",
|
85 |
+
c700="#C2410C",
|
86 |
+
c800="#9A3412",
|
87 |
+
c900="#7C2D12",
|
88 |
+
c950="#431407",
|
89 |
+
),
|
90 |
+
secondary_hue="zinc",
|
91 |
+
neutral_hue="zinc",
|
92 |
+
font=("Pretendard", "sans-serif")
|
93 |
+
),
|
94 |
+
css=css
|
95 |
+
) as demo:
|
96 |
with gr.Row():
|
97 |
with gr.Column():
|
98 |
gr.HTML(
|
99 |
+
"""
|
100 |
+
<h1>FLUX.1-dev 이미지 생성기</h1>
|
101 |
+
"""
|
102 |
+
)
|
|
|
|
|
103 |
gr.HTML(
|
104 |
"""
|
105 |
+
<div style='text-align: center'>
|
106 |
+
제작: <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a>
|
107 |
+
<br>
|
108 |
+
<a href="https://discord.gg/AQsmBmgEPy">
|
109 |
+
<img src="https://img.shields.io/discord/1198701940511617164?color=%23738ADB&label=디스코드&style=for-the-badge" alt="Discord">
|
110 |
+
</a>
|
111 |
+
</div>
|
112 |
"""
|
113 |
+
)
|
114 |
+
|
115 |
with gr.Group():
|
116 |
with gr.Column():
|
117 |
+
prompt = gr.Textbox(
|
118 |
+
label="프롬프트",
|
119 |
+
info="원하는 이미지를 설명해주세요",
|
120 |
+
placeholder="고양이..."
|
121 |
+
)
|
122 |
+
run_button = gr.Button("생성하기", variant="primary")
|
123 |
+
result = gr.Gallery(
|
124 |
+
label="생성된 AI 이미지",
|
125 |
+
elem_id="gallery"
|
126 |
+
)
|
127 |
+
|
128 |
+
with gr.Accordion("고급 설정", open=False):
|
129 |
with gr.Row():
|
130 |
+
num_inference_steps = gr.Slider(
|
131 |
+
label="추론 단계 수",
|
132 |
+
info="이미지의 디노이징 단계 수입니다. 더 많은 단계는 더 높은 품질의 이미지를 생성하지만 시간이 더 걸립니다",
|
133 |
+
minimum=1,
|
134 |
+
maximum=50,
|
135 |
+
value=25,
|
136 |
+
step=1
|
137 |
+
)
|
138 |
+
guidance_scale = gr.Slider(
|
139 |
+
label="가이던스 스케일",
|
140 |
+
info="텍스트 프롬프트를 얼마나 충실히 따를지 제어합니다. 높은 값은 입력 텍스트에 더 가깝게 생성됩니다",
|
141 |
+
minimum=0.0,
|
142 |
+
maximum=7.0,
|
143 |
+
value=3.5,
|
144 |
+
step=0.1
|
145 |
+
)
|
146 |
+
|
147 |
with gr.Row():
|
148 |
+
width = gr.Slider(
|
149 |
+
label="너비",
|
150 |
+
info="이미지의 너비",
|
151 |
+
minimum=256,
|
152 |
+
maximum=1024,
|
153 |
+
step=32,
|
154 |
+
value=1024
|
155 |
+
)
|
156 |
+
height = gr.Slider(
|
157 |
+
label="높이",
|
158 |
+
info="이미지의 높이",
|
159 |
+
minimum=256,
|
160 |
+
maximum=1024,
|
161 |
+
step=32,
|
162 |
+
value=1024
|
163 |
+
)
|
164 |
+
|
165 |
with gr.Row():
|
166 |
+
seed = gr.Slider(
|
167 |
+
value=42,
|
168 |
+
minimum=0,
|
169 |
+
maximum=MAX_SEED,
|
170 |
+
step=1,
|
171 |
+
label="시드",
|
172 |
+
info="생성 과정의 시작점입니다. 0을 입력하면 랜덤한 시드가 사용됩니다"
|
173 |
+
)
|
174 |
+
num_images_per_prompt = gr.Slider(
|
175 |
+
label="프롬프트당 이미지 수",
|
176 |
+
info="설정된 값으로 생성할 이미지의 수",
|
177 |
+
minimum=1,
|
178 |
+
maximum=4,
|
179 |
+
step=1,
|
180 |
+
value=2
|
181 |
+
)
|
182 |
|
183 |
gr.Examples(
|
184 |
examples=examples,
|