bibou.jpeg / app.py
halimbahae's picture
Update app.py
daa0aa3 verified
raw
history blame
2.97 kB
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.cuda.max_memory_allocated(device=device)
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
pipe.enable_xformers_memory_efficient_attention()
pipe = pipe.to(device)
else:
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
def infer(prompt, width, height):
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
image = pipe(
prompt=prompt,
guidance_scale=7.5,
num_inference_steps=50,
width=width,
height=height,
generator=generator
).images[0]
return image
examples = [
"Sunset over the Atlas Mountains",
"Flying carpet in space",
"Unicorn riding a camel in the Sahara Desert",
"Moroccan souk floating in the sky",
"Traditional Amazigh jewelry under the moonlight"
]
css="""
#col-container {
margin: 0 auto;
max-width: 840px;
color: #003366;
}
body {
background-color: white;
}
"""
if torch.cuda.is_available():
power_device = "GPU"
else:
power_device = "CPU"
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(f"""
# bibou.jpeg
Generate Moroccan folkloric pictures, inspired by Moroccan and Amazigh arts. 🎨🎶
Currently running on {power_device}.
""")
with gr.Row():
prompt = gr.Textbox(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False,
)
run_button = gr.Button("Run", scale=0)
result = gr.Image(label="Result", show_label=False)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=512,
maximum=3000,
step=32,
value=512,
)
height = gr.Slider(
label="Height",
minimum=512,
maximum=3000,
step=32,
value=512,
)
gr.Examples(
examples=examples,
inputs=[prompt]
)
gr.Markdown("""
<div style="text-align: center;">
Built with ❤️ by <a href="https://www.linkedin.com/in/halimbahae/" target="_blank">Bahae Eddine HALIM</a>
</div>
""")
run_button.click(
fn=infer,
inputs=[prompt, width, height],
outputs=[result]
)
demo.queue().launch()