Christian
commited on
Commit
·
7e6bfa5
1
Parent(s):
012e4d2
Aggiunto nuovo appv3
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch
|
|
| 3 |
from PIL import Image
|
| 4 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
-
from safetensors
|
| 7 |
|
| 8 |
# Scarica il modello .safetensors dalla tua model card
|
| 9 |
model_path = hf_hub_download(
|
|
@@ -11,17 +11,49 @@ model_path = hf_hub_download(
|
|
| 11 |
filename="juggernaut_reborn.safetensors"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Carica il modello Stable Diffusion
|
| 15 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 16 |
"runwayml/stable-diffusion-v1-5", # Modello base compatibile
|
| 17 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 18 |
use_safetensors=True,
|
| 19 |
-
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
|
| 27 |
def enhance_image(image, prompt, negative_prompt, cfg_scale, denoising_strength):
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
+
from safetensors import safe_open
|
| 7 |
|
| 8 |
# Scarica il modello .safetensors dalla tua model card
|
| 9 |
model_path = hf_hub_download(
|
|
|
|
| 11 |
filename="juggernaut_reborn.safetensors"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Carica il modello Stable Diffusion
|
| 15 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 16 |
"runwayml/stable-diffusion-v1-5", # Modello base compatibile
|
| 17 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 18 |
use_safetensors=True,
|
| 19 |
+
safety_checker=None
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Determina se è un LoRA o un modello completo
|
| 23 |
+
try:
|
| 24 |
+
# Opzione 1: Carica come LoRA
|
| 25 |
+
pipe.load_lora_weights(model_path)
|
| 26 |
+
except Exception as lora_error:
|
| 27 |
+
try:
|
| 28 |
+
# Opzione 2: Carica direttamente nell'UNet
|
| 29 |
+
with safe_open(model_path, framework="pt", device="cpu") as f:
|
| 30 |
+
unet_params = {k: f.get_tensor(k) for k in f.keys() if k.startswith("unet.")}
|
| 31 |
+
|
| 32 |
+
# Rimuovi il prefisso "unet." dalle chiavi se presente
|
| 33 |
+
clean_unet_params = {}
|
| 34 |
+
for k, v in unet_params.items():
|
| 35 |
+
if k.startswith("unet."):
|
| 36 |
+
clean_unet_params[k[5:]] = v # Rimuovi il prefisso "unet."
|
| 37 |
+
else:
|
| 38 |
+
clean_unet_params[k] = v
|
| 39 |
+
|
| 40 |
+
# Carica i pesi nell'UNet
|
| 41 |
+
if clean_unet_params:
|
| 42 |
+
pipe.unet.load_state_dict(clean_unet_params, strict=False)
|
| 43 |
+
else:
|
| 44 |
+
# Opzione 3: Prova a caricare come checkpoint completo
|
| 45 |
+
pipe = StableDiffusionImg2ImgPipeline.from_single_file(
|
| 46 |
+
model_path,
|
| 47 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 48 |
+
use_safetensors=True,
|
| 49 |
+
load_safety_checker=False
|
| 50 |
+
)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Errore nel caricamento del modello: {e}")
|
| 53 |
+
# Fallback: usa il modello base
|
| 54 |
+
print("Utilizzo del modello base come fallback")
|
| 55 |
+
|
| 56 |
+
# Sposta il modello su GPU se disponibile
|
| 57 |
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 58 |
|
| 59 |
def enhance_image(image, prompt, negative_prompt, cfg_scale, denoising_strength):
|