Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516177 unmodified lines252627282930312 unmodified lines343536373839401 unmodified line424344454647489 unmodified lines5859606162636465666768697071727374754 unmodified lines8081828384858687888990919213 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781796 unmodified lines1861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162174 unmodified lines2222232242252262272282292302312322332342352362372 unmodified lines24024124224324424524624724824925025125225325425525625725825926026126226313 unmodified lines2772782792802812822832842852862872882892902912922932942952962972982993003011 unmodified line30330430530630730810 unmodified lines3193203213223233243253263271 unmodified line329330331332333334import osimport gcimport randomimport threadingimport tracebackimport gradio as grimport torchimport spacesfrom diffusers import QwenImage21Pipeline# ============================================================# CONFIGURATION# ============================================================BASE_MODEL = "Qwen/Qwen-Image-2.1"7 unmodified linesgeneration_lock = threading.Lock()# ============================================================# MODEL LOADING# ============================================================def load_pipeline():2 unmodified linesif pipe is not None:return pipeprint("Loading Qwen Image 2.1...", flush=True)pipeline = QwenImage21Pipeline.from_pretrained(BASE_MODEL,1 unmodified linelow_cpu_mem_usage=True,)print("Loading Pixelship LoRA...", flush=True)pipeline.load_lora_weights(LORA_REPO,9 unmodified linespipe = pipelineprint("Pixelship 2 ready!", flush=True)return pipe# ============================================================# GENERATION# ============================================================@spaces.GPU(duration=120)def generate(prompt,negative_prompt,width,height,4 unmodified linesrandom_seed,progress=gr.Progress(),):if not prompt or not prompt.strip():raise gr.Error("Please enter a prompt.")with generation_lock:try:progress(0, desc="Loading Pixelship 2")pipeline = load_pipeline()13 unmodified lineskwargs = {"prompt": prompt,"width": int(width),"height": int(height),"num_inference_steps": int(steps),"true_cfg_scale": float(true_cfg),"generator": generator,}if float(true_cfg) > 1.0:kwargs["negative_prompt"] = negative_prompt or ""progress(0.1, desc="Generating image")with torch.no_grad():result = pipeline(**kwargs)image = result.images[0]progress(1, desc="Generation complete")return (image,f"Seed: {seed} | Steps: {steps} | "f"True CFG: {true_cfg} | "f"LoRA Strength: {lora_strength}")except torch.cuda.OutOfMemoryError:gc.collect()if torch.cuda.is_available():torch.cuda.empty_cache()raise gr.Error("GPU ran out of memory. ""Try reducing the resolution.")except Exception as e:traceback.print_exc()raise gr.Error(f"Generation failed: {e}")# ============================================================# INTERFACE# ============================================================CSS = """.gradio-container {max-width: 1500px !important;margin: auto !important;}#prompt-box textarea {min-height: 260px !important;font-family: monospace !important;font-size: 14px !important;}#generate-button {font-size: 18px !important;font-weight: 700 !important;}#output-image {min-height: 550px;}"""with gr.Blocks(6 unmodified lines"""# π¨ Pixelship 2### Powered by Qwen Image 2.1Generate images using the Pixelship 2 LoRA.**LoRA loaded directly from Hugging Face β’ ZeroGPU**""")with gr.Row():with gr.Column(scale=5):prompt = gr.Textbox(label="Prompt",placeholder="Describe your image in as much detail as you want...",lines=12,max_lines=40,max_length=None,elem_id="prompt-box",)negative_prompt = gr.Textbox(label="Negative Prompt",placeholder="Optional negative prompt...",lines=3,max_length=None,)with gr.Accordion(4 unmodified lineswith gr.Row():width = gr.Slider(minimum=256,maximum=2048,value=1024,step=64,label="Width",)height = gr.Slider(minimum=256,maximum=2048,value=1024,step=64,label="Height",2 unmodified lineswith gr.Row():steps = gr.Slider(minimum=1,maximum=100,value=30,step=1,label="Inference Steps",)true_cfg = gr.Slider(minimum=1.0,maximum=10.0,value=1.0,step=0.1,label="True CFG Scale",)lora_strength = gr.Slider(minimum=0.0,maximum=2.0,value=0.75,step=0.05,label="LoRA Strength",13 unmodified lines)generate_button = gr.Button("Generate Image",variant="primary",size="lg",elem_id="generate-button",)with gr.Column(scale=5):output_image = gr.Image(label="Generated Image",type="pil",format="png",interactive=False,height=650,elem_id="output-image",)status = gr.Textbox(label="Generation Status",interactive=False,)1 unmodified linefn=generate,inputs=[prompt,negative_prompt,width,height,10 unmodified linesconcurrency_limit=1,)# ============================================================# QUEUE / LAUNCH# ============================================================demo.queue(1 unmodified linedefault_concurrency_limit=1,)if __name__ == "__main__":demo.launch(server_name="0.0.0.0",123456789101112131415167 unmodified lines242526272829302 unmodified lines333435363738391 unmodified line414243444546479 unmodified lines575859606162636465666768697071727374754 unmodified lines8081828384858687888990919213 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881896 unmodified lines1961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432444 unmodified lines2492502512522532542552562572582592602612622632642 unmodified lines26726826927027127227327427527627727827928028128228328428528628728828929013 unmodified lines3043053063073083093103113123133143153163173183193203213223233243253263273283293301 unmodified line33233333433533633733810 unmodified lines3493503513523533543553561 unmodified line358359360361362363364365366367import gcimport randomimport threadingimport tracebackimport gradio as grimport spacesimport torchfrom diffusers import QwenImage21Pipeline# ============================================================# CONFIG# ============================================================BASE_MODEL = "Qwen/Qwen-Image-2.1"7 unmodified linesgeneration_lock = threading.Lock()# ============================================================# LOAD PIPELINE# ============================================================def load_pipeline():2 unmodified linesif pipe is not None:return pipeprint("Loading Qwen-Image-2.1...", flush=True)pipeline = QwenImage21Pipeline.from_pretrained(BASE_MODEL,1 unmodified linelow_cpu_mem_usage=True,)print("Loading Pixelship 2.0 LoRA...", flush=True)pipeline.load_lora_weights(LORA_REPO,9 unmodified linespipe = pipelineprint("Pipeline ready.", flush=True)return pipe# ============================================================# GENERATE / EDIT# ============================================================@spaces.GPU(duration=120)def generate(prompt,input_image,negative_prompt,width,height,4 unmodified linesrandom_seed,progress=gr.Progress(),):if not prompt.strip():raise gr.Error("Prompt is required.")with generation_lock:try:progress(0.0, desc="Loading model")pipeline = load_pipeline()13 unmodified lineskwargs = {"prompt": prompt,"num_inference_steps": int(steps),"true_cfg_scale": float(true_cfg),"generator": generator,}if input_image is not None:kwargs["image"] = input_imageelse:kwargs["width"] = int(width)kwargs["height"] = int(height)if float(true_cfg) > 1.0:kwargs["negative_prompt"] = (negative_prompt or "")progress(0.15, desc="Generating")with torch.no_grad():result = pipeline(**kwargs)image = result.images[0]mode = ("Image Editing"if input_image is not Noneelse "Text Generation")progress(1.0, desc="Complete")return (image,f"{mode} | Seed={seed} | "f"Steps={steps} | "f"CFG={true_cfg} | "f"LoRA={lora_strength}")except torch.cuda.OutOfMemoryError:gc.collect()if torch.cuda.is_available():torch.cuda.empty_cache()raise gr.Error("Out of VRAM. Try a smaller image.")except Exception as e:traceback.print_exc()raise gr.Error(f"Generation failed:\n{e}")# ============================================================# UI# ============================================================CSS = """.gradio-container {max-width: 1600px !important;margin: auto !important;}#prompt-box textarea {min-height: 240px !important;font-family: monospace !important;}#generate-button {font-size: 18px !important;font-weight: 700 !important;}"""with gr.Blocks(6 unmodified lines"""# π¨ Pixelship 2### Qwen Image 2.1 + Pixelship 2.0Supports:β Text β Imageβ Image β Image Editingβ Adjustable LoRA Strengthβ ZeroGPU Compatible""")with gr.Row():# ========================================================# LEFT# ========================================================with gr.Column(scale=5):prompt = gr.Textbox(label="Prompt",lines=12,max_lines=40,max_length=None,elem_id="prompt-box",placeholder=("Describe what you want..."),)input_image = gr.Image(label="Image To Edit (Optional)",type="pil",)negative_prompt = gr.Textbox(label="Negative Prompt",lines=3,max_length=None,placeholder="Optional",)with gr.Accordion(4 unmodified lineswith gr.Row():width = gr.Slider(256,2048,value=1024,step=64,label="Width",)height = gr.Slider(256,2048,value=1024,step=64,label="Height",2 unmodified lineswith gr.Row():steps = gr.Slider(1,100,value=30,step=1,label="Steps",)true_cfg = gr.Slider(1.0,10.0,value=1.0,step=0.1,label="True CFG",)lora_strength = gr.Slider(0.0,2.0,value=0.75,step=0.05,label="LoRA Strength",13 unmodified lines)generate_button = gr.Button("Generate",variant="primary",size="lg",elem_id="generate-button",)# ========================================================# RIGHT# ========================================================with gr.Column(scale=5):output_image = gr.Image(label="Output",type="pil",format="png",height=700,)status = gr.Textbox(label="Status",interactive=False,)1 unmodified linefn=generate,inputs=[prompt,input_image,negative_prompt,width,height,10 unmodified linesconcurrency_limit=1,)# ============================================================# QUEUE# ============================================================demo.queue(1 unmodified linedefault_concurrency_limit=1,)# ============================================================# LAUNCH# ============================================================if __name__ == "__main__":demo.launch(server_name="0.0.0.0",
app.py+88-55