#!/usr/bin/env python

import os

import gradio as gr
import numpy as np
import PIL.Image
import spaces
import torch
from diffusers import AutoencoderKL, DiffusionPipeline

DESCRIPTION = "# SDXL"

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    vae=vae,
    torch_dtype=torch.float16,
    use_safetensors=True,
    variant="fp16",
).to(device)
refiner = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-refiner-1.0",
    vae=vae,
    torch_dtype=torch.float16,
    use_safetensors=True,
    variant="fp16",
).to(device)


def get_seed(randomize_seed: bool, seed: int) -> int:
    """Determine and return the random seed to use for model generation or sampling.

    - MAX_SEED is the maximum value for a 32-bit integer (np.iinfo(np.int32).max).
    - This function is typically used to ensure reproducibility or to introduce randomness in model generation.
    - The random seed affects the stochastic processes in downstream model inference or sampling.

    Args:
        randomize_seed (bool): If True, a random seed (an integer in [0, MAX_SEED)) is generated using NumPy's default random number generator. If False, the provided seed argument is returned as-is.
        seed (int): The seed value to use if randomize_seed is False.

    Returns:
        int: The selected seed value. If randomize_seed is True, a randomly generated integer; otherwise, the value of the seed argument.
    """
    rng = np.random.default_rng()
    return int(rng.integers(0, MAX_SEED)) if randomize_seed else seed


@spaces.GPU
def generate(
    prompt: str,
    negative_prompt: str = "",
    prompt_2: str = "",
    negative_prompt_2: str = "",
    use_negative_prompt: bool = False,
    use_prompt_2: bool = False,
    use_negative_prompt_2: bool = False,
    seed: int = 0,
    width: int = 1024,
    height: int = 1024,
    guidance_scale_base: float = 5.0,
    guidance_scale_refiner: float = 5.0,
    num_inference_steps_base: int = 25,
    num_inference_steps_refiner: int = 25,
    apply_refiner: bool = False,
    progress: gr.Progress = gr.Progress(track_tqdm=True),  # noqa: ARG001, B008
) -> PIL.Image.Image:
    """Generates an image from a text prompt using the SDXL (Stable Diffusion XL) model.

    This function allows fine-grained control over image generation through prompts,
    negative prompts, and optional refinement stages.

    Note:
        All prompt-related inputs (e.g., `prompt`, `negative_prompt`, `prompt_2`, and `negative_prompt_2`)
        must be written in English for proper model performance.

    Args:
        prompt (str): Main text prompt used to guide image generation.
        negative_prompt (str, optional): Text specifying elements to exclude from the image.
        prompt_2 (str, optional): Secondary prompt for additional guidance. Used only if `use_prompt_2` is True.
        negative_prompt_2 (str, optional): Secondary negative prompt. Used only if `use_negative_prompt_2` is True.
        use_negative_prompt (bool, optional): Whether to apply `negative_prompt` during generation.
        use_prompt_2 (bool, optional): Whether to apply `prompt_2` during generation.
        use_negative_prompt_2 (bool, optional): Whether to apply `negative_prompt_2` during generation.
        seed (int, optional): Seed for random number generation. Use 0 to generate a random seed.
        width (int, optional): Width of the output image in pixels.
        height (int, optional): Height of the output image in pixels.
        guidance_scale_base (float, optional): Guidance scale for the base model. Higher values follow the prompt more closely.
        guidance_scale_refiner (float, optional): Guidance scale for the refiner model.
        num_inference_steps_base (int, optional): Number of inference steps for the base model.
        num_inference_steps_refiner (int, optional): Number of inference steps for the refiner model.
        apply_refiner (bool, optional): Whether to apply the refiner stage after the base image is generated.
        progress (gr.Progress, optional): Gradio progress object to show progress during generation.

    Returns:
        PIL.Image.Image: The generated image as a PIL Image object.
    """
    generator = torch.Generator().manual_seed(seed)

    if not use_negative_prompt:
        negative_prompt = None  # type: ignore
    if not use_prompt_2:
        prompt_2 = None  # type: ignore
    if not use_negative_prompt_2:
        negative_prompt_2 = None  # type: ignore

    if not apply_refiner:
        return pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            prompt_2=prompt_2,
            negative_prompt_2=negative_prompt_2,
            width=width,
            height=height,
            guidance_scale=guidance_scale_base,
            num_inference_steps=num_inference_steps_base,
            generator=generator,
            output_type="pil",
        ).images[0]
    latents = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        prompt_2=prompt_2,
        negative_prompt_2=negative_prompt_2,
        width=width,
        height=height,
        guidance_scale=guidance_scale_base,
        num_inference_steps=num_inference_steps_base,
        generator=generator,
        output_type="latent",
    ).images
    images = refiner(
        prompt=prompt,
        negative_prompt=negative_prompt,
        prompt_2=prompt_2,
        negative_prompt_2=negative_prompt_2,
        guidance_scale=guidance_scale_refiner,
        num_inference_steps=num_inference_steps_refiner,
        image=latents,
        generator=generator,
    ).images
    return images[0]


examples = [
    "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
    "An astronaut riding a green horse",
]

with gr.Blocks(css_paths="style.css") as demo:
    gr.Markdown(DESCRIPTION)

    with gr.Group():
        with gr.Row():
            prompt = gr.Textbox(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                submit_btn=True,
            )
        result = gr.Image(label="Result", show_label=False)
    with gr.Accordion("Advanced options", open=False):
        with gr.Row():
            use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
            use_prompt_2 = gr.Checkbox(label="Use prompt 2", value=False)
            use_negative_prompt_2 = gr.Checkbox(label="Use negative prompt 2", value=False)
        negative_prompt = gr.Textbox(
            label="Negative prompt",
            max_lines=1,
            placeholder="Enter a negative prompt",
            visible=False,
            value="",
        )
        prompt_2 = gr.Textbox(
            label="Prompt 2",
            max_lines=1,
            placeholder="Enter your prompt",
            visible=False,
            value="",
        )
        negative_prompt_2 = gr.Textbox(
            label="Negative prompt 2",
            max_lines=1,
            placeholder="Enter a negative prompt",
            visible=False,
            value="",
        )

        seed = gr.Slider(
            label="Seed",
            minimum=0,
            maximum=MAX_SEED,
            step=1,
            value=0,
        )
        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        with gr.Row():
            width = gr.Slider(
                label="Width",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=1024,
            )
            height = gr.Slider(
                label="Height",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=1024,
            )
        apply_refiner = gr.Checkbox(label="Apply refiner", value=True)
        with gr.Row():
            guidance_scale_base = gr.Slider(
                label="Guidance scale for base",
                minimum=1,
                maximum=20,
                step=0.1,
                value=5.0,
            )
            num_inference_steps_base = gr.Slider(
                label="Number of inference steps for base",
                minimum=10,
                maximum=100,
                step=1,
                value=25,
            )
        with gr.Row() as refiner_params:
            guidance_scale_refiner = gr.Slider(
                label="Guidance scale for refiner",
                minimum=1,
                maximum=20,
                step=0.1,
                value=5.0,
            )
            num_inference_steps_refiner = gr.Slider(
                label="Number of inference steps for refiner",
                minimum=10,
                maximum=100,
                step=1,
                value=25,
            )

    gr.Examples(
        examples=examples,
        inputs=prompt,
        outputs=result,
        fn=generate,
    )

    use_negative_prompt.change(
        fn=lambda x: gr.Textbox(visible=x),
        inputs=use_negative_prompt,
        outputs=negative_prompt,
        queue=False,
        api_name=False,
    )
    use_prompt_2.change(
        fn=lambda x: gr.Textbox(visible=x),
        inputs=use_prompt_2,
        outputs=prompt_2,
        queue=False,
        api_name=False,
    )
    use_negative_prompt_2.change(
        fn=lambda x: gr.Textbox(visible=x),
        inputs=use_negative_prompt_2,
        outputs=negative_prompt_2,
        queue=False,
        api_name=False,
    )
    apply_refiner.change(
        fn=lambda x: gr.Row(visible=x),
        inputs=apply_refiner,
        outputs=refiner_params,
        queue=False,
        api_name=False,
    )

    gr.on(
        triggers=[
            prompt.submit,
            negative_prompt.submit,
            prompt_2.submit,
            negative_prompt_2.submit,
        ],
        fn=get_seed,
        inputs=[randomize_seed, seed],
        outputs=seed,
        queue=False,
    ).then(
        fn=generate,
        inputs=[
            prompt,
            negative_prompt,
            prompt_2,
            negative_prompt_2,
            use_negative_prompt,
            use_prompt_2,
            use_negative_prompt_2,
            seed,
            width,
            height,
            guidance_scale_base,
            guidance_scale_refiner,
            num_inference_steps_base,
            num_inference_steps_refiner,
            apply_refiner,
        ],
        outputs=result,
        api_name="predict",
    )

if __name__ == "__main__":
    demo.launch(mcp_server=True)