Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import AutoPipelineForText2Image
|
| 4 |
+
|
| 5 |
+
# List of available models
|
| 6 |
+
MODEL_OPTIONS = {
|
| 7 |
+
"Stable Diffusion 1.5": "runwayml/stable-diffusion-v1-5",
|
| 8 |
+
"Stable Diffusion 2.1": "stabilityai/stable-diffusion-2-1",
|
| 9 |
+
"Stable Diffusion XL": "stabilityai/stable-diffusion-xl-base-1.0"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def generate_image(
|
| 13 |
+
model_choice,
|
| 14 |
+
lora_url,
|
| 15 |
+
prompt,
|
| 16 |
+
negative_prompt,
|
| 17 |
+
steps,
|
| 18 |
+
width,
|
| 19 |
+
height,
|
| 20 |
+
guidance_scale,
|
| 21 |
+
seed
|
| 22 |
+
):
|
| 23 |
+
# Get the selected model ID
|
| 24 |
+
model_id = MODEL_OPTIONS[model_choice]
|
| 25 |
+
|
| 26 |
+
# Initialize the pipeline
|
| 27 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 28 |
+
model_id,
|
| 29 |
+
torch_dtype=torch.float16,
|
| 30 |
+
use_safetensors=True
|
| 31 |
+
).to("cuda")
|
| 32 |
+
|
| 33 |
+
# Load LoRA weights if provided
|
| 34 |
+
if lora_url:
|
| 35 |
+
pipe.load_lora_weights(lora_url)
|
| 36 |
+
pipe.fuse_lora()
|
| 37 |
+
|
| 38 |
+
# Set up generator with seed if provided
|
| 39 |
+
generator = None
|
| 40 |
+
if seed:
|
| 41 |
+
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
| 42 |
+
|
| 43 |
+
# Generate image
|
| 44 |
+
image = pipe(
|
| 45 |
+
prompt=prompt,
|
| 46 |
+
negative_prompt=negative_prompt,
|
| 47 |
+
num_inference_steps=int(steps),
|
| 48 |
+
width=int(width),
|
| 49 |
+
height=int(height),
|
| 50 |
+
guidance_scale=guidance_scale,
|
| 51 |
+
generator=generator
|
| 52 |
+
).images[0]
|
| 53 |
+
|
| 54 |
+
return image
|
| 55 |
+
|
| 56 |
+
# Gradio UI components
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("## 🎨 Text-to-Image Generation with LoRA")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
model_choice = gr.Dropdown(
|
| 63 |
+
label="Select Base Model",
|
| 64 |
+
choices=list(MODEL_OPTIONS.keys()),
|
| 65 |
+
value="Stable Diffusion XL"
|
| 66 |
+
)
|
| 67 |
+
lora_url = gr.Textbox(
|
| 68 |
+
label="LoRA Repository ID (e.g., 'username/lora-name')",
|
| 69 |
+
placeholder="Optional Hugging Face repository ID"
|
| 70 |
+
)
|
| 71 |
+
prompt = gr.Textbox(
|
| 72 |
+
label="Prompt",
|
| 73 |
+
placeholder="Enter your prompt here..."
|
| 74 |
+
)
|
| 75 |
+
negative_prompt = gr.Textbox(
|
| 76 |
+
label="Negative Prompt",
|
| 77 |
+
placeholder="Enter what to exclude from the image..."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
steps = gr.Number(
|
| 82 |
+
label="Inference Steps",
|
| 83 |
+
value=25,
|
| 84 |
+
precision=0
|
| 85 |
+
)
|
| 86 |
+
guidance_scale = gr.Slider(
|
| 87 |
+
label="Guidance Scale",
|
| 88 |
+
minimum=1.0,
|
| 89 |
+
maximum=20.0,
|
| 90 |
+
value=7.5
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
width = gr.Number(
|
| 95 |
+
label="Width",
|
| 96 |
+
value=1024,
|
| 97 |
+
precision=0
|
| 98 |
+
)
|
| 99 |
+
height = gr.Number(
|
| 100 |
+
label="Height",
|
| 101 |
+
value=1024,
|
| 102 |
+
precision=0
|
| 103 |
+
)
|
| 104 |
+
seed = gr.Number(
|
| 105 |
+
label="Seed (optional)",
|
| 106 |
+
precision=0
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 110 |
+
|
| 111 |
+
with gr.Column():
|
| 112 |
+
output_image = gr.Image(label="Generated Image", height=600)
|
| 113 |
+
|
| 114 |
+
generate_btn.click(
|
| 115 |
+
fn=generate_image,
|
| 116 |
+
inputs=[
|
| 117 |
+
model_choice,
|
| 118 |
+
lora_url,
|
| 119 |
+
prompt,
|
| 120 |
+
negative_prompt,
|
| 121 |
+
steps,
|
| 122 |
+
width,
|
| 123 |
+
height,
|
| 124 |
+
guidance_scale,
|
| 125 |
+
seed
|
| 126 |
+
],
|
| 127 |
+
outputs=output_image
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
demo.launch()
|