Upload app.py with huggingface_hub
Browse files247 unmodified lines2482492502512522533 unmodified lines25725825926026126226313 unmodified lines2772782792802812822 unmodified lines285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322247 unmodified lines@spaces.GPU(duration=estimate_duration)def generate_image(prompt: str = "",input_images=None,3 unmodified linesseed: int = 0,resolution: int = 1024,aspect_ratio: str = "Auto",progress=gr.Progress(track_tqdm=True),):"""Generate or edit an image with Qwen-Image-2.1.13 unmodified linesReturns:The generated image, as RGBA (the model natively supports transparency)."""prompt = (prompt or "").strip()if not prompt:raise gr.Error("Please enter a prompt.")2 unmodified linesif len(image_paths) > MAX_INPUT_IMAGES:raise gr.Error(f"Up to {MAX_INPUT_IMAGES} input images are supported.")# The UI already screened this in `prepare`; repeating it here is cheap (a 270M CPU# classifier) and keeps the guard in force for callers that hit /generate directly.if image_paths:check_guard(prompt)condition_images = [Image.open(p) for p in image_paths] or Nonewidth, height = resolve_size(resolution, aspect_ratio)negative_prompt = (negative_prompt or "").strip()call_kwargs = {}if negative_prompt and float(true_cfg_scale) > 1.0:call_kwargs["negative_prompt"] = negative_promptcall_kwargs["true_cfg_scale"] = float(true_cfg_scale)started = time.perf_counter()image = pipe(prompt=prompt,image=condition_images,height=height,width=width,output_resolution=int(resolution),num_inference_steps=int(num_inference_steps),generator=torch.Generator(device="cuda").manual_seed(int(seed) % (MAX_SEED + 1)),**call_kwargs,).images[0]print(f"[generate] images={len(image_paths)} steps={num_inference_steps} "f"res={resolution} ratio={aspect_ratio} size={image.size} "f"elapsed={time.perf_counter() - started:.1f}s",flush=True,)return imagedef prepare(prompt, input_images, enhance, seed, randomize_seed, aspect_ratio):247 unmodified lines2482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932943 unmodified lines29829930030130230313 unmodified lines3173183193203213223233243252 unmodified lines328329330331332333334335336337338339340341342343344345346347348247 unmodified lines@spaces.GPU(duration=estimate_duration)def _diffuse(prompt: str,image_paths: list,negative_prompt: str,true_cfg_scale: float,num_inference_steps: int,seed: int,resolution: int,aspect_ratio: str,progress=gr.Progress(track_tqdm=True),):"""Run the diffusion pipeline. Inputs are already validated and screened."""condition_images = [Image.open(p) for p in image_paths] or Nonewidth, height = resolve_size(resolution, aspect_ratio)negative_prompt = (negative_prompt or "").strip()call_kwargs = {}if negative_prompt and float(true_cfg_scale) > 1.0:call_kwargs["negative_prompt"] = negative_promptcall_kwargs["true_cfg_scale"] = float(true_cfg_scale)started = time.perf_counter()image = pipe(prompt=prompt,image=condition_images,height=height,width=width,output_resolution=int(resolution),num_inference_steps=int(num_inference_steps),generator=torch.Generator(device="cuda").manual_seed(int(seed) % (MAX_SEED + 1)),**call_kwargs,).images[0]print(f"[generate] images={len(image_paths)} steps={num_inference_steps} "f"res={resolution} ratio={aspect_ratio} size={image.size} "f"elapsed={time.perf_counter() - started:.1f}s",flush=True,)return imagedef generate_image(prompt: str = "",input_images=None,3 unmodified linesseed: int = 0,resolution: int = 1024,aspect_ratio: str = "Auto",):"""Generate or edit an image with Qwen-Image-2.1.13 unmodified linesReturns:The generated image, as RGBA (the model natively supports transparency)."""# Validation and screening deliberately happen here, in the main process: a rejected# prompt must never reserve a GPU slot, and gr.Error raised inside the ZeroGPU worker# reaches the browser as a bare "RuntimeError" instead of its message.prompt = (prompt or "").strip()if not prompt:raise gr.Error("Please enter a prompt.")2 unmodified linesif len(image_paths) > MAX_INPUT_IMAGES:raise gr.Error(f"Up to {MAX_INPUT_IMAGES} input images are supported.")# `prepare` already screened the UI path; this keeps the guard in force for callers# that hit /generate directly.if image_paths:check_guard(prompt)return _diffuse(prompt,image_paths,negative_prompt,true_cfg_scale,num_inference_steps,seed,resolution,aspect_ratio,)def prepare(prompt, input_images, enhance, seed, randomize_seed, aspect_ratio):
app.py+55-29