ritwikraha commited on
Commit
31772c8
1 Parent(s): 09d39fb

chore: creating app

Browse files
Files changed (1) hide show
  1. app.py +38 -41
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 gradio import Interface
 
6
 
7
- # Load models (outside of the app function for efficiency)
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("ritwikraha/khabib_sketch_LoRA") # Assuming correct model ID
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
- @spaces.GPU
22
- def inference(prompt, negative_prompt=None, guidance_scale=3, num_inference_steps=50):
23
- """Generates an image using the Stable Diffusion XL model with LoRA weights.
 
24
 
25
  Args:
26
- prompt (str): Prompt for image generation, entered by the user.
27
- negative_prompt (str, optional): Negative prompt to guide model away from unwanted features. Defaults to "ugly face, multiple bodies, bad anatomy, disfigured, extra fingers".
28
- guidance_scale (float, optional): Controls the strength of the guidance from the prompt. Defaults to 3.
29
- num_inference_steps (int, optional): Number of inference steps for image generation. Defaults to 50.
30
 
31
  Returns:
32
- PIL.Image: Generated 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
- ).images[0]
41
-
42
- return image.convert("RGB") # Ensure RGB format for compatibility
43
-
44
-
45
-
46
- # Create the Gradio interface
47
- interface = Interface(
48
- fn=inference,
49
- inputs=[
50
- "text", # Prompt from user
51
- "text", # Optional negative prompt
52
- {"type": "slider", "min": 1, "max": 10, "default": 3},
53
- {"type": "slider", "min": 10, "max": 100, "default": 50},
54
- ],
55
- outputs="image",
56
- title="Stable Diffusion XL with Khabib LoRA",
57
- description="Generate sketches using the Stable Diffusion XL model fine-tuned on Khabib Nurmagomedov sketches.",
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()