Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,11 @@ from PIL import Image
|
|
| 4 |
import replicate
|
| 5 |
|
| 6 |
# ---- CONFIG ----
|
| 7 |
-
SPACE_ID = os.getenv("SPACE_ID", "") #
|
| 8 |
BASE_IMG_URL = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/pinky-monster.png"
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def _fetch_image(url: str) -> Image.Image:
|
| 12 |
r = requests.get(url, timeout=120)
|
|
@@ -14,69 +16,66 @@ def _fetch_image(url: str) -> Image.Image:
|
|
| 14 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 15 |
|
| 16 |
def generate(prompt, strength, guidance, steps, seed, count):
|
| 17 |
-
# Vérifs usuelles
|
| 18 |
token = os.getenv("REPLICATE_API_TOKEN")
|
| 19 |
if not token:
|
| 20 |
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value="❌ Ajoute REPLICATE_API_TOKEN (Settings → Secrets).")
|
| 21 |
|
| 22 |
-
# Vérifie
|
| 23 |
try:
|
| 24 |
test = requests.get(BASE_IMG_URL, timeout=30)
|
| 25 |
if test.status_code != 200:
|
| 26 |
-
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value=f"❌ Image introuvable : {BASE_IMG_URL}\nUpload ‘pinky-monster.png’
|
| 27 |
except Exception as e:
|
| 28 |
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value=f"❌ Erreur en lisant l'image de base : {e}")
|
| 29 |
|
| 30 |
client = replicate.Client(api_token=token)
|
| 31 |
|
| 32 |
-
imgs = []
|
| 33 |
-
note = "✅ Génération OK"
|
| 34 |
try:
|
| 35 |
-
for
|
| 36 |
use_seed = None if int(seed) == 0 else int(seed)
|
|
|
|
| 37 |
out = client.run(
|
| 38 |
MODEL,
|
| 39 |
input={
|
| 40 |
"prompt": str(prompt),
|
| 41 |
-
"image": BASE_IMG_URL, #
|
| 42 |
"prompt_strength": float(strength),# 0.1–0.9 (bas = plus fidèle)
|
| 43 |
"num_inference_steps": int(steps), # 10–50
|
| 44 |
"guidance_scale": float(guidance), # 1–15
|
| 45 |
"seed": use_seed,
|
| 46 |
-
"width":
|
| 47 |
-
"height":
|
| 48 |
},
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# Replicate renvoie
|
| 52 |
-
if isinstance(out, list) and
|
| 53 |
url = out[0]
|
| 54 |
elif isinstance(out, str):
|
| 55 |
url = out
|
| 56 |
else:
|
| 57 |
raise RuntimeError(f"Sortie inattendue de Replicate: {out}")
|
| 58 |
|
| 59 |
-
# Télécharge l'image et renvoie PIL (plus sûr que lier l’URL)
|
| 60 |
img = _fetch_image(url)
|
| 61 |
imgs.append(img)
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
note = f"❌ Erreur: {e}\n\nTrace:\n{traceback.format_exc()}"
|
| 65 |
-
# On retourne au moins une image “placeholder” avec le message dans la note
|
| 66 |
if not imgs:
|
| 67 |
-
imgs = [Image.new("RGB", (
|
| 68 |
|
| 69 |
return imgs, gr.update(value=note)
|
| 70 |
|
| 71 |
with gr.Blocks(title="PINKY Maker") as demo:
|
| 72 |
gr.Markdown("### PINKY Maker — génère ta version du monstre (IA)\n"
|
| 73 |
"- Mets ta clé **REPLICATE_API_TOKEN** dans Settings → Secrets\n"
|
| 74 |
-
"- Upload **pinky-monster.png** dans Files & versions")
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
prompt = gr.Textbox(label="Décris ta variante",
|
| 78 |
value="monstre bleu avec des cornes dorées et une grosse chaîne")
|
| 79 |
-
strength = gr.Slider(0.1, 0.9, value=0.
|
| 80 |
guidance = gr.Slider(1, 15, value=7.5, step=0.5, label="Guidance (suivre le prompt)")
|
| 81 |
steps = gr.Slider(10, 50, value=28, step=1, label="Pas (qualité/temps)")
|
| 82 |
seed = gr.Number(value=0, precision=0, label="Seed (0 = aléatoire)")
|
|
|
|
| 4 |
import replicate
|
| 5 |
|
| 6 |
# ---- CONFIG ----
|
| 7 |
+
SPACE_ID = os.getenv("SPACE_ID", "") # ex: "aymce/pinky-maker" (fourni par HF automatiquement)
|
| 8 |
BASE_IMG_URL = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/pinky-monster.png"
|
| 9 |
+
|
| 10 |
+
# ✅ Modèle img2img qui existe bien sur Replicate
|
| 11 |
+
MODEL = "stability-ai/stable-diffusion" # SD 1.5 img2img
|
| 12 |
|
| 13 |
def _fetch_image(url: str) -> Image.Image:
|
| 14 |
r = requests.get(url, timeout=120)
|
|
|
|
| 16 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 17 |
|
| 18 |
def generate(prompt, strength, guidance, steps, seed, count):
|
|
|
|
| 19 |
token = os.getenv("REPLICATE_API_TOKEN")
|
| 20 |
if not token:
|
| 21 |
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value="❌ Ajoute REPLICATE_API_TOKEN (Settings → Secrets).")
|
| 22 |
|
| 23 |
+
# Vérifie l’accessibilité de l’image de base
|
| 24 |
try:
|
| 25 |
test = requests.get(BASE_IMG_URL, timeout=30)
|
| 26 |
if test.status_code != 200:
|
| 27 |
+
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value=f"❌ Image introuvable : {BASE_IMG_URL}\nUpload ‘pinky-monster.png’ à la racine du Space.")
|
| 28 |
except Exception as e:
|
| 29 |
return [Image.new("RGB", (1024, 1024), (30, 30, 30))], gr.update(value=f"❌ Erreur en lisant l'image de base : {e}")
|
| 30 |
|
| 31 |
client = replicate.Client(api_token=token)
|
| 32 |
|
| 33 |
+
imgs, note = [], "✅ Génération OK"
|
|
|
|
| 34 |
try:
|
| 35 |
+
for _ in range(count):
|
| 36 |
use_seed = None if int(seed) == 0 else int(seed)
|
| 37 |
+
# Appel Replicate — SD 1.5 img2img
|
| 38 |
out = client.run(
|
| 39 |
MODEL,
|
| 40 |
input={
|
| 41 |
"prompt": str(prompt),
|
| 42 |
+
"image": BASE_IMG_URL, # URL publique du fichier du Space
|
| 43 |
"prompt_strength": float(strength),# 0.1–0.9 (bas = plus fidèle)
|
| 44 |
"num_inference_steps": int(steps), # 10–50
|
| 45 |
"guidance_scale": float(guidance), # 1–15
|
| 46 |
"seed": use_seed,
|
| 47 |
+
"width": 768, # SD 1.5 tient bien en 768
|
| 48 |
+
"height": 768,
|
| 49 |
},
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Replicate renvoie une ou plusieurs URLs
|
| 53 |
+
if isinstance(out, list) and out and isinstance(out[0], str):
|
| 54 |
url = out[0]
|
| 55 |
elif isinstance(out, str):
|
| 56 |
url = out
|
| 57 |
else:
|
| 58 |
raise RuntimeError(f"Sortie inattendue de Replicate: {out}")
|
| 59 |
|
|
|
|
| 60 |
img = _fetch_image(url)
|
| 61 |
imgs.append(img)
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
+
note = f"❌ Erreur Replicate: {e}\n\nTrace:\n{traceback.format_exc()}"
|
|
|
|
| 65 |
if not imgs:
|
| 66 |
+
imgs = [Image.new("RGB", (768, 768), (30, 30, 30))]
|
| 67 |
|
| 68 |
return imgs, gr.update(value=note)
|
| 69 |
|
| 70 |
with gr.Blocks(title="PINKY Maker") as demo:
|
| 71 |
gr.Markdown("### PINKY Maker — génère ta version du monstre (IA)\n"
|
| 72 |
"- Mets ta clé **REPLICATE_API_TOKEN** dans Settings → Secrets\n"
|
| 73 |
+
"- Upload **pinky-monster.png** dans Files & versions (racine)")
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=1):
|
| 76 |
prompt = gr.Textbox(label="Décris ta variante",
|
| 77 |
value="monstre bleu avec des cornes dorées et une grosse chaîne")
|
| 78 |
+
strength = gr.Slider(0.1, 0.9, value=0.55, step=0.01, label="Force (fidèle ↔ créatif)")
|
| 79 |
guidance = gr.Slider(1, 15, value=7.5, step=0.5, label="Guidance (suivre le prompt)")
|
| 80 |
steps = gr.Slider(10, 50, value=28, step=1, label="Pas (qualité/temps)")
|
| 81 |
seed = gr.Number(value=0, precision=0, label="Seed (0 = aléatoire)")
|