Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import AutoPipelineForText2Image
|
| 4 |
+
|
| 5 |
+
# 2. Load the model
|
| 6 |
+
# We use AutoPipelineForText2Image to automatically load the correct pipeline for SDXL.
|
| 7 |
+
# torch_dtype=torch.float16 is a memory optimization.
|
| 8 |
+
# variant="fp16" tells the model to use a smaller, faster version of the weights.
|
| 9 |
+
# .to("cuda") moves the model to the GPU for fast inference.
|
| 10 |
+
print("Loading model...")
|
| 11 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
variant="fp16",
|
| 15 |
+
use_safetensors=True
|
| 16 |
+
).to("cuda")
|
| 17 |
+
print("Model loaded.")
|
| 18 |
+
|
| 19 |
+
# 3. Define the image generation function
|
| 20 |
+
# This function will take a text prompt and return a generated image.
|
| 21 |
+
def generate_image(prompt, negative_prompt, steps, guidance):
|
| 22 |
+
print(f"Generating image for prompt: {prompt}")
|
| 23 |
+
# The 'pipe' object does all the work. We pass it the prompt and other parameters.
|
| 24 |
+
# num_inference_steps controls how many steps the model takes to generate the image.
|
| 25 |
+
# guidance_scale controls how much the model follows the prompt.
|
| 26 |
+
image = pipe(
|
| 27 |
+
prompt=prompt,
|
| 28 |
+
negative_prompt=negative_prompt,
|
| 29 |
+
num_inference_steps=steps,
|
| 30 |
+
guidance_scale=guidance
|
| 31 |
+
).images[0]
|
| 32 |
+
|
| 33 |
+
# The output is a PIL Image object, which Gradio can display directly.
|
| 34 |
+
return image
|
| 35 |
+
|
| 36 |
+
# 4. Create the Gradio interface
|
| 37 |
+
# This is where we design the web UI.
|
| 38 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="sky")) as demo:
|
| 39 |
+
gr.Markdown("# 🖼️ Stable Diffusion XL Text-to-Image")
|
| 40 |
+
gr.Markdown("Enter a text prompt and see the magic of AI-powered image generation!")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column(scale=4):
|
| 44 |
+
# Textbox for the main prompt
|
| 45 |
+
prompt_input = gr.Textbox(label="Your Prompt", placeholder="An astronaut riding a horse on Mars, cinematic, 4k")
|
| 46 |
+
# Textbox for the negative prompt
|
| 47 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="low quality, blurry, watermark, text")
|
| 48 |
+
# Submit button
|
| 49 |
+
submit_btn = gr.Button("Generate Image", variant="primary")
|
| 50 |
+
|
| 51 |
+
with gr.Column(scale=1):
|
| 52 |
+
# Sliders for advanced options
|
| 53 |
+
steps_slider = gr.Slider(minimum=10, maximum=100, value=25, step=1, label="Inference Steps")
|
| 54 |
+
guidance_slider = gr.Slider(minimum=0, maximum=20, value=7.5, step=0.1, label="Guidance Scale")
|
| 55 |
+
|
| 56 |
+
# Image component to display the output
|
| 57 |
+
output_image = gr.Image(label="Generated Image", width=768, height=768)
|
| 58 |
+
|
| 59 |
+
# Define some example prompts to make it easy for users to start
|
| 60 |
+
gr.Examples(
|
| 61 |
+
examples=[
|
| 62 |
+
["A majestic lion wearing a crown, photorealistic", "cartoon, drawing", 30, 8],
|
| 63 |
+
["A beautiful cityscape at sunset, painted by Van Gogh", "blurry, modern", 25, 7.5],
|
| 64 |
+
["A cute robot serving coffee in a futuristic cafe, 3D render", "text, humans", 35, 9],
|
| 65 |
+
],
|
| 66 |
+
inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# 5. Connect the button to the function
|
| 70 |
+
# When the submit button is clicked, it will call the `generate_image` function
|
| 71 |
+
# with the values from the input components. The result will be displayed in `output_image`.
|
| 72 |
+
submit_btn.click(
|
| 73 |
+
fn=generate_image,
|
| 74 |
+
inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider],
|
| 75 |
+
outputs=output_image
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# 6. Launch the application
|
| 79 |
+
demo.launch()
|