Spaces:
Running
Running
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Canny Detector ल
|
| 9 |
-
canny_detector = CannyDetector
|
| 10 |
|
| 11 |
-
# ControlNet मॉडल लोड करें
|
| 12 |
-
|
| 13 |
controlnet = ControlNetModel.from_pretrained(
|
| 14 |
-
|
| 15 |
torch_dtype=torch.float32,
|
| 16 |
use_safetensors=True
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# SSD-1B के साथ
|
| 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 |
-
#
|
| 33 |
pipe.to("cpu")
|
| 34 |
|
| 35 |
def edit_image(original_image_path, prompt):
|
| 36 |
-
#
|
| 37 |
image = Image.open(original_image_path).convert("RGB")
|
| 38 |
-
control_image = canny_detector(image) # इमेज का ढांचा तैयार है
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
image_edited = pipe(
|
| 43 |
prompt=prompt,
|
| 44 |
image=control_image,
|
| 45 |
-
controlnet_conditioning_scale=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 |
-
#
|
| 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")
|
|
|
|
|
|