Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,23 @@ import torch
|
|
| 3 |
from diffusers import AutoPipelineForText2Image
|
| 4 |
|
| 5 |
# 2. Load the model
|
| 6 |
-
# We
|
| 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 |
-
)
|
| 17 |
-
print("Model loaded.")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
# This
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
|
@@ -33,30 +35,23 @@ def generate_image(prompt, negative_prompt, steps, guidance):
|
|
| 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],
|
|
@@ -66,9 +61,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="sky"))
|
|
| 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],
|
|
|
|
| 3 |
from diffusers import AutoPipelineForText2Image
|
| 4 |
|
| 5 |
# 2. Load the model
|
| 6 |
+
# We remove the .to("cuda") from the end of this line.
|
|
|
|
|
|
|
|
|
|
| 7 |
print("Loading model...")
|
| 8 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 9 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 10 |
torch_dtype=torch.float16,
|
| 11 |
variant="fp16",
|
| 12 |
use_safetensors=True
|
| 13 |
+
)
|
| 14 |
+
print("Model partially loaded.")
|
| 15 |
|
| 16 |
+
# ### CHANGE HERE ###
|
| 17 |
+
# This is the magic line that enables memory-efficient offloading.
|
| 18 |
+
pipe.enable_model_cpu_offload()
|
| 19 |
+
print("Model loaded with CPU offloading.")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# 3. Define the image generation function (This part remains the same)
|
| 23 |
def generate_image(prompt, negative_prompt, steps, guidance):
|
| 24 |
print(f"Generating image for prompt: {prompt}")
|
| 25 |
# The 'pipe' object does all the work. We pass it the prompt and other parameters.
|
|
|
|
| 35 |
# The output is a PIL Image object, which Gradio can display directly.
|
| 36 |
return image
|
| 37 |
|
| 38 |
+
# 4. Create the Gradio interface (This part remains the same)
|
|
|
|
| 39 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="sky")) as demo:
|
| 40 |
gr.Markdown("# 🖼️ Stable Diffusion XL Text-to-Image")
|
| 41 |
gr.Markdown("Enter a text prompt and see the magic of AI-powered image generation!")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column(scale=4):
|
|
|
|
| 45 |
prompt_input = gr.Textbox(label="Your Prompt", placeholder="An astronaut riding a horse on Mars, cinematic, 4k")
|
|
|
|
| 46 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="low quality, blurry, watermark, text")
|
|
|
|
| 47 |
submit_btn = gr.Button("Generate Image", variant="primary")
|
| 48 |
|
| 49 |
with gr.Column(scale=1):
|
|
|
|
| 50 |
steps_slider = gr.Slider(minimum=10, maximum=100, value=25, step=1, label="Inference Steps")
|
| 51 |
guidance_slider = gr.Slider(minimum=0, maximum=20, value=7.5, step=0.1, label="Guidance Scale")
|
| 52 |
|
|
|
|
| 53 |
output_image = gr.Image(label="Generated Image", width=768, height=768)
|
| 54 |
|
|
|
|
| 55 |
gr.Examples(
|
| 56 |
examples=[
|
| 57 |
["A majestic lion wearing a crown, photorealistic", "cartoon, drawing", 30, 8],
|
|
|
|
| 61 |
inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider]
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# 5. Connect the button to the function (This part remains the same)
|
|
|
|
|
|
|
| 65 |
submit_btn.click(
|
| 66 |
fn=generate_image,
|
| 67 |
inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider],
|