Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,12 +11,14 @@ import cv2
|
|
| 11 |
import os
|
| 12 |
|
| 13 |
# --- CONFIGURATION ---
|
| 14 |
-
# SECURE SETUP:
|
| 15 |
-
# Do NOT paste your token here directly.
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
-
|
| 18 |
TRIPOSR_MODEL = "stabilityai/TripoSR"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def photo_to_sketch(image):
|
| 21 |
"""Instant local sketch – always returns PIL Image"""
|
| 22 |
print("-> Starting Sketch Generation...")
|
|
@@ -64,15 +66,14 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
|
|
| 64 |
files = {"inputs": ("sketch.png", img_bytes, "image/png")}
|
| 65 |
data = {"parameters": json.dumps({"prompt": prompt})}
|
| 66 |
|
| 67 |
-
# Use the token securely
|
| 68 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 69 |
|
| 70 |
# Retry Loop
|
| 71 |
for attempt in range(1, 4):
|
| 72 |
try:
|
| 73 |
-
print(f"-> Attempt {attempt}/3: Sending request to
|
| 74 |
r = requests.post(
|
| 75 |
-
|
| 76 |
headers=headers,
|
| 77 |
files=files,
|
| 78 |
data=data,
|
|
@@ -93,8 +94,12 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
|
|
| 93 |
return f"data:model/gltf-binary;base64,{b64}", tmp.name
|
| 94 |
|
| 95 |
elif r.status_code == 503:
|
| 96 |
-
print("-> Model is loading (503). Waiting...")
|
| 97 |
time.sleep(20)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
elif r.status_code == 401:
|
| 99 |
raise gr.Error("Authentication Failed. Check your HF_TOKEN secret.")
|
| 100 |
else:
|
|
@@ -105,7 +110,7 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
|
|
| 105 |
time.sleep(5)
|
| 106 |
|
| 107 |
print("-> All attempts failed.")
|
| 108 |
-
raise gr.Error("Generation Failed. The
|
| 109 |
|
| 110 |
# =============== UI ===============
|
| 111 |
with gr.Blocks(title="SketchToLife") as demo:
|
|
|
|
| 11 |
import os
|
| 12 |
|
| 13 |
# --- CONFIGURATION ---
|
| 14 |
+
# SECURE SETUP: Read token from Environment Variables.
|
|
|
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 16 |
TRIPOSR_MODEL = "stabilityai/TripoSR"
|
| 17 |
|
| 18 |
+
# NEW URL: The old "api-inference" URL is deprecated (Error 410).
|
| 19 |
+
# We use the new Router URL.
|
| 20 |
+
API_URL = f"https://router.huggingface.co/hf-inference/models/{TRIPOSR_MODEL}"
|
| 21 |
+
|
| 22 |
def photo_to_sketch(image):
|
| 23 |
"""Instant local sketch – always returns PIL Image"""
|
| 24 |
print("-> Starting Sketch Generation...")
|
|
|
|
| 66 |
files = {"inputs": ("sketch.png", img_bytes, "image/png")}
|
| 67 |
data = {"parameters": json.dumps({"prompt": prompt})}
|
| 68 |
|
|
|
|
| 69 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 70 |
|
| 71 |
# Retry Loop
|
| 72 |
for attempt in range(1, 4):
|
| 73 |
try:
|
| 74 |
+
print(f"-> Attempt {attempt}/3: Sending request to {API_URL}...")
|
| 75 |
r = requests.post(
|
| 76 |
+
API_URL,
|
| 77 |
headers=headers,
|
| 78 |
files=files,
|
| 79 |
data=data,
|
|
|
|
| 94 |
return f"data:model/gltf-binary;base64,{b64}", tmp.name
|
| 95 |
|
| 96 |
elif r.status_code == 503:
|
| 97 |
+
print("-> Model is loading (503). Waiting 20s...")
|
| 98 |
time.sleep(20)
|
| 99 |
+
elif r.status_code == 410:
|
| 100 |
+
print("-> Error 410: Endpoint deprecated. Trying fallback...")
|
| 101 |
+
# Fallback to standard inference if router fails, though router is the fix.
|
| 102 |
+
time.sleep(5)
|
| 103 |
elif r.status_code == 401:
|
| 104 |
raise gr.Error("Authentication Failed. Check your HF_TOKEN secret.")
|
| 105 |
else:
|
|
|
|
| 110 |
time.sleep(5)
|
| 111 |
|
| 112 |
print("-> All attempts failed.")
|
| 113 |
+
raise gr.Error("Generation Failed. The model is busy. If this persists, the free API tier for TripoSR might be overloaded.")
|
| 114 |
|
| 115 |
# =============== UI ===============
|
| 116 |
with gr.Blocks(title="SketchToLife") as demo:
|