moose Claude Opus 4.7 (1M context) commited on
Commit ·
b34fbef
1
Parent(s): 486537f
Expose a second image input slot in the UI
Browse filesThe Qwen-Image-Edit-Plus pipeline natively supports one or two reference
images, but the previous UI used a single Gallery widget that hid the
capability. Replaced with two explicit gr.Image components — Image 1
and Image 2 (optional). Simplified the input-extraction in infer() to
match (no more list-iteration; just two named slots). Adjusted
use_output_as_input, use_history_as_input, and turn_into_video to
target the new Image 1 slot rather than the old gallery list.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
24 unmodified lines25262728293031328 unmodified lines4142434445464735 unmodified lines8384858687888990919240 unmodified lines13313413513613713813914014114232 unmodified lines17517617717817918018116 unmodified lines19819920020120220320420520620720820921021121221321421521621721821922022122222375 unmodified lines29930030130230330430530630730877 unmodified lines38638738838939039139215 unmodified lines4084094104114124134144154164174184194204219 unmodified lines43143243343443543643743824 unmodified linesimport osimport gradio as grdef turn_into_video(input_images, output_images, prompt, progress=gr.Progress(track_tqdm=True)):if not input_images or not output_images:raise gr.Error("Please generate an output image first.")progress(0.02, desc="Preparing images...")8 unmodified lineselse:raise gr.Error(f"Unsupported image format: {type(img_entry)}")start_img = extract_pil(input_images[0])end_img = extract_pil(output_images[0])progress(0.10, desc="Saving temp files...")35 unmodified linesreturn historydef use_history_as_input(evt: gr.SelectData):"""Sets the selected history image as the new input image."""if evt.value is not None:# For filepath gallery, return the path directly in a listreturn gr.update(value=[evt.value])return gr.update()# --- Model Loading ---40 unmodified linesMAX_SEED = np.iinfo(np.int32).maxdef use_output_as_input(output_images):"""Convert output images to input format for the gallery"""if output_images is None or len(output_images) == 0:return []return output_images# --- Anonymous diagnostics: fire-and-forget POST of usage stats. ---def _emit_diagnostics(input_images, output_images, prompt, params):32 unmodified lines# --- Main Inference Function (with hardcoded negative prompt) ---@spaces.GPU(duration=60)def infer(images,prompt,seed=42,randomize_seed=False,16 unmodified lines# Set up the generator for reproducibilitygenerator = torch.Generator(device=device).manual_seed(seed)# Load input images into PIL Imagespil_images = []if images is not None:for item in images:try:if isinstance(item, str):# Direct file path from filepath gallerypil_images.append(Image.open(item).convert("RGB"))elif isinstance(item, tuple) and len(item) > 0:# Tuple format (legacy support)if isinstance(item[0], Image.Image):pil_images.append(item[0].convert("RGB"))elif isinstance(item[0], str):pil_images.append(Image.open(item[0]).convert("RGB"))elif isinstance(item, Image.Image):pil_images.append(item.convert("RGB"))elif hasattr(item, "name"):pil_images.append(Image.open(item.name).convert("RGB"))except Exception:continueif height==256 and width==256:height, width = None, None75 unmodified lines""")with gr.Row():with gr.Column():input_images = gr.Gallery(label="Input Images",show_label=False,type="filepath",interactive=True)prompt = gr.Text(label="Prompt 🪄",77 unmodified linestriggers=[run_button.click, prompt.submit],fn=infer,inputs=[input_images,prompt,seed,randomize_seed,15 unmodified linesuse_output_btn.click(fn=use_output_as_input,inputs=[result],outputs=[input_images])# History gallery event handlershistory_gallery.select(fn=use_history_as_input,inputs=None,outputs=[input_images],)9 unmodified linesinputs=None,outputs=[output_video],).then(fn=turn_into_video,inputs=[input_images, result, prompt],outputs=[output_video],)24 unmodified lines25262728293031328 unmodified lines4142434445464735 unmodified lines8384858687888990919240 unmodified lines13313413513613713813914014114214314414532 unmodified lines17817918018118218318418516 unmodified lines20220320420520620720820921021121221321421521621721821922022175 unmodified lines29729829930030130230330430577 unmodified lines38338438538638738838939015 unmodified lines4064074084094104114124134144154164174184199 unmodified lines42943043143243343443543624 unmodified linesimport osimport gradio as grdef turn_into_video(input_image, output_images, prompt, progress=gr.Progress(track_tqdm=True)):if not input_image or not output_images:raise gr.Error("Please generate an output image first.")progress(0.02, desc="Preparing images...")8 unmodified lineselse:raise gr.Error(f"Unsupported image format: {type(img_entry)}")start_img = extract_pil(input_image)end_img = extract_pil(output_images[0])progress(0.10, desc="Saving temp files...")35 unmodified linesreturn historydef use_history_as_input(evt: gr.SelectData):"""Sets the selected history image into the Image 1 slot."""if evt.value is not None:# gr.Image with type='filepath' accepts a path directly.return gr.update(value=evt.value)return gr.update()# --- Model Loading ---40 unmodified linesMAX_SEED = np.iinfo(np.int32).maxdef use_output_as_input(output_images):"""Move the first output image into the Image 1 slot."""if not output_images:return gr.update()first = output_images[0]# Gallery items can be filepath strings or (filepath, label) tuples.path = first[0] if isinstance(first, (list, tuple)) else firstreturn gr.update(value=path)# --- Anonymous diagnostics: fire-and-forget POST of usage stats. ---def _emit_diagnostics(input_images, output_images, prompt, params):32 unmodified lines# --- Main Inference Function (with hardcoded negative prompt) ---@spaces.GPU(duration=60)def infer(image_1,image_2,prompt,seed=42,randomize_seed=False,16 unmodified lines# Set up the generator for reproducibilitygenerator = torch.Generator(device=device).manual_seed(seed)# Load input images into PIL Images — two optional slots.pil_images = []for img in (image_1, image_2):if img is None:continuetry:if isinstance(img, str):pil_images.append(Image.open(img).convert("RGB"))elif isinstance(img, Image.Image):pil_images.append(img.convert("RGB"))elif hasattr(img, "name"):pil_images.append(Image.open(img.name).convert("RGB"))except Exception:continueif height==256 and width==256:height, width = None, None75 unmodified lines""")with gr.Row():with gr.Column():with gr.Row():image_1 = gr.Image(label="Image 1", type="filepath", interactive=True)image_2 = gr.Image(label="Image 2 (optional)", type="filepath", interactive=True)prompt = gr.Text(label="Prompt 🪄",77 unmodified linestriggers=[run_button.click, prompt.submit],fn=infer,inputs=[image_1,image_2,prompt,seed,randomize_seed,15 unmodified linesuse_output_btn.click(fn=use_output_as_input,inputs=[result],outputs=[image_1])# History gallery event handlershistory_gallery.select(fn=use_history_as_input,inputs=None,outputs=[image_1],)9 unmodified linesinputs=None,outputs=[output_video],).then(fn=turn_into_video,inputs=[image_1, result, prompt],outputs=[output_video],)
app.py+37-39