import gradio as gr import spaces import torch import os from compel import Compel, ReturnedEmbeddingsType from diffusers import DiffusionPipeline # Load model model_name = os.environ.get('MODEL_NAME', 'UnfilteredAI/NSFW-gen-v2.1') pipe = DiffusionPipeline.from_pretrained( model_name, torch_dtype=torch.float16 ) pipe.to('cuda') # Compel setup compel = Compel( tokenizer=[pipe.tokenizer, pipe.tokenizer_2], text_encoder=[pipe.text_encoder, pipe.text_encoder_2], returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, requires_pooled=[False, True] ) # Default negative prompt default_negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation." # Example prompts example_prompts = [ ["a beautiful 19 aged rusian girl in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4], ["a beautiful japan woman in a vikini at the beach, noon sunset, professional photography, 8k", default_negative_prompt, 45, 7.0, 1024, 1024, 4], ] # Image generation function @spaces.GPU(duration=120) def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()): progress(0, desc="Preparing") embeds, pooled = compel(prompt) neg_embeds, neg_pooled = compel(negative_prompt) progress(0.1, desc="Generating images") # Define proper callback for step end def callback_on_step_end(pipe, i, t, callback_kwargs): progress((i + 1) / num_inference_steps) return callback_kwargs images = pipe( prompt_embeds=embeds, pooled_prompt_embeds=pooled, negative_prompt_embeds=neg_embeds, negative_pooled_prompt_embeds=neg_pooled, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, width=width, height=height, num_images_per_prompt=num_samples, callback_on_step_end=callback_on_step_end ).images return images # CSS styles css = """ .gallery-item { transition: transform 0.2s; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-radius: 10px; } .gallery-item:hover { transform: scale(1.03); box-shadow: 0 8px 16px rgba(0,0,0,0.2); } .container { max-width: 1200px; margin: auto; } .header { text-align: center; margin-bottom: 2rem; padding: 1rem; background: linear-gradient(90deg, rgba(76,0,161,0.8) 0%, rgba(28,110,164,0.8) 100%); border-radius: 10px; color: white; } .slider-container { background-color: #f5f5f5; padding: 1rem; border-radius: 10px; margin-bottom: 1rem; } .prompt-container { background-color: #f0f8ff; padding: 1rem; border-radius: 10px; margin-bottom: 1rem; border: 1px solid #d0e8ff; } .examples-header { background: linear-gradient(90deg, rgba(41,128,185,0.7) 0%, rgba(142,68,173,0.7) 100%); color: white; padding: 0.5rem; border-radius: 8px; text-align: center; margin-bottom: 0.5rem; } """ # Gradio interface with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo: gr.HTML("""
Enter creative prompts and generate high-quality images.
💡 Tip: For high-quality images, use detailed prompts and higher inference steps.
Example: Add quality terms like "professional photography, 8k, highly detailed, sharp focus, HDR" to your prompts.