Spaces:
Runtime error
Runtime error
ritwikraha
commited on
Commit
•
4a2914e
1
Parent(s):
34ef960
chore: creating app
Browse files
app.py
CHANGED
@@ -1,4 +1,60 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from diffusers import DiffusionPipeline, AutoencoderKL
|
4 |
+
from gradio import Interface
|
5 |
|
6 |
+
# Load models (outside of the app function for efficiency)
|
7 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix")
|
8 |
+
pipe = DiffusionPipeline.from_pretrained(
|
9 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
10 |
+
vae=vae,
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
variant="fp16",
|
13 |
+
)
|
14 |
+
pipe.load_lora_weights("ritwikraha/khabib_sketch_LoRA") # Assuming correct model ID
|
15 |
+
|
16 |
+
# Move models to CUDA if available (outside of the app function for efficiency)
|
17 |
+
if torch.cuda.is_available():
|
18 |
+
pipe.to("cuda")
|
19 |
+
|
20 |
+
@spaces.GPU
|
21 |
+
def inference(prompt, negative_prompt=None, guidance_scale=3, num_inference_steps=50):
|
22 |
+
"""Generates an image using the Stable Diffusion XL model with LoRA weights.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
prompt (str): Prompt for image generation, entered by the user.
|
26 |
+
negative_prompt (str, optional): Negative prompt to guide model away from unwanted features. Defaults to "ugly face, multiple bodies, bad anatomy, disfigured, extra fingers".
|
27 |
+
guidance_scale (float, optional): Controls the strength of the guidance from the prompt. Defaults to 3.
|
28 |
+
num_inference_steps (int, optional): Number of inference steps for image generation. Defaults to 50.
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
PIL.Image: Generated image.
|
32 |
+
"""
|
33 |
+
|
34 |
+
image = pipe(
|
35 |
+
prompt=prompt,
|
36 |
+
negative_prompt=negative_prompt,
|
37 |
+
guidance_scale=guidance_scale,
|
38 |
+
num_inference_steps=num_inference_steps,
|
39 |
+
).images[0]
|
40 |
+
|
41 |
+
return image.convert("RGB") # Ensure RGB format for compatibility
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
# Create the Gradio interface
|
46 |
+
interface = Interface(
|
47 |
+
fn=inference,
|
48 |
+
inputs=[
|
49 |
+
"text", # Prompt from user
|
50 |
+
"text", # Optional negative prompt
|
51 |
+
{"type": "slider", "min": 1, "max": 10, "default": 3},
|
52 |
+
{"type": "slider", "min": 10, "max": 100, "default": 50},
|
53 |
+
],
|
54 |
+
outputs="image",
|
55 |
+
title="Stable Diffusion XL with Khabib LoRA",
|
56 |
+
description="Generate sketches using the Stable Diffusion XL model fine-tuned on Khabib Nurmagomedov sketches.",
|
57 |
+
)
|
58 |
+
|
59 |
+
# Launch the Space
|
60 |
+
interface.launch()
|