Update app.py
Browse files
app.py
CHANGED
|
@@ -20,18 +20,48 @@ MODEL = "stability-ai/stable-diffusion-img2img:15a3689ee13b0d2616e98820eca31d4c3
|
|
| 20 |
|
| 21 |
|
| 22 |
def generate(prompt, strength, guidance, steps, seed, count):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
r = requests.get(url, timeout=120)
|
| 26 |
r.raise_for_status()
|
| 27 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
token = os.getenv("REPLICATE_API_TOKEN")
|
| 30 |
if not token:
|
| 31 |
note = "❌ Ajoute REPLICATE_API_TOKEN (Settings → Secrets)."
|
| 32 |
return [Image.new("RGB", (768, 768), (30, 30, 30))], gr.update(value=note)
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
test = requests.get(BASE_IMG_URL, timeout=30)
|
| 37 |
if test.status_code != 200:
|
|
@@ -43,42 +73,61 @@ def generate(prompt, strength, guidance, steps, seed, count):
|
|
| 43 |
|
| 44 |
client = replicate.Client(api_token=token)
|
| 45 |
|
| 46 |
-
# Seed
|
| 47 |
try:
|
| 48 |
s = int(seed)
|
| 49 |
except Exception:
|
| 50 |
s = 0
|
| 51 |
use_seed_base = s if s != 0 else random.randint(1, 2_000_000_000)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
for i in range(int(count)):
|
| 57 |
-
use_seed = use_seed_base + i
|
| 58 |
-
|
| 59 |
out = client.run(
|
| 60 |
MODEL,
|
| 61 |
input={
|
| 62 |
"prompt": str(prompt),
|
| 63 |
-
"image": BASE_IMG_URL,
|
| 64 |
-
"prompt_strength": float(strength),
|
| 65 |
-
"num_inference_steps": int(steps),
|
| 66 |
-
"guidance_scale": float(guidance),
|
| 67 |
-
"seed": int(use_seed),
|
| 68 |
"width": 768,
|
| 69 |
"height": 768,
|
| 70 |
},
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
if isinstance(out, list) and out
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
elif isinstance(out, str):
|
| 77 |
-
|
| 78 |
else:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
img = _fetch_image(url)
|
| 82 |
imgs.append(img)
|
| 83 |
|
| 84 |
except replicate.exceptions.ReplicateError as e:
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def generate(prompt, strength, guidance, steps, seed, count):
|
| 23 |
+
import os, random, traceback
|
| 24 |
+
from io import BytesIO
|
| 25 |
+
from PIL import Image
|
| 26 |
+
import requests, replicate
|
| 27 |
+
import gradio as gr
|
| 28 |
+
|
| 29 |
+
# Helpers
|
| 30 |
+
def _fetch_image_from_url(url: str) -> Image.Image:
|
| 31 |
r = requests.get(url, timeout=120)
|
| 32 |
r.raise_for_status()
|
| 33 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 34 |
|
| 35 |
+
def _fileoutput_to_image(file_obj) -> Image.Image:
|
| 36 |
+
"""
|
| 37 |
+
Convertit un replicate.helpers.FileOutput en PIL.Image.
|
| 38 |
+
Tente .read() (bytes), puis .open(), puis .path.
|
| 39 |
+
"""
|
| 40 |
+
# try read()
|
| 41 |
+
if hasattr(file_obj, "read"):
|
| 42 |
+
data = file_obj.read()
|
| 43 |
+
return Image.open(BytesIO(data)).convert("RGB")
|
| 44 |
+
# try open()
|
| 45 |
+
if hasattr(file_obj, "open"):
|
| 46 |
+
with file_obj.open() as f:
|
| 47 |
+
data = f.read()
|
| 48 |
+
return Image.open(BytesIO(data)).convert("RGB")
|
| 49 |
+
# try path
|
| 50 |
+
if hasattr(file_obj, "path"):
|
| 51 |
+
with open(file_obj.path, "rb") as f:
|
| 52 |
+
data = f.read()
|
| 53 |
+
return Image.open(BytesIO(data)).convert("RGB")
|
| 54 |
+
# fallback: stringification (rare)
|
| 55 |
+
raise RuntimeError(f"Type de sortie Replicate non géré: {type(file_obj)}")
|
| 56 |
+
|
| 57 |
token = os.getenv("REPLICATE_API_TOKEN")
|
| 58 |
if not token:
|
| 59 |
note = "❌ Ajoute REPLICATE_API_TOKEN (Settings → Secrets)."
|
| 60 |
return [Image.new("RGB", (768, 768), (30, 30, 30))], gr.update(value=note)
|
| 61 |
|
| 62 |
+
SPACE_ID = os.getenv("SPACE_ID", "")
|
| 63 |
+
BASE_IMG_URL = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/pinky-monster.png"
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
test = requests.get(BASE_IMG_URL, timeout=30)
|
| 67 |
if test.status_code != 200:
|
|
|
|
| 73 |
|
| 74 |
client = replicate.Client(api_token=token)
|
| 75 |
|
| 76 |
+
# Seed sûr = toujours un entier
|
| 77 |
try:
|
| 78 |
s = int(seed)
|
| 79 |
except Exception:
|
| 80 |
s = 0
|
| 81 |
use_seed_base = s if s != 0 else random.randint(1, 2_000_000_000)
|
| 82 |
|
| 83 |
+
# ⚠️ Mets bien ton ref complet de modèle avec :hash ici
|
| 84 |
+
MODEL = "stability-ai/stable-diffusion-img2img:15a3689ee13b0d2616e98820eca31d4c3abcd36672df6afce5cb6feb1d66087d"
|
| 85 |
+
|
| 86 |
+
imgs, note = [], "✅ Génération OK"
|
| 87 |
try:
|
| 88 |
for i in range(int(count)):
|
| 89 |
+
use_seed = use_seed_base + i
|
|
|
|
| 90 |
out = client.run(
|
| 91 |
MODEL,
|
| 92 |
input={
|
| 93 |
"prompt": str(prompt),
|
| 94 |
+
"image": BASE_IMG_URL, # URL publique de ton Space
|
| 95 |
+
"prompt_strength": float(strength), # 0.1–0.9
|
| 96 |
+
"num_inference_steps": int(steps), # 10–50
|
| 97 |
+
"guidance_scale": float(guidance), # 1–15
|
| 98 |
+
"seed": int(use_seed), # jamais None
|
| 99 |
"width": 768,
|
| 100 |
"height": 768,
|
| 101 |
},
|
| 102 |
+
# IMPORTANT : certains modèles renvoient des FileOutput.
|
| 103 |
+
# On peut aussi forcer un type de sortie avec use_file_output=True/False,
|
| 104 |
+
# mais on gère les deux cas ci-dessous.
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Normalement: list[str] (URLs) OU list[FileOutput]
|
| 108 |
+
if isinstance(out, list) and out:
|
| 109 |
+
first = out[0]
|
| 110 |
+
if isinstance(first, str):
|
| 111 |
+
img = _fetch_image_from_url(first)
|
| 112 |
+
else:
|
| 113 |
+
# ex: replicate.helpers.FileOutput
|
| 114 |
+
img = _fileoutput_to_image(first)
|
| 115 |
elif isinstance(out, str):
|
| 116 |
+
img = _fetch_image_from_url(out)
|
| 117 |
else:
|
| 118 |
+
# Dernier filet de sécurité: certaines implémentations renvoient un dict
|
| 119 |
+
# avec une clé 'output' ou 'image'. On tente.
|
| 120 |
+
if isinstance(out, dict):
|
| 121 |
+
possible = out.get("output") or out.get("image") or out.get("url")
|
| 122 |
+
if isinstance(possible, str):
|
| 123 |
+
img = _fetch_image_from_url(possible)
|
| 124 |
+
elif hasattr(possible, "read") or hasattr(possible, "open") or hasattr(possible, "path"):
|
| 125 |
+
img = _fileoutput_to_image(possible)
|
| 126 |
+
else:
|
| 127 |
+
raise RuntimeError(f"Sortie Replicate inattendue: {out}")
|
| 128 |
+
else:
|
| 129 |
+
raise RuntimeError(f"Sortie Replicate inattendue: {out}")
|
| 130 |
|
|
|
|
| 131 |
imgs.append(img)
|
| 132 |
|
| 133 |
except replicate.exceptions.ReplicateError as e:
|