Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files8 unmodified lines9101112131415161718193 unmodified lines232425262728293031323334353615 unmodified lines52535455565758596061626324 unmodified lines8889909192939495969798991001019 unmodified lines1111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512521 unmodified line2542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852868 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteSchedulerDESCRIPTIONz= """## SDXL-LoRA-DLC ⚡"""def save_image(img):unique_name = str(uuid.uuid4()) + ".png"img.save(unique_name)return unique_name3 unmodified linesreturn seedMAX_SEED = np.iinfo(np.int32).maxif not torch.cuda.is_available():DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU. If it runs for an extended time or if you encounter errors, try running it on a GPU by duplicating the space using @spaces.GPU(). +import spaces.📍</p>"USE_TORCH_COMPILE = 0ENABLE_CPU_OFFLOAD = 0if torch.cuda.is_available():pipe = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0_Lightning",torch_dtype=torch.float16,15 unmodified lines"Canes Cars (realistic/futurecars)🚘": ("prithivMLmods/Canes-Cars-Model-LoRA", "Canes-Cars-Model-LoRA.safetensors", "car"),"Pencil Art (characteristic/creative)✏️": ("prithivMLmods/Canopus-Pencil-Art-LoRA", "Canopus-Pencil-Art-LoRA.safetensors", "Pencil Art"),"Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),}for model_name, weight_name, adapter_name in LORA_OPTIONS.values():pipe.load_lora_weights(model_name, weight_name=weight_name, adapter_name=adapter_name)pipe.to("cuda")style_list = [{24 unmodified linesSTYLE_NAMES = list(styles.keys())def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:if style_name in styles:p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])else:p, n = styles[DEFAULT_STYLE_NAME]if not negative:negative = ""return p.replace("{prompt}", positive), n + negative@spaces.GPU(duration=180, enable_queue=True)def generate(9 unmodified lineslora_model: str = "Realism (face/character)👦🏻",progress=gr.Progress(track_tqdm=True),):seed = int(randomize_seed_fn(seed, randomize_seed))positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)if not use_negative_prompt:effective_negative_prompt = "" # type: ignoremodel_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]pipe.set_adapters(adapter_name)images = pipe(prompt=positive_prompt,negative_prompt=effective_negative_prompt,width=width,height=height,guidance_scale=guidance_scale,num_inference_steps=20,num_images_per_prompt=1,cross_attention_kwargs={"scale": 0.65},output_type="pil",).imagesimage_paths = [save_image(img) for img in images]return image_paths, seedexamples = ["Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational ","Pixar: A young man with light brown wavy hair and light brown eyes sitting in an armchair and looking directly at the camera, pixar style, disney pixar, office background, ultra detailed, 1 man","Hoodie: Front view, capture a urban style, Superman Hoodie, technical materials, fabric small point label on text Blue theory, the design is minimal, with a raised collar, fabric is a Light yellow, low angle to capture the Hoodies form and detailing, f/5.6 to focus on the hoodies craftsmanship, solid grey background, studio light setting, with batman logo in the chest region of the t-shirt",]css = '''.gradio-container{max-width: 545px !important}h1{text-align:center}footer {visibility: hidden}'''def load_predefined_images():predefined_images = ["assets/1.png","assets/2.png","assets/3.png","assets/4.png","assets/5.png","assets/6.png","assets/7.png","assets/8.png","assets/9.png",]return predefined_imageswith gr.Blocks(css=css) as demo:gr.Markdown(DESCRIPTIONz)with gr.Row():with gr.Column(scale=1):with gr.Row():prompt = gr.Text(show_label=False,max_lines=1,placeholder="Enter your prompt",container=False,)run_button = gr.Button("Run", scale=0, variant="primary")with gr.Accordion("Advanced options", open=False, visible=False):use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)negative_prompt = gr.Text(label="Negative prompt",lines=4,max_lines=6,value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",placeholder="Enter a negative prompt",visible=True,)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=0,visible=True)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Row(visible=True):width = gr.Slider(label="Width",minimum=512,maximum=2048,step=8,value=1024,)height = gr.Slider(label="Height",minimum=512,maximum=2048,step=8,value=1024,)with gr.Row():guidance_scale = gr.Slider(label="Guidance Scale",minimum=0.1,maximum=20.0,step=0.1,value=3.0,)style_selection = gr.Radio(show_label=True,container=True,interactive=True,choices=STYLE_NAMES,value=DEFAULT_STYLE_NAME,label="Quality Style",)with gr.Row(visible=True):model_choice = gr.Dropdown(label="LoRA Selection",choices=list(LORA_OPTIONS.keys()),value="Realism (face/character)👦🏻")gr.Examples(examples=examples,inputs=prompt,outputs=[result, seed],fn=generate,cache_examples=False,)use_negative_prompt.change(fn=lambda x: gr.update(visible=x),inputs=use_negative_prompt,1 unmodified lineapi_name=False,)gr.on(triggers=[prompt.submit,negative_prompt.submit,run_button.click,],fn=generate,inputs=[prompt,negative_prompt,use_negative_prompt,seed,width,height,guidance_scale,randomize_seed,style_selection,model_choice,],outputs=[result, seed],api_name="run",)with gr.Column(scale=3):gr.Markdown("### Image Gallery")predefined_gallery = gr.Gallery(label="Image Gallery", columns=3, show_label=False, value=load_predefined_images())if __name__ == "__main__":demo.queue(max_size=30).launch()8 unmodified lines9101112131415161718192021222324252627283 unmodified lines3233343536373839404142434445464715 unmodified lines63646566676869707172737475767778798081828324 unmodified lines1081091101111121131141151161171189 unmodified lines1281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233241 unmodified line3263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994008 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteSchedulerDESCRIPTIONz = """## SDXL-LoRA-DLC ⚡"""# Ensure assets directory exists if needed for predefined imagesif not os.path.exists("assets"):print("Warning: 'assets' directory not found. Predefined gallery might be empty.")# Optionally create it: os.makedirs("assets")def save_image(img):# Ensure an 'outputs' directory exists to save generated images (optional, good practice)output_dir = "outputs"if not os.path.exists(output_dir):os.makedirs(output_dir)unique_name = os.path.join(output_dir, str(uuid.uuid4()) + ".png")img.save(unique_name)return unique_name3 unmodified linesreturn seedMAX_SEED = np.iinfo(np.int32).maxpipe = None # Initialize pipe to Noneif not torch.cuda.is_available():DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU. If it runs for an extended time or if you encounter errors, try running it on a GPU by duplicating the space using @spaces.GPU(). +import spaces.📍</p>"# Optionally, you could add a placeholder or disable functionality hereelse:USE_TORCH_COMPILE = False # Set to False as 0 is not standard booleanENABLE_CPU_OFFLOAD = False # Set to False as 0 is not standard boolean# Moved pipe initialization inside the CUDA checkpipe = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0_Lightning",torch_dtype=torch.float16,15 unmodified lines"Canes Cars (realistic/futurecars)🚘": ("prithivMLmods/Canes-Cars-Model-LoRA", "Canes-Cars-Model-LoRA.safetensors", "car"),"Pencil Art (characteristic/creative)✏️": ("prithivMLmods/Canopus-Pencil-Art-LoRA", "Canopus-Pencil-Art-LoRA.safetensors", "Pencil Art"),"Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),}# Load LoRAs only if pipe is initializedif pipe:for model_name, weight_name, adapter_name in LORA_OPTIONS.values():try:pipe.load_lora_weights(model_name, weight_name=weight_name, adapter_name=adapter_name)print(f"Loaded LoRA: {adapter_name}")except Exception as e:print(f"Warning: Could not load LoRA {adapter_name} from {model_name}. Error: {e}")pipe.to("cuda")print("Pipeline and LoRAs loaded to CUDA.")else:print("Pipeline not initialized (likely no CUDA available).")style_list = [{24 unmodified linesSTYLE_NAMES = list(styles.keys())def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME]) # Use .get for safetyif not negative:negative = ""return p.replace("{prompt}", positive), n + " " + negative # Add space for clarity@spaces.GPU(duration=180, enable_queue=True)def generate(9 unmodified lineslora_model: str = "Realism (face/character)👦🏻",progress=gr.Progress(track_tqdm=True),):if pipe is None:raise gr.Error("Pipeline not initialized. Check if CUDA is available and drivers are installed.")seed = int(randomize_seed_fn(seed, randomize_seed))# Apply style firstpositive_prompt, base_negative_prompt = apply_style(style_name, prompt, negative_prompt if use_negative_prompt else "")# If user explicitly provided a negative prompt and wants to use it, append it# (apply_style already incorporates the style's negative prompt)# This logic might need adjustment depending on desired behavior: replace or append?# Current: Style neg prompt + user neg prompteffective_negative_prompt = base_negative_promptif use_negative_prompt and negative_prompt:# Check if the negative prompt from apply_style is already there to avoid duplicationif not negative_prompt in effective_negative_prompt:effective_negative_prompt = (effective_negative_prompt + " " + negative_prompt).strip()# Ensure LoRA selection is validif lora_model not in LORA_OPTIONS:print(f"Warning: Invalid LoRA selection '{lora_model}'. Using default or first available.")# Fallback logic could be added here, e.g., use the first keylora_model = next(iter(LORA_OPTIONS)) # Get the first key as a fallbackmodel_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]try:print(f"Setting adapter: {adapter_name}")pipe.set_adapters(adapter_name)# Optional: Add LoRA scale if needed, often done via cross_attention_kwargs# Example: cross_attention_kwargs={"scale": lora_scale}# Note: RealVisXL Lightning might not need explicit scale adjustments like older models.# Using 0.65 as hardcoded before. Keeping it.lora_scale = 0.65print(f"Generating with prompt: '{positive_prompt}'")print(f"Negative prompt: '{effective_negative_prompt}'")print(f"Seed: {seed}, W: {width}, H: {height}, Scale: {guidance_scale}, Steps: 20")images = pipe(prompt=positive_prompt,negative_prompt=effective_negative_prompt,width=width,height=height,guidance_scale=guidance_scale,num_inference_steps=20, # Lightning models use fewer stepsnum_images_per_prompt=1,generator=torch.Generator("cuda").manual_seed(seed), # Ensure reproducibilitycross_attention_kwargs={"scale": lora_scale}, # Apply LoRA scale if neededoutput_type="pil",).imagesimage_paths = [save_image(img) for img in images]print(f"Generated {len(image_paths)} image(s).")return image_paths, seedexcept Exception as e:print(f"Error during generation: {e}")# Raise a Gradio error to display it in the UIimport tracebacktraceback.print_exc()raise gr.Error(f"Generation failed: {e}")examples = [["Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational"],["Pixar: A young man with light brown wavy hair and light brown eyes sitting in an armchair and looking directly at the camera, pixar style, disney pixar, office background, ultra detailed, 1 man"],["Hoodie: Front view, capture a urban style, Superman Hoodie, technical materials, fabric small point label on text Blue theory, the design is minimal, with a raised collar, fabric is a Light yellow, low angle to capture the Hoodies form and detailing, f/5.6 to focus on the hoodies craftsmanship, solid grey background, studio light setting, with batman logo in the chest region of the t-shirt"],]css = '''.gradio-container{max-width: 780px !important; margin: auto;}h1{text-align:center}#gallery { min-height: 400px; }footer { display: none !important; visibility: hidden !important; }'''def load_predefined_images():predefined_images = []asset_dir = "assets"if os.path.exists(asset_dir):valid_extensions = {".png", ".jpg", ".jpeg", ".webp"}try:for i in range(1, 10): # Try loading 1.png to 9.pngfor ext in valid_extensions:img_path = os.path.join(asset_dir, f"{i}{ext}")if os.path.exists(img_path):predefined_images.append(img_path)break # Found image for this number, move to nextexcept Exception as e:print(f"Error loading predefined images: {e}")if not predefined_images:print("No predefined images found in assets folder (e.g., assets/1.png, assets/2.jpg).")return predefined_images# --- Gradio UI Definition ---with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:gr.Markdown(DESCRIPTIONz)# Define the output gallery component firstresult_gallery = gr.Gallery(label="Generated Images",show_label=False,elem_id="gallery", # For CSS stylingcolumns=1, # Adjust as neededheight="auto")# Define the output seed componentoutput_seed = gr.State(value=0) # Use gr.State for non-displayed outputs or values needing persistencewith gr.Row():prompt = gr.Textbox(label="Prompt",show_label=False,max_lines=2,placeholder="Enter your prompt here...",container=False,scale=7 # Give more space to prompt)run_button = gr.Button("Generate", scale=1, variant="primary")with gr.Row():model_choice = gr.Dropdown(label="LoRA Selection",choices=list(LORA_OPTIONS.keys()),value="Realism (face/character)👦🏻", # Default selectionscale=3)style_selection = gr.Radio(show_label=False, # Label provided by Row context or Accordioncontainer=True,interactive=True,choices=STYLE_NAMES,value=DEFAULT_STYLE_NAME,label="Quality Style",scale=2)with gr.Accordion("Advanced options", open=False):with gr.Row():use_negative_prompt = gr.Checkbox(label="Use Negative Prompt", value=True, scale=1)randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, scale=1)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=0, # Initial valuevisible=True, # Controlled by randomize_seed logic later if neededscale=3)negative_prompt = gr.Textbox(label="Negative Prompt",lines=2,max_lines=4,value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",placeholder="Enter things to avoid...",visible=True, # Controlled by use_negative_prompt checkbox)with gr.Row():width = gr.Slider(label="Width",minimum=512,maximum=1536, # Adjusted max for typical SDXL usagestep=64, # Step by 64 for common resolutionsvalue=1024,)height = gr.Slider(label="Height",minimum=512,maximum=1536, # Adjusted maxstep=64, # Step by 64value=1024,)guidance_scale = gr.Slider(label="Guidance Scale (CFG)",minimum=1.0, # Usually start CFG from 1maximum=10.0, # Lightning models often use low CFGstep=0.1,value=3.0,)# --- Event Listeners ---# Toggle negative prompt visibilityuse_negative_prompt.change(fn=lambda x: gr.update(visible=x),inputs=use_negative_prompt,1 unmodified lineapi_name=False,)# Toggle seed slider visibility based on randomize checkbox# def toggle_seed_visibility(randomize):# return gr.update(interactive=not randomize)# randomize_seed.change(# fn=toggle_seed_visibility,# inputs=randomize_seed,# outputs=seed,# api_name=False# )# --- Image Generation Trigger ---inputs = [prompt,negative_prompt,use_negative_prompt,seed,width,height,guidance_scale,randomize_seed,style_selection,model_choice,]# Define outputs using the created componentsoutputs = [result_gallery, # The gallery to display imagesoutput_seed # The state to hold the used seed]# Connect the generate function to the button click and prompt submitgr.on(triggers=[run_button.click, prompt.submit],fn=generate,inputs=inputs,outputs=outputs,api_name="run" # Keep API name if needed)# Update the seed slider display when a new seed is generated and returned via output_seedoutput_seed.change(fn=lambda x: x, inputs=output_seed, outputs=seed, api_name=False)# --- Examples ---gr.Examples(examples=examples,inputs=[prompt], # Only prompt needed for examplesoutputs=[result_gallery, output_seed], # Update example outputs as wellfn=generate, # Function to run when example is clickedcache_examples=os.getenv("CACHE_EXAMPLES", "False").lower() == "true" # Cache examples in Spaces)# --- Predefined Image Gallery (Static) ---with gr.Column(): # Use column for better layout control if neededgr.Markdown("### Example Gallery (Predefined)")try:predefined_gallery_images = load_predefined_images()if predefined_gallery_images:predefined_gallery = gr.Gallery(label="Predefined Images",value=predefined_gallery_images,columns=3,show_label=False)else:gr.Markdown("_(No predefined images found in 'assets' folder)_")except Exception as e:gr.Markdown(f"_Error loading predefined gallery: {e}_")# --- Launch the App ---if __name__ == "__main__":demo.queue(max_size=20).launch(debug=True) # Add debug=True for more detailed logs
app.py+253-139