# Configuration prod = False port = 8080 show_options = True # Changed to True for better visibility import os import random import time import gradio as gr import numpy as np import spaces import imageio from huggingface_hub import HfApi import gc import torch import cv2 from PIL import Image from diffusers import ( ControlNetModel, DPMSolverMultistepScheduler, StableDiffusionControlNetPipeline, ) from controlnet_aux_local import NormalBaeDetector MAX_SEED = np.iinfo(np.int32).max API_KEY = os.environ.get("API_KEY", None) print("CUDA version:", torch.version.cuda) print("loading everything") compiled = False api = HfApi() class Preprocessor: MODEL_ID = "lllyasviel/Annotators" def __init__(self): self.model = None self.name = "" def load(self, name: str) -> None: if name == self.name: return elif name == "NormalBae": print("Loading NormalBae") device = "cuda" if torch.cuda.is_available() else "cpu" self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to(device) if torch.cuda.is_available(): torch.cuda.empty_cache() self.name = name else: raise ValueError return def __call__(self, image: Image.Image, **kwargs) -> Image.Image: device = "cuda" if torch.cuda.is_available() else "cpu" if hasattr(self.model, 'device'): if self.model.device.type != device: print(f"Moving preprocessor model to {device}") try: self.model.to(device) except Exception as e: print(f"Error moving preprocessor model to {device}: {e}") pass else: print("Warning: Preprocessor model has no .device attribute. Attempting to move to correct device.") try: self.model.to(device) except Exception as e: print(f"Error attempting to move preprocessor model without .device attribute: {e}") pass return self.model(image, **kwargs) # Load models and preprocessor when the script starts model_id = "lllyasviel/control_v11p_sd15_normalbae" print("initializing controlnet") device = "cuda" if torch.cuda.is_available() else "cpu" controlnet = ControlNetModel.from_pretrained( model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, attn_implementation="flash_attention_2" if torch.cuda.is_available() else None, ).to(device) # Scheduler scheduler = DPMSolverMultistepScheduler.from_pretrained( "ashllay/stable-diffusion-v1-5-archive", solver_order=2, subfolder="scheduler", use_karras_sigmas=True, final_sigmas_type="sigma_min", algorithm_type="sde-dpmsolver++", prediction_type="epsilon", thresholding=False, denoise_final=True, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, ) # Stable Diffusion Pipeline URL base_model_url = "https://huggingface.co/Lykon/AbsoluteReality/blob/main/AbsoluteReality_1.8.1_pruned.safetensors" print('loading pipe') pipe = StableDiffusionControlNetPipeline.from_single_file( base_model_url, safety_checker=None, controlnet=controlnet, scheduler=scheduler, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, ).to(device) print("loading preprocessor") preprocessor = Preprocessor() preprocessor.load("NormalBae") # Load textual inversions try: textual_inversions = { "EasyNegativeV2": "EasyNegativeV2.safetensors", "badhandv4": "badhandv4.pt", "fcNeg-neg": "fcNeg-neg.pt", "HDA_Ahegao": "HDA_Ahegao.pt", "HDA_Bondage": "HDA_Bondage.pt", "HDA_pet_play": "HDA_pet_play.pt", "HDA_unconventional_maid": "HDA_unconventional maid.pt", "HDA_NakedHoodie": "HDA_NakedHoodie.pt", "HDA_NunDress": "HDA_NunDress.pt", "HDA_Shibari": "HDA_Shibari.pt", } for token, weight_name in textual_inversions.items(): try: pipe.load_textual_inversion( "broyang/hentaidigitalart_v20", weight_name=weight_name, token=token, ) print(f"Loaded textual inversion: {token}") except Exception as e: print(f"Warning: Could not load textual inversion {weight_name}: {e}") except Exception as e: print(f"Error during textual inversions loading process: {e}") print("---------------Loaded controlnet pipeline---------------") if torch.cuda.is_available(): torch.cuda.empty_cache() gc.collect() print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB") def get_additional_prompt(): prompt = "hyperrealistic photography,extremely detailed,(intricate details),unity 8k wallpaper,ultra detailed" top = ["tank top", "blouse", "button up shirt", "sweater", "corset top"] bottom = ["short skirt", "athletic shorts", "jean shorts", "pleated skirt", "short skirt", "leggings", "high-waisted shorts"] accessory = ["knee-high boots", "gloves", "Thigh-high stockings", "Garter belt", "choker", "necklace", "headband", "headphones"] return f"{prompt}, {random.choice(top)}, {random.choice(bottom)}, {random.choice(accessory)}, score_9" def get_prompt(prompt, additional_prompt): interior = "design-style interior designed (interior space),tungsten white balance,captured with a DSLR camera using f/10 aperture, 1/60 sec shutter speed, ISO 400, 20mm focal length" prompt_parts = [] if prompt: prompt_parts.append(f"Photo from Pinterest of {prompt}") else: prompt_parts.append("Photo from Pinterest of interior space") prompt_parts.append(interior) if additional_prompt: prompt_parts.append(additional_prompt) return ", ".join(filter(None, prompt_parts)) # Enhanced style list with more diverse options style_list = [ {"name": "None", "prompt": "" }, {"name": "Minimalistic", "prompt": "Minimalist interior design,clean lines,neutral colors,uncluttered space,functional furniture,lots of natural light" }, {"name": "Boho", "prompt": "Bohemian chic interior,eclectic mix of patterns and textures,vintage furniture,plants,woven textiles,warm earthy colors" }, {"name": "Farmhouse", "prompt": "Modern farmhouse interior,rustic wood elements,shiplap walls,neutral color palette,industrial accents,cozy textiles" }, {"name": "Saudi Prince", "prompt": "Opulent gold interior,luxurious ornate furniture,crystal chandeliers,rich fabrics,marble floors,intricate Arabic patterns" }, {"name": "Neoclassical", "prompt": "Neoclassical interior design,elegant columns,ornate moldings,symmetrical layout,refined furniture,muted color palette" }, {"name": "Eclectic", "prompt": "Eclectic interior design,mix of styles and eras,bold color combinations,diverse furniture pieces,unique art objects" }, {"name": "Parisian", "prompt": "Parisian apartment interior,all-white color scheme,ornate moldings,herringbone wood floors,elegant furniture,large windows" }, {"name": "Hollywood", "prompt": "Hollywood Regency interior,glamorous and luxurious,bold colors,mirrored surfaces,velvet upholstery,gold accents" }, {"name": "Scandinavian", "prompt": "Scandinavian interior design,light wood tones,white walls,minimalist furniture,cozy textiles,hygge atmosphere" }, {"name": "Beach", "prompt": "Coastal beach house interior,light blue and white color scheme,weathered wood,nautical accents,sheer curtains,ocean view" }, {"name": "Japanese", "prompt": "Traditional Japanese interior,tatami mats,shoji screens,low furniture,zen garden view,minimalist decor,natural materials" }, {"name": "Midcentury Modern", "prompt": "Mid-century modern interior,1950s-60s style furniture,organic shapes,warm wood tones,bold accent colors,large windows" }, {"name": "Retro Futurism", "prompt": "Neon (atompunk world) retro cyberpunk background", }, {"name": "Texan", "prompt": "Western cowboy interior,rustic wood beams,leather furniture,cowhide rugs,antler chandeliers,southwestern patterns" }, {"name": "Matrix", "prompt": "Futuristic cyberpunk interior,neon accent lighting,holographic plants,sleek black surfaces,advanced gaming setup,transparent screens,Blade Runner inspired decor,high-tech minimalist furniture" }, # New added styles {"name": "Industrial Loft", "prompt": "Industrial loft interior,exposed brick walls,metal finishes,high ceilings with exposed pipes,concrete floors,vintage factory lights,open floor plan" }, {"name": "Art Deco", "prompt": "Art Deco interior design,geometric patterns,bold colors,luxurious materials,symmetrical designs,metallic accents,sophisticated lighting" }, {"name": "Contemporary", "prompt": "Contemporary interior design,sleek finishes,neutral palette with bold accents,clean lines,minimal ornamentation,statement lighting,open concept" }, {"name": "Tropical Villa", "prompt": "Tropical villa interior,palm leaf patterns,natural materials,indoor plants,rattan furniture,light and airy spaces,ocean view,swimming pool" }, {"name": "Mediterranean", "prompt": "Mediterranean interior design,terracotta tiles,arched doorways,wrought iron details,warm color palette,hand-painted ceramics,indoor-outdoor living" }, {"name": "Gothic Victorian", "prompt": "Gothic Victorian interior,dark wood paneling,ornate furniture,velvet drapery,crystal chandeliers,rich jewel tones,antique decorative elements" }, {"name": "Rustic Cabin", "prompt": "Rustic mountain cabin interior,log walls,stone fireplace,wooden beams,cozy textiles,leather furniture,forest views,warm lighting" }, {"name": "Penthouse", "prompt": "Luxury penthouse interior,floor-to-ceiling windows,city skyline views,modern furniture,high-end appliances,marble countertops,designer lighting fixtures" }] styles = {k["name"]: (k["prompt"]) for k in style_list} STYLE_NAMES = list(styles.keys()) def apply_style(style_name): return styles.get(style_name, "") # Enhanced CSS for Gradio UI css = """ /* Global Styles */ :root { --primary-color: #3498db; --secondary-color: #2ecc71; --accent-color: #e74c3c; --text-color: #333; --light-bg: #f8f9fa; --border-radius: 10px; --box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: var(--text-color); background-color: var(--light-bg); } /* Typography */ h1, h2, h3 { text-align: center; display: block; color: var(--primary-color); margin-bottom: 1rem; } h1 { font-size: 2.5rem; margin-top: 1rem; font-weight: 700; } h2 { font-size: 1.8rem; color: var(--accent-color); } /* Layout */ footer { visibility: hidden; } .gradio-container { max-width: 1200px !important; margin: 0 auto; padding: 20px; } /* Image Containers */ .gr-image { display: flex; justify-content: center; align-items: center; width: 100%; height: 512px; overflow: hidden; border-radius: var(--border-radius); box-shadow: var(--box-shadow); transition: all 0.3s ease; } .gr-image:hover { box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2); } .gr-image img { width: 100%; height: 100%; object-fit: cover; object-position: center; border-radius: var(--border-radius); } /* Radio buttons styling */ .gr-radio-group { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; padding: 15px; background-color: white; border-radius: var(--border-radius); box-shadow: var(--box-shadow); } /* Buttons */ button.gr-button { background-color: var(--primary-color) !important; color: white !important; border: none !important; padding: 10px 20px !important; border-radius: var(--border-radius) !important; font-weight: bold !important; transition: all 0.3s ease !important; box-shadow: var(--box-shadow) !important; } button.gr-button:hover { background-color: var(--secondary-color) !important; transform: translateY(-2px) !important; box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15) !important; } /* Slider customization */ .gr-slider { margin-top: 10px !important; } /* Accordion styling */ .gr-accordion { margin-top: 20px; border-radius: var(--border-radius); overflow: hidden; box-shadow: var(--box-shadow); } /* Helper text */ .helper-text { background-color: #f0f8ff; padding: 10px; border-left: 4px solid var(--primary-color); margin: 15px 0; border-radius: 0 var(--border-radius) var(--border-radius) 0; } /* Style categories */ .style-category { font-weight: bold; margin-top: 10px; color: var(--accent-color); } /* Progress bar */ .gr-progress { height: 10px !important; border-radius: 5px !important; background-color: #e0e0e0 !important; } .gr-progress-bar { background-color: var(--secondary-color) !important; border-radius: 5px !important; } /* Main image section highlight */ .main-images { border: 2px solid var(--primary-color); border-radius: var(--border-radius); padding: 15px; background-color: white; margin-bottom: 20px; } /* Example images section */ .example-images { display: flex; justify-content: center; gap: 10px; margin-top: 10px; } .example-image { width: 120px; height: 90px; object-fit: cover; border-radius: 5px; cursor: pointer; transition: all 0.2s ease; border: 2px solid transparent; } .example-image:hover { transform: scale(1.05); border-color: var(--primary-color); } .example-thumb { border: 2px solid #ddd; border-radius: 8px; cursor: pointer; transition: all 0.3s ease; } .example-thumb:hover { border-color: var(--primary-color); transform: translateY(-2px); } """ # Load example images def load_examples(): examples = [] for i in range(1, 5): try: img_path = f"in{i}.jpg" if os.path.exists(img_path): examples.append(Image.open(img_path)) else: print(f"Warning: Example image {img_path} not found") except Exception as e: print(f"Error loading example image in{i}.jpg: {e}") return examples example_images = load_examples() # Function to select example image def select_example(index): if 0 <= index < len(example_images): return example_images[index] return None # Gradio Interface Definition with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo: gr.Markdown("