aanchal77 commited on
Commit
f253fa6
·
verified ·
1 Parent(s): fa0cb45

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +54 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
+
5
+ # Detect device
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+ dtype = torch.float16 if device == "cuda" else torch.float32
8
+
9
+ # Load base model
10
+ pipe = StableDiffusionPipeline.from_pretrained(
11
+ "runwayml/stable-diffusion-v1-5",
12
+ torch_dtype=dtype
13
+ )
14
+
15
+ # Load LoRA weights
16
+ pipe.load_lora_weights("./lora")
17
+
18
+ # Move to device
19
+ pipe = pipe.to(device)
20
+
21
+ # Extra CPU optimizations
22
+ if device == "cpu":
23
+ pipe.enable_attention_slicing()
24
+ pipe.enable_sequential_cpu_offload()
25
+ pipe.enable_vae_tiling()
26
+
27
+ # Define image generation function with quality toggle
28
+ def generate(prompt, quality):
29
+ if quality == "Fast":
30
+ steps = 20
31
+ guidance_scale = 7.0
32
+ else: # High Quality
33
+ steps = 40
34
+ guidance_scale = 8.5
35
+
36
+ with torch.no_grad():
37
+ image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0]
38
+ return image
39
+
40
+ # Build Gradio UI
41
+ demo = gr.Interface(
42
+ fn=generate,
43
+ inputs=[
44
+ gr.Textbox(label="Enter your prompt"),
45
+ gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Mode")
46
+ ],
47
+ outputs=gr.Image(label="Generated Image"),
48
+ title="Fine-tuned Stable Diffusion with LoRA (CPU-Optimized)",
49
+ description="Choose 'Fast' for quicker generation or 'High Quality' for better details."
50
+ )
51
+
52
+ # Launch app
53
+ if __name__ == "__main__":
54
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ diffusers>=0.21.0
4
+ gradio
5
+ peft
6
+ accelerate
7
+ safetensors