Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import random | |
| from diffusers import DiffusionPipeline | |
| import torch | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| if torch.cuda.is_available(): | |
| torch_dtype = torch.float16 | |
| else: | |
| torch_dtype = torch.float32 | |
| model_turbo_repo_id = "stabilityai/sdxl-turbo" | |
| model_repo_id = "stabilityai/stable-diffusion-2" | |
| pipe = DiffusionPipeline.from_pretrained(model_turbo_repo_id, torch_dtype=torch_dtype) | |
| pipe = pipe.to(device) | |
| # Recommended if your computer has < 64 GB of RAM | |
| pipe.enable_attention_slicing() | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 1024 | |
| def create_generator(seed): | |
| return torch.Generator().manual_seed(seed) | |
| def generate(prompt, negative_prompt, seed, is_random_seed, width, height, guidance_scale, inference_steps): | |
| if is_random_seed: | |
| seed = random.randint(0, np.iinfo(np.int32).max) | |
| generator = create_generator(seed) | |
| return pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| guidance_scale=guidance_scale, | |
| num_inference_steps=inference_steps, | |
| width=width, | |
| height=height, | |
| generator=generator | |
| ).images[0] | |
| with gr.Blocks() as imageGenerator: | |
| with gr.Column(): | |
| gr.Markdown(f""" | |
| # Zatsit Image Generator | |
| ## Générateur d'image basé sur des modèles de stable diffusion | |
| Vous cherchez de l'inspiration pour vos prompts ? | |
| [lien](https://stablediffusion.fr/prompts) | |
| """) | |
| with gr.Row(): | |
| prompt = gr.Text( | |
| label="Prompt", | |
| show_label=False, | |
| max_lines=4, | |
| placeholder="Votre prompt", | |
| container=False, | |
| ) | |
| generate_btn = gr.Button("Générer", scale=0) | |
| result = gr.Image(label="Image générée", show_label=False) | |
| with gr.Accordion("Paramètres", open=True): | |
| negative_prompt = gr.Text( | |
| label="Negative prompt", | |
| max_lines=4, | |
| placeholder="Votre prompt négatif", | |
| ) | |
| seed = gr.Slider( | |
| label="Seed", | |
| minimum=0, | |
| maximum=MAX_SEED, | |
| step=1, | |
| value=70537634, | |
| ) | |
| is_random_seed = gr.Checkbox(label="Seed en mode random", value=False) | |
| width = gr.Slider( | |
| label="Width", | |
| minimum=256, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=10, | |
| value=1024, | |
| ) | |
| height = gr.Slider( | |
| label="Height", | |
| minimum=256, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=10, | |
| value=1024, | |
| ) | |
| guidance_scale = gr.Slider( | |
| label="Guildance scale", | |
| minimum=0, | |
| maximum=10.0, | |
| step=0.1, | |
| value=0.0, | |
| ) | |
| inference_steps = gr.Slider( | |
| label="Nombre d'inférences", | |
| minimum=0, | |
| maximum=100.0, | |
| step=1, | |
| value=2, | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["pikachu eating spagetti, Antonio J. Manzanedo", 3384976558, 7, 20], | |
| ["Gandalf from Lord of the Rings, diffuse lighting, fantasy, intricate elegant highly detailed " | |
| "lifelike photorealistic digital painting, artstation", 248215544, 7, 42], | |
| ["Ethereal gardens of marble built in a shining teal river in future city, gorgeous ornate " | |
| "multi-tiered fountain, futuristic, intricate elegant highly detailed lifelike photorealistic " | |
| "realistic painting, long shot, studio lighting, octane render, by Dorian Cleavenger", 3868142022, | |
| 7, 20], | |
| ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", 70537634, 1, 2] | |
| ], | |
| inputs=[prompt, seed, guidance_scale, inference_steps] | |
| ) | |
| gr.on( | |
| triggers=[generate_btn.click, prompt.submit], | |
| fn=generate, | |
| inputs=[prompt, negative_prompt, seed, is_random_seed, width, height, guidance_scale, inference_steps], | |
| outputs=[result] | |
| ) | |
| imageGenerator.queue().launch() | |