Spaces:
Runtime error
Runtime error
ritwikraha
commited on
Commit
•
31772c8
1
Parent(s):
09d39fb
chore: creating app
Browse files
app.py
CHANGED
@@ -1,61 +1,58 @@
|
|
|
|
1 |
import torch
|
2 |
-
import spaces
|
3 |
-
from PIL import Image
|
4 |
from diffusers import DiffusionPipeline, AutoencoderKL
|
5 |
-
from
|
|
|
6 |
|
7 |
-
#
|
8 |
-
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix")
|
9 |
pipe = DiffusionPipeline.from_pretrained(
|
10 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
11 |
vae=vae,
|
12 |
torch_dtype=torch.float16,
|
13 |
variant="fp16",
|
|
|
14 |
)
|
15 |
-
pipe.load_lora_weights(
|
16 |
-
|
17 |
-
# Move models to CUDA if available (outside of the app function for efficiency)
|
18 |
if torch.cuda.is_available():
|
19 |
-
pipe.to("cuda")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
Args:
|
26 |
-
prompt (str):
|
27 |
-
negative_prompt (str, optional): Negative prompt to
|
28 |
-
guidance_scale (
|
29 |
-
num_inference_steps (int, optional):
|
30 |
|
31 |
Returns:
|
32 |
-
PIL.Image:
|
33 |
"""
|
34 |
-
|
35 |
-
image = pipe(
|
36 |
prompt=prompt,
|
37 |
negative_prompt=negative_prompt,
|
38 |
guidance_scale=guidance_scale,
|
39 |
num_inference_steps=num_inference_steps,
|
40 |
-
)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Launch the Space
|
61 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
import torch
|
|
|
|
|
3 |
from diffusers import DiffusionPipeline, AutoencoderKL
|
4 |
+
from PIL import Image
|
5 |
+
import spaces # Assuming you're still working within a framework that uses @spaces
|
6 |
|
7 |
+
# Initialize the VAE model and Diffusion Pipeline outside the GPU-enabled function for efficiency
|
8 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
9 |
pipe = DiffusionPipeline.from_pretrained(
|
10 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
11 |
vae=vae,
|
12 |
torch_dtype=torch.float16,
|
13 |
variant="fp16",
|
14 |
+
use_safetensors=True
|
15 |
)
|
16 |
+
pipe.load_lora_weights('ritwikraha/khabib_sketch_LoRA')
|
|
|
|
|
17 |
if torch.cuda.is_available():
|
18 |
+
_ = pipe.to("cuda")
|
19 |
|
20 |
+
# Define the image generation function
|
21 |
+
@spaces.GPU(enable_queue=True)
|
22 |
+
def generate_sketch(prompt, negative_prompt="ugly face, multiple bodies, bad anatomy, disfigured, extra fingers", guidance_scale=3, num_inference_steps=50):
|
23 |
+
"""Generate a sketch image based on a prompt using Stable Diffusion XL with LoRA weights.
|
24 |
|
25 |
Args:
|
26 |
+
prompt (str): Description of the image to generate.
|
27 |
+
negative_prompt (str, optional): Negative prompt to avoid certain features. Defaults to common undesirables.
|
28 |
+
guidance_scale (int, optional): The strength of the guidance. Defaults to 3.
|
29 |
+
num_inference_steps (int, optional): The number of steps for the diffusion process. Defaults to 50.
|
30 |
|
31 |
Returns:
|
32 |
+
PIL.Image: The generated sketch image.
|
33 |
"""
|
34 |
+
result = pipe(
|
|
|
35 |
prompt=prompt,
|
36 |
negative_prompt=negative_prompt,
|
37 |
guidance_scale=guidance_scale,
|
38 |
num_inference_steps=num_inference_steps,
|
39 |
+
)
|
40 |
+
return result.images[0].convert("RGB") # Ensure the image is in RGB format
|
41 |
+
|
42 |
+
# Setup Gradio interface
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
with gr.Column():
|
45 |
+
prompt_input = gr.Textbox(label="Enter your image prompt", value="a sketch of TOK khabib pointing at another khabib like the spiderman meme, monchrome, pen sketch")
|
46 |
+
negative_prompt_input = gr.Textbox(label="Enter negative prompt", value="ugly face, multiple bodies, bad anatomy, disfigured, extra fingers", lines=2)
|
47 |
+
guidance_scale_slider = gr.Slider(label="Guidance Scale", min_value=1, max_value=5, value=3)
|
48 |
+
steps_slider = gr.Slider(label="Number of Inference Steps", min_value=20, max_value=100, value=50)
|
49 |
+
submit_button = gr.Button("Generate Sketch")
|
50 |
+
output_image = gr.Image(label="Generated Sketch")
|
51 |
+
|
52 |
+
submit_button.click(
|
53 |
+
fn=generate_sketch,
|
54 |
+
inputs=[prompt_input, negative_prompt_input, guidance_scale_slider, steps_slider],
|
55 |
+
outputs=output_image
|
56 |
+
)
|
57 |
+
|
58 |
+
demo.launch()
|
|
|
|