AIRider commited on
Commit
8ab6152
·
verified ·
1 Parent(s): b2a0565

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -38
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("Using GPU")
13
  else:
14
  device = "cpu"
15
- print("Using CPU")
16
-
17
 
18
- # login hf token
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
- # Initialize the pipeline and download the model
26
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
27
  pipe.to(device)
28
 
29
- # Define the image generation function
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
- ["A cat holding a sign that says hello world"],
57
- ["a tiny astronaut hatching from an egg on the moon"],
58
- ["An astrounat on mars in a futuristic cyborg suit."],
59
  ]
60
 
 
61
  css = '''
62
- .gradio-container{max-width: 1000px !important}
63
- h1{text-align:center}
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  '''
65
- with gr.Blocks(css=css) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  with gr.Row():
67
  with gr.Column():
68
  gr.HTML(
69
- """
70
- <h1 style='text-align: center'>
71
- FLUX.1-dev
72
- </h1>
73
- """
74
- )
75
  gr.HTML(
76
  """
77
- Made by <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a>
78
- <br> <a href="https://discord.gg/AQsmBmgEPy"> <img src="https://img.shields.io/discord/1198701940511617164?color=%23738ADB&label=Discord&style=for-the-badge" alt="Discord"> </a>
 
 
 
 
 
79
  """
80
- )
 
81
  with gr.Group():
82
  with gr.Column():
83
- prompt = gr.Textbox(label="Prompt", info="Describe the image you want", placeholder="A cat...")
84
- run_button = gr.Button("Run")
85
- result = gr.Gallery(label="Generated AI Images", elem_id="gallery")
86
- with gr.Accordion("Advanced options", open=False):
 
 
 
 
 
 
 
 
87
  with gr.Row():
88
- num_inference_steps = gr.Slider(label="Number of Inference Steps", info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference", minimum=1, maximum=50, value=25, step=1)
89
- guidance_scale = gr.Slider(label="Guidance Scale", info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.", minimum=0.0, maximum=7.0, value=3.5, step=0.1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  with gr.Row():
91
- width = gr.Slider(label="Width", info="Width of the Image", minimum=256, maximum=1024, step=32, value=1024)
92
- height = gr.Slider(label="Height", info="Height of the Image", minimum=256, maximum=1024, step=32, value=1024)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  with gr.Row():
94
- seed = gr.Slider(value=42, minimum=0, maximum=MAX_SEED, step=1, label="Seed", info="A starting point to initiate the generation process, put 0 for a random one")
95
- num_images_per_prompt = gr.Slider(label="Images Per Prompt", info="Number of Images to generate with the settings",minimum=1, maximum=4, step=1, value=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,