Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +74 -0
- examples/example_inputs.txt +0 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionXLPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model (cache locally first)
|
| 6 |
+
def load_model():
|
| 7 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
variant="fp16",
|
| 12 |
+
use_safetensors=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
if torch.cuda.is_available():
|
| 16 |
+
pipe.to("cuda")
|
| 17 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 18 |
+
elif torch.backends.mps.is_available():
|
| 19 |
+
pipe.to("mps")
|
| 20 |
+
else:
|
| 21 |
+
pipe.to("cpu")
|
| 22 |
+
|
| 23 |
+
return pipe
|
| 24 |
+
|
| 25 |
+
# Image generation function
|
| 26 |
+
def generate_image(prompt, negative_prompt, steps, guidance_scale, width=1024, height=1024):
|
| 27 |
+
pipe = load_model()
|
| 28 |
+
|
| 29 |
+
# Generate image
|
| 30 |
+
image = pipe(
|
| 31 |
+
prompt=prompt,
|
| 32 |
+
negative_prompt=negative_prompt,
|
| 33 |
+
num_inference_steps=int(steps),
|
| 34 |
+
guidance_scale=guidance_scale,
|
| 35 |
+
width=width,
|
| 36 |
+
height=height,
|
| 37 |
+
).images[0]
|
| 38 |
+
|
| 39 |
+
return image
|
| 40 |
+
|
| 41 |
+
# Gradio interface
|
| 42 |
+
with gr.Blocks(title="Text to Image Generator") as demo:
|
| 43 |
+
gr.Markdown("# 🎨 Text-to-Image Generator with SDXL")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
prompt = gr.Textbox(label="Prompt", placeholder="A futuristic cityscape at sunset...")
|
| 48 |
+
negative_prompt = gr.Textbox(
|
| 49 |
+
label="Negative Prompt",
|
| 50 |
+
value="low quality, blurry, distorted, watermark",
|
| 51 |
+
)
|
| 52 |
+
steps = gr.Slider(10, 100, value=30, label="Inference Steps")
|
| 53 |
+
guidance_scale = gr.Slider(1.0, 20.0, value=7.5, label="Guidance Scale")
|
| 54 |
+
submit = gr.Button("Generate Image")
|
| 55 |
+
|
| 56 |
+
with gr.Column():
|
| 57 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 58 |
+
|
| 59 |
+
submit.click(
|
| 60 |
+
generate_image,
|
| 61 |
+
inputs=[prompt, negative_prompt, steps, guidance_scale],
|
| 62 |
+
outputs=output_image,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
gr.Examples(
|
| 66 |
+
examples=[
|
| 67 |
+
["A cyberpunk cat wearing neon sunglasses, digital art", "", 30, 7.5],
|
| 68 |
+
["A realistic photo of a dinosaur working in a modern office", "cartoonish, drawing", 40, 8.5],
|
| 69 |
+
],
|
| 70 |
+
inputs=[prompt, negative_prompt, steps, guidance_scale],
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo.launch(debug=True)
|
examples/example_inputs.txt
ADDED
|
File without changes
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
diffusers>=0.20.0
|
| 4 |
+
accelerate>=0.21.0
|
| 5 |
+
gradio>=3.40.0
|
| 6 |
+
xformers==0.0.22
|