Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionPipeline, UNet2DConditionModel
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
model_id = "UX4567/Stable-Diffusion-v1-3.0"
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
print("Model load ho raha hai...")
|
| 10 |
+
# 1. Load your fine-tuned UNet in float16
|
| 11 |
+
trained_unet = UNet2DConditionModel.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
torch_dtype=torch.float16
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 2. Initialize pipeline with runwayml base and your custom UNet
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
"runwayml/stable-diffusion-v1-5",
|
| 19 |
+
unet=trained_unet,
|
| 20 |
+
torch_dtype=torch.float16
|
| 21 |
+
).to(device)
|
| 22 |
+
|
| 23 |
+
# 3. Generation function with GPU decorator
|
| 24 |
+
@spaces.gpu
|
| 25 |
+
def generate_image(prompt):
|
| 26 |
+
image = pipe(prompt, num_inference_steps=30).images[0]
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
# 4. Gradio UI setup
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=generate_image,
|
| 32 |
+
inputs=gr.Textbox(label="Enter your prompt", value="a futuristic cyberpunk city at night"),
|
| 33 |
+
outputs=gr.Image(label="Generated Image"),
|
| 34 |
+
title="Calico Image Generator",
|
| 35 |
+
description="Apne fine-tuned Stable Diffusion model se creative images generate karein!"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|
| 40 |
+
|