ginipick commited on
Commit
a861751
Β·
verified Β·
1 Parent(s): 4c7bfc3

Update app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +69 -101
app-backup.py CHANGED
@@ -56,7 +56,7 @@ pipe = FluxPipeline.from_pretrained(
56
  use_auth_token=HF_TOKEN
57
  )
58
 
59
- # Hyper-SD LoRA λ‘œλ“œ (인증 포함)
60
  pipe.load_lora_weights(
61
  hf_hub_download(
62
  "ByteDance/Hyper-SD",
@@ -89,10 +89,6 @@ def save_image(image):
89
  image = Image.fromarray(image)
90
  image.save(filepath, "PNG", quality=100)
91
 
92
- if not os.path.exists(filepath):
93
- print(f"Warning: Failed to verify saved image at {filepath}")
94
- return None
95
-
96
  return filepath
97
  except Exception as e:
98
  print(f"Failed to save image: {str(e)}")
@@ -102,7 +98,6 @@ def save_image(image):
102
  print(f"Error in save_image: {str(e)}")
103
  return None
104
 
105
-
106
  # μ˜ˆμ‹œ ν”„λ‘¬ν”„νŠΈ μ •μ˜
107
  examples = [
108
  ["A 3D Star Wars Darth Vader helmet, highly detailed metallic finish"],
@@ -117,7 +112,61 @@ examples = [
117
  ["A 3D floating magical crystal orb with internal energy swirls"]
118
  ]
119
 
120
- # Gradio μΈν„°νŽ˜μ΄μŠ€ μˆ˜μ •
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  with gr.Blocks(
122
  theme=gr.themes.Soft(),
123
  css="""
@@ -160,57 +209,6 @@ with gr.Blocks(
160
  lines=3
161
  )
162
 
163
- # ... (κΈ°μ‘΄ Advanced Settings μ½”λ“œ)
164
-
165
- with gr.Column(scale=4, elem_classes=["fixed-width"]):
166
- # κΈ°λ³Έ 이미지 μ„€μ •
167
- output = gr.Image(
168
- label="Generated Image",
169
- elem_id="output-image",
170
- elem_classes=["output-image", "fixed-width"],
171
- value="3d.webp" # κΈ°λ³Έ 이미지 μ„€μ •
172
- )
173
-
174
- # Examples μ„Ήμ…˜ μΆ”κ°€
175
- gr.Examples(
176
- examples=examples,
177
- inputs=prompt,
178
- outputs=output,
179
- fn=process_and_save_image,
180
- cache_examples=True, # μ˜ˆμ‹œ κ²°κ³Ό 캐싱 ν™œμ„±ν™”
181
- examples_per_page=5
182
- )
183
-
184
-
185
-
186
- if __name__ == "__main__":
187
- demo.launch(
188
- allowed_paths=[PERSISTENT_DIR],
189
- share=True
190
- )
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
- # Create Gradio interface
205
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
206
- with gr.Row():
207
- with gr.Column(scale=3):
208
- prompt = gr.Textbox(
209
- label="Image Description",
210
- placeholder="Describe the image you want to create...",
211
- lines=3
212
- )
213
-
214
  with gr.Accordion("Advanced Settings", open=False):
215
  with gr.Row():
216
  height = gr.Slider(
@@ -244,9 +242,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
244
  value=3.5
245
  )
246
 
247
- def get_random_seed():
248
- return torch.randint(0, 1000000, (1,)).item()
249
-
250
  seed = gr.Number(
251
  label="Seed (random by default, set for reproducibility)",
252
  value=get_random_seed(),
@@ -264,51 +259,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
264
  output = gr.Image(
265
  label="Generated Image",
266
  elem_id="output-image",
267
- elem_classes=["output-image", "fixed-width"]
 
268
  )
269
 
270
- @spaces.GPU
271
- def process_and_save_image(height, width, steps, scales, prompt, seed):
272
- global pipe
273
-
274
- # ν•œκΈ€ 감지 및 λ²ˆμ—­
275
- def contains_korean(text):
276
- return any(ord('κ°€') <= ord(c) <= ord('힣') for c in text)
277
-
278
- # ν”„λ‘¬ν”„νŠΈ μ „μ²˜λ¦¬
279
- if contains_korean(prompt):
280
- # ν•œκΈ€μ„ μ˜μ–΄λ‘œ λ²ˆμ—­
281
- translated = translator(prompt)[0]['translation_text']
282
- prompt = translated
283
-
284
- # ν”„λ‘¬ν”„νŠΈ ν˜•μ‹ κ°•μ œ
285
- formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background"
286
-
287
- with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
288
- try:
289
- generated_image = pipe(
290
- prompt=[formatted_prompt],
291
- generator=torch.Generator().manual_seed(int(seed)),
292
- num_inference_steps=int(steps),
293
- guidance_scale=float(scales),
294
- height=int(height),
295
- width=int(width),
296
- max_sequence_length=256
297
- ).images[0]
298
-
299
- saved_path = save_image(generated_image)
300
- if saved_path is None:
301
- print("Warning: Failed to save generated image")
302
-
303
- return generated_image
304
- except Exception as e:
305
- print(f"Error in image generation: {str(e)}")
306
- return None
307
-
308
  def update_seed():
309
  return get_random_seed()
310
 
311
- # Click event handlers inside gr.Blocks context
312
  generate_btn.click(
313
  process_and_save_image,
314
  inputs=[height, width, steps, scales, prompt, seed],
 
56
  use_auth_token=HF_TOKEN
57
  )
58
 
59
+ # Hyper-SD LoRA λ‘œλ“œ
60
  pipe.load_lora_weights(
61
  hf_hub_download(
62
  "ByteDance/Hyper-SD",
 
89
  image = Image.fromarray(image)
90
  image.save(filepath, "PNG", quality=100)
91
 
 
 
 
 
92
  return filepath
93
  except Exception as e:
94
  print(f"Failed to save image: {str(e)}")
 
98
  print(f"Error in save_image: {str(e)}")
99
  return None
100
 
 
101
  # μ˜ˆμ‹œ ν”„λ‘¬ν”„νŠΈ μ •μ˜
102
  examples = [
103
  ["A 3D Star Wars Darth Vader helmet, highly detailed metallic finish"],
 
112
  ["A 3D floating magical crystal orb with internal energy swirls"]
113
  ]
114
 
115
+ @spaces.GPU
116
+ def process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt="", seed=None):
117
+ global pipe
118
+
119
+ if seed is None:
120
+ seed = torch.randint(0, 1000000, (1,)).item()
121
+
122
+ # ν•œκΈ€ 감지 및 λ²ˆμ—­
123
+ def contains_korean(text):
124
+ return any(ord('κ°€') <= ord(c) <= ord('힣') for c in text)
125
+
126
+ # ν”„λ‘¬ν”„νŠΈ μ „μ²˜λ¦¬
127
+ if contains_korean(prompt):
128
+ translated = translator(prompt)[0]['translation_text']
129
+ prompt = translated
130
+
131
+ formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background"
132
+
133
+ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
134
+ try:
135
+ generated_image = pipe(
136
+ prompt=[formatted_prompt],
137
+ generator=torch.Generator().manual_seed(int(seed)),
138
+ num_inference_steps=int(steps),
139
+ guidance_scale=float(scales),
140
+ height=int(height),
141
+ width=int(width),
142
+ max_sequence_length=256
143
+ ).images[0]
144
+
145
+ saved_path = save_image(generated_image)
146
+ if saved_path is None:
147
+ print("Warning: Failed to save generated image")
148
+
149
+ return generated_image
150
+ except Exception as e:
151
+ print(f"Error in image generation: {str(e)}")
152
+ return None
153
+
154
+ def get_random_seed():
155
+ return torch.randint(0, 1000000, (1,)).item()
156
+
157
+
158
+ def process_example(prompt):
159
+ return process_and_save_image(
160
+ height=1024,
161
+ width=1024,
162
+ steps=8,
163
+ scales=3.5,
164
+ prompt=prompt,
165
+ seed=get_random_seed()
166
+ )
167
+
168
+
169
+ # Gradio μΈν„°νŽ˜μ΄μŠ€
170
  with gr.Blocks(
171
  theme=gr.themes.Soft(),
172
  css="""
 
209
  lines=3
210
  )
211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  with gr.Accordion("Advanced Settings", open=False):
213
  with gr.Row():
214
  height = gr.Slider(
 
242
  value=3.5
243
  )
244
 
 
 
 
245
  seed = gr.Number(
246
  label="Seed (random by default, set for reproducibility)",
247
  value=get_random_seed(),
 
259
  output = gr.Image(
260
  label="Generated Image",
261
  elem_id="output-image",
262
+ elem_classes=["output-image", "fixed-width"],
263
+ value="3d.webp"
264
  )
265
 
266
+ # Examples μ„Ήμ…˜
267
+ gr.Examples(
268
+ examples=examples,
269
+ inputs=prompt,
270
+ outputs=output,
271
+ fn=process_example, # μˆ˜μ •λœ ν•¨μˆ˜ μ‚¬μš©
272
+ cache_examples=False,
273
+ examples_per_page=5
274
+ )
275
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  def update_seed():
277
  return get_random_seed()
278
 
279
+ # 이벀트 ν•Έλ“€λŸ¬
280
  generate_btn.click(
281
  process_and_save_image,
282
  inputs=[height, width, steps, scales, prompt, seed],