rahul7star commited on
Commit
766561b
·
verified ·
1 Parent(s): be8c7a4

Update app_allfile.py

Browse files
Files changed (1) hide show
  1. app_allfile.py +44 -37
app_allfile.py CHANGED
@@ -54,7 +54,6 @@ def upload_image_and_prompt_cpu(input_image, prompt_text) -> str:
54
  from huggingface_hub import HfApi
55
 
56
  api = HfApi()
57
- print(prompt_text)
58
  today_str = datetime.now().strftime("%Y-%m-%d")
59
  unique_subfolder = f"Upload-Image-{uuid.uuid4().hex[:8]}"
60
  hf_folder = f"{today_str}/{unique_subfolder}"
@@ -121,6 +120,7 @@ pipe.load_lora_weights(
121
  weight_name="qwen_lora/Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16_dim1.safetensors"
122
  )
123
  pipe.fuse_lora(lora_scale=1.0)
 
124
  pipe.load_lora_weights(
125
  "rahul7star/qwen-char-lora",
126
  weight_name="qwen_lora/qwen-multiple-char.safetensors",
@@ -131,7 +131,7 @@ pipe.transformer.__class__ = QwenImageTransformer2DModel
131
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
132
  optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
133
 
134
- # --- Helpers ---
135
  def _append_prompt(base: str, extra: str) -> str:
136
  extra = (extra or "").strip()
137
  return (base if not extra else f"{base} {extra}").strip()
@@ -148,8 +148,7 @@ def generate_single_view(input_images, prompt, seed, num_inference_steps, true_g
148
  num_images_per_prompt=1,
149
  ).images
150
  try:
151
- #upload_image_and_prompt_cpu(result[0], prompt)
152
- print("dont upload")
153
  except Exception as e:
154
  print("Upload failed:", e)
155
  return result[0]
@@ -177,10 +176,11 @@ def concat_images_horizontally(images, bg_color=(255, 255, 255)):
177
  x += img.width
178
  return canvas
179
 
180
- # --- Generate all camera angles dynamically ---
181
  @spaces.GPU()
182
  def generate_turnaround(
183
  image,
 
184
  extra_prompt="",
185
  preset_key="nealy 9:16",
186
  seed=42,
@@ -192,61 +192,68 @@ def generate_turnaround(
192
  if randomize_seed:
193
  seed = random.randint(0, MAX_SEED)
194
  if image is None:
195
- return [None]*(len(BASE_PROMPTS)+1), seed, "❌ 入力画像をアップロードしてください"
196
 
197
  input_image = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image).convert("RGB")
198
- pil_images = [input_image]
199
 
200
  results = {}
201
- total = len(BASE_PROMPTS)
202
- for i, (key, base_prompt) in enumerate(BASE_PROMPTS.items(), start=1):
203
- progress(i/total, desc=f"{key} 生成中...")
204
- prompt_full = _append_prompt(base_prompt, extra_prompt)
205
- img = generate_single_view(pil_images, prompt_full, seed+i, num_inference_steps, true_guidance_scale)
206
- results[key] = resize_to_preset(img, preset_key)
 
 
 
 
 
 
207
 
208
- concat = concat_images_horizontally(list(results.values()))
209
- return [*results.values(), concat, seed, f"✅ {len(results)}視点の画像+連結画像を生成しました"]
210
 
211
  # --- UI ---
212
  css = """
213
  #col-container {margin: 0 auto; max-width: 1400px;}
214
  .image-container img {object-fit: contain !important; max-width: 100%; max-height: 100%;}
215
- .notice {background: #fff5f5; border: 1px solid #fca5a5; color: #7f1d1d; padding: 12px 14px; border-radius: 10px; font-weight: 600; line-height: 1.5; margin-bottom: 10px;}
216
  """
217
 
218
  with gr.Blocks(css=css) as demo:
219
  with gr.Column(elem_id="col-container"):
220
  input_image = gr.Image(label="入力画像", type="pil", height=500)
221
- extra_prompt = gr.Textbox(
222
- label="追加プロンプト(各視点プロンプト末尾に追加)",
223
- placeholder="high detail, anime style, soft lighting, 4k",
224
- lines=2
 
 
 
 
 
 
 
225
  )
226
- preset_dropdown = gr.Dropdown(
227
- label="出力解像度プリセット",
228
- choices=list(RESOLUTIONS.keys()),
229
- value="nealy 9:16"
 
 
230
  )
 
231
  run_button = gr.Button("🎨 生成開始", variant="primary")
232
  status_text = gr.Textbox(label="ステータス", interactive=False)
233
 
234
- # Dynamic outputs for all angles
235
- result_images = []
236
- for key in BASE_PROMPTS.keys():
237
- result_images.append(gr.Image(label=key.capitalize(), type="pil", format="png", height=400, show_download_button=True))
238
- result_concat = gr.Image(label="連結画像(全視点)", type="pil", format="png", height=400, show_download_button=True)
239
-
240
- with gr.Accordion("⚙️ 詳細設定", open=False):
241
- seed_slider = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
242
- randomize_seed_checkbox = gr.Checkbox(label="ランダムシード", value=True)
243
- guidance_scale_slider = gr.Slider(label="True guidance scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
244
- num_steps_slider = gr.Slider(label="生成ステップ数", minimum=1, maximum=40, step=1, value=4)
245
 
 
246
  run_button.click(
247
  fn=generate_turnaround,
248
- inputs=[input_image, extra_prompt, preset_dropdown, seed_slider, randomize_seed_checkbox, guidance_scale_slider, num_steps_slider],
249
- outputs=[*result_images, result_concat, seed_slider, status_text]
250
  )
251
 
252
  if __name__ == "__main__":
 
54
  from huggingface_hub import HfApi
55
 
56
  api = HfApi()
 
57
  today_str = datetime.now().strftime("%Y-%m-%d")
58
  unique_subfolder = f"Upload-Image-{uuid.uuid4().hex[:8]}"
59
  hf_folder = f"{today_str}/{unique_subfolder}"
 
120
  weight_name="qwen_lora/Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16_dim1.safetensors"
121
  )
122
  pipe.fuse_lora(lora_scale=1.0)
123
+
124
  pipe.load_lora_weights(
125
  "rahul7star/qwen-char-lora",
126
  weight_name="qwen_lora/qwen-multiple-char.safetensors",
 
131
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
132
  optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
133
 
134
+ # --- Utilities ---
135
  def _append_prompt(base: str, extra: str) -> str:
136
  extra = (extra or "").strip()
137
  return (base if not extra else f"{base} {extra}").strip()
 
148
  num_images_per_prompt=1,
149
  ).images
150
  try:
151
+ upload_image_and_prompt_cpu(result[0], prompt)
 
152
  except Exception as e:
153
  print("Upload failed:", e)
154
  return result[0]
 
176
  x += img.width
177
  return canvas
178
 
179
+ # --- Main generation function ---
180
  @spaces.GPU()
181
  def generate_turnaround(
182
  image,
183
+ selected_angles,
184
  extra_prompt="",
185
  preset_key="nealy 9:16",
186
  seed=42,
 
192
  if randomize_seed:
193
  seed = random.randint(0, MAX_SEED)
194
  if image is None:
195
+ return {}, seed, "❌ 入力画像をアップロードしてください"
196
 
197
  input_image = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image).convert("RGB")
 
198
 
199
  results = {}
200
+ current_seed = seed
201
+ for i, angle in enumerate(selected_angles):
202
+ progress((i+1)/len(selected_angles), desc=f"{angle} 生成中...")
203
+ prompt = _append_prompt(BASE_PROMPTS[angle], extra_prompt)
204
+ img = generate_single_view([input_image], prompt, current_seed, num_inference_steps, true_guidance_scale)
205
+ img = resize_to_preset(img, preset_key)
206
+ results[angle] = img
207
+ current_seed += 1
208
+
209
+ # Concatenate all selected images in order
210
+ concat_img = concat_images_horizontally(list(results.values()))
211
+ results["concat"] = concat_img
212
 
213
+ return results, seed, f"✅ {preset_key} にリサイズして {len(selected_angles)} 視点+連結画像を生成しました"
 
214
 
215
  # --- UI ---
216
  css = """
217
  #col-container {margin: 0 auto; max-width: 1400px;}
218
  .image-container img {object-fit: contain !important; max-width: 100%; max-height: 100%;}
 
219
  """
220
 
221
  with gr.Blocks(css=css) as demo:
222
  with gr.Column(elem_id="col-container"):
223
  input_image = gr.Image(label="入力画像", type="pil", height=500)
224
+ extra_prompt = gr.Textbox(label="追加プロンプト", placeholder="high detail, anime style, soft lighting, 4k", lines=2)
225
+ preset_dropdown = gr.Dropdown(label="出力解像度プリセット", choices=list(RESOLUTIONS.keys()), value="nealy 9:16")
226
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
227
+ randomize_seed = gr.Checkbox(label="ランダムシード", value=True)
228
+
229
+ # --- Checklist for angles ---
230
+ select_all_checkbox = gr.Checkbox(label="Select All", value=True)
231
+ angles_checklist = gr.CheckboxGroup(
232
+ label="生成するカメラ視点を選択",
233
+ choices=list(BASE_PROMPTS.keys()),
234
+ value=list(BASE_PROMPTS.keys())
235
  )
236
+
237
+ # JS: update checklist when select_all changes
238
+ select_all_checkbox.change(
239
+ lambda select_all: list(BASE_PROMPTS.keys()) if select_all else [],
240
+ inputs=select_all_checkbox,
241
+ outputs=angles_checklist
242
  )
243
+
244
  run_button = gr.Button("🎨 生成開始", variant="primary")
245
  status_text = gr.Textbox(label="ステータス", interactive=False)
246
 
247
+ # Dynamic image outputs
248
+ image_outputs = {angle: gr.Image(label=angle, type="pil", format="png", height=400, show_download_button=True)
249
+ for angle in BASE_PROMPTS.keys()}
250
+ image_outputs["concat"] = gr.Image(label="連結画像", type="pil", format="png", height=400, show_download_button=True)
 
 
 
 
 
 
 
251
 
252
+ # Button click
253
  run_button.click(
254
  fn=generate_turnaround,
255
+ inputs=[input_image, angles_checklist, extra_prompt, preset_dropdown, seed, randomize_seed, seed, 4],
256
+ outputs=[image_outputs, seed, status_text],
257
  )
258
 
259
  if __name__ == "__main__":