Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
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,
|
| 23 |
+
controlnet=controlnet,
|
| 24 |
+
torch_dtype=torch.float32,
|
| 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)
|