Vedika-advanced-AI commited on
Commit
856fe24
·
verified ·
1 Parent(s): 6c42efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -3,20 +3,24 @@ import cv2
3
  import numpy as np
4
  from PIL import Image
5
  from controlnet_aux import CannyDetector
6
- from diffusers import DiffusionPipeline, LCMScheduler, StableDiffusionXLControlNetPipeline, ControlNetModel
 
 
 
 
7
 
8
- # Canny Detector लोड करें (इमेजे ढंचे के लिए)
9
- canny_detector = CannyDetector.from_pretrained("lllyasviel/Annotators")
10
 
11
- # ControlNet मॉडल लोड करें (SSD-1B के लिए छोटा ControlNet ज़रूरी है)
12
- controlnet_id = "controlnet-xl-fp16/controlnet-canny-xl-fp16" # छोटा और तेज़ वर्शन
13
  controlnet = ControlNetModel.from_pretrained(
14
- controlnet_id,
15
  torch_dtype=torch.float32,
16
  use_safetensors=True
17
  )
18
 
19
- # SSD-1B के साथ ControlNet पाइपलाइन लोड करें
20
  model_id = "segmind/SSD-1B"
21
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
22
  model_id,
@@ -25,32 +29,31 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
25
  use_safetensors=True
26
  )
27
 
28
- # LCM-LoRA लोड करें (तेज़ स्पीड के लिए)
29
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
30
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
31
 
32
- # पाइपलाइन को CPU पर रखें
33
  pipe.to("cpu")
34
 
35
  def edit_image(original_image_path, prompt):
36
- # मूल इमेज लोड करें और Canny मै तैयार करें
37
  image = Image.open(original_image_path).convert("RGB")
38
- control_image = canny_detector(image) # इमेज का ढांचा तैयार है
39
 
40
- # इमेज एडिट कर
41
- # inference steps को 4-8 रखें
 
 
42
  image_edited = pipe(
43
  prompt=prompt,
44
  image=control_image,
45
- controlnet_conditioning_scale=0.6, # कंट्रोलनेट का असर (0.0 से 1.0)
46
  num_inference_steps=5,
47
  guidance_scale=1.5
48
  ).images[0]
49
 
50
  image_edited.save("edited_output.png")
51
- print("इमेज एडिट हो गई: edited_output.png")
52
 
53
- # टेस्टिंग के लिए
54
- # input_image = "image_0.png" # पहाड़ों वाली इमेज
55
- # new_prompt = "Add detailed blooming cherry blossom trees with pink flowers to the mountain foreground, warm sunrise light, realistic photo"
56
- # edit_image(input_image, new_prompt)
 
3
  import numpy as np
4
  from PIL import Image
5
  from controlnet_aux import CannyDetector
6
+ from diffusers import (
7
+ StableDiffusionXLControlNetPipeline,
8
+ ControlNetModel,
9
+ LCMScheduler
10
+ )
11
 
12
+ # 1. Canny Detector इनिशियाइज़ करें (सही तरीका)
13
+ canny_detector = CannyDetector()
14
 
15
+ # 2. ControlNet मॉडल लोड करें
16
+ # SSD-1B के लिए Canny ControlNet का उपयोग
17
  controlnet = ControlNetModel.from_pretrained(
18
+ "diffusers/controlnet-canny-sdxl-1.0",
19
  torch_dtype=torch.float32,
20
  use_safetensors=True
21
  )
22
 
23
+ # 3. SSD-1B के साथ पाइपलाइन लोड करें
24
  model_id = "segmind/SSD-1B"
25
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
26
  model_id,
 
29
  use_safetensors=True
30
  )
31
 
32
+ # 4. LCM-LoRA लोड करें (तेज़ स्पीड के लिए)
33
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
34
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
35
 
36
+ # 5. CPU पर रखें
37
  pipe.to("cpu")
38
 
39
  def edit_image(original_image_path, prompt):
40
+ # इमेज को लोड और पोसेस करें
41
  image = Image.open(original_image_path).convert("RGB")
 
42
 
43
+ # Cannyडिटेक्शन
44
+ control_image = canny_detector(image)
45
+
46
+ # इमेज जनरेशन (LCM के कारण केवल 4-5 स्टेप्स काफी हैं)
47
  image_edited = pipe(
48
  prompt=prompt,
49
  image=control_image,
50
+ controlnet_conditioning_scale=0.5,
51
  num_inference_steps=5,
52
  guidance_scale=1.5
53
  ).images[0]
54
 
55
  image_edited.save("edited_output.png")
56
+ print("इमेज एडिट होकर सेव हो गई है: edited_output.png")
57
 
58
+ # उपयोग करनका उदाहरण:
59
+ # edit_image("image_0.png", "A beautiful landscape with cherry blossom trees, realistic, high quality")