Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1105,6 +1105,7 @@ def do_polish_transform_prompt(prompt: str, style: str, do_polish: bool) -> Tupl
|
|
| 1105 |
def generate_with_polish(prompt: str, style: str, do_polish: bool, ratio: str, steps: int, seed: int, randomize: bool):
|
| 1106 |
"""Unified generate with progress feedback using generator.
|
| 1107 |
Yields intermediate status updates with timer so user knows what's happening.
|
|
|
|
| 1108 |
"""
|
| 1109 |
logger.info(f"generate_with_polish: do_polish={do_polish}, style={style}, prompt_len={len(prompt) if prompt else 0}")
|
| 1110 |
|
|
@@ -1133,8 +1134,33 @@ def generate_with_polish(prompt: str, style: str, do_polish: bool, ratio: str, s
|
|
| 1133 |
# Show status before GPU generation with the prompt that will be used
|
| 1134 |
yield None, create_status_html("Generating image", timer.format()), seed
|
| 1135 |
|
| 1136 |
-
# GPU generation
|
| 1137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1138 |
|
| 1139 |
# Stop timer and show final result
|
| 1140 |
timer.stop()
|
|
@@ -1145,6 +1171,7 @@ def generate_with_polish(prompt: str, style: str, do_polish: bool, ratio: str, s
|
|
| 1145 |
def transform_with_polish(input_image: Optional[Image.Image], prompt: str, style: str, do_polish: bool, strength: float, steps: int, seed: int, randomize: bool):
|
| 1146 |
"""Unified transform with progress feedback using generator.
|
| 1147 |
Yields intermediate status updates with timer so user knows what's happening.
|
|
|
|
| 1148 |
|
| 1149 |
Style-only transformation: When a style is selected without a prompt, the style
|
| 1150 |
suffix contains all the information needed to guide the transformation.
|
|
@@ -1181,8 +1208,33 @@ def transform_with_polish(input_image: Optional[Image.Image], prompt: str, style
|
|
| 1181 |
# Show status before GPU transform
|
| 1182 |
yield None, create_status_html("Transforming image", timer.format()), 0
|
| 1183 |
|
| 1184 |
-
# GPU transform
|
| 1185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1186 |
|
| 1187 |
# Stop timer and show final result
|
| 1188 |
timer.stop()
|
|
|
|
| 1105 |
def generate_with_polish(prompt: str, style: str, do_polish: bool, ratio: str, steps: int, seed: int, randomize: bool):
|
| 1106 |
"""Unified generate with progress feedback using generator.
|
| 1107 |
Yields intermediate status updates with timer so user knows what's happening.
|
| 1108 |
+
Includes automatic retry for ZeroGPU allocation failures.
|
| 1109 |
"""
|
| 1110 |
logger.info(f"generate_with_polish: do_polish={do_polish}, style={style}, prompt_len={len(prompt) if prompt else 0}")
|
| 1111 |
|
|
|
|
| 1134 |
# Show status before GPU generation with the prompt that will be used
|
| 1135 |
yield None, create_status_html("Generating image", timer.format()), seed
|
| 1136 |
|
| 1137 |
+
# GPU generation with automatic retry for ZeroGPU failures
|
| 1138 |
+
max_retries = 3
|
| 1139 |
+
image = None
|
| 1140 |
+
used_seed = seed
|
| 1141 |
+
last_error = None
|
| 1142 |
+
|
| 1143 |
+
for attempt in range(max_retries):
|
| 1144 |
+
try:
|
| 1145 |
+
image, used_seed = generate(full_prompt, polished_display, ratio, steps, seed, randomize)
|
| 1146 |
+
if image is not None:
|
| 1147 |
+
break # Success
|
| 1148 |
+
except RuntimeError as e:
|
| 1149 |
+
last_error = e
|
| 1150 |
+
error_msg = str(e).lower()
|
| 1151 |
+
if "cuda" in error_msg or "gpu" in error_msg or "driver" in error_msg:
|
| 1152 |
+
logger.warning(f"GPU allocation failed (attempt {attempt + 1}/{max_retries}): {e}")
|
| 1153 |
+
if attempt < max_retries - 1:
|
| 1154 |
+
import time
|
| 1155 |
+
time.sleep(1) # Brief pause before retry
|
| 1156 |
+
yield None, create_status_html(f"GPU busy, retrying ({attempt + 2}/{max_retries})", timer.format()), seed
|
| 1157 |
+
continue
|
| 1158 |
+
raise # Re-raise non-GPU errors
|
| 1159 |
+
|
| 1160 |
+
if image is None and last_error:
|
| 1161 |
+
timer.stop()
|
| 1162 |
+
yield None, create_status_html(f"GPU unavailable after {max_retries} attempts. Please try again.", timer.format(), is_generating=False).replace("✅", "❌"), seed
|
| 1163 |
+
return
|
| 1164 |
|
| 1165 |
# Stop timer and show final result
|
| 1166 |
timer.stop()
|
|
|
|
| 1171 |
def transform_with_polish(input_image: Optional[Image.Image], prompt: str, style: str, do_polish: bool, strength: float, steps: int, seed: int, randomize: bool):
|
| 1172 |
"""Unified transform with progress feedback using generator.
|
| 1173 |
Yields intermediate status updates with timer so user knows what's happening.
|
| 1174 |
+
Includes automatic retry for ZeroGPU allocation failures.
|
| 1175 |
|
| 1176 |
Style-only transformation: When a style is selected without a prompt, the style
|
| 1177 |
suffix contains all the information needed to guide the transformation.
|
|
|
|
| 1208 |
# Show status before GPU transform
|
| 1209 |
yield None, create_status_html("Transforming image", timer.format()), 0
|
| 1210 |
|
| 1211 |
+
# GPU transform with automatic retry for ZeroGPU failures
|
| 1212 |
+
max_retries = 3
|
| 1213 |
+
image = None
|
| 1214 |
+
used_seed = 0
|
| 1215 |
+
last_error = None
|
| 1216 |
+
|
| 1217 |
+
for attempt in range(max_retries):
|
| 1218 |
+
try:
|
| 1219 |
+
image, used_seed = transform(input_image, full_prompt, polished_display, strength, steps, seed, randomize)
|
| 1220 |
+
if image is not None:
|
| 1221 |
+
break # Success
|
| 1222 |
+
except RuntimeError as e:
|
| 1223 |
+
last_error = e
|
| 1224 |
+
error_msg = str(e).lower()
|
| 1225 |
+
if "cuda" in error_msg or "gpu" in error_msg or "driver" in error_msg:
|
| 1226 |
+
logger.warning(f"GPU allocation failed (attempt {attempt + 1}/{max_retries}): {e}")
|
| 1227 |
+
if attempt < max_retries - 1:
|
| 1228 |
+
import time
|
| 1229 |
+
time.sleep(1) # Brief pause before retry
|
| 1230 |
+
yield None, create_status_html(f"GPU busy, retrying ({attempt + 2}/{max_retries})", timer.format()), 0
|
| 1231 |
+
continue
|
| 1232 |
+
raise # Re-raise non-GPU errors
|
| 1233 |
+
|
| 1234 |
+
if image is None and last_error:
|
| 1235 |
+
timer.stop()
|
| 1236 |
+
yield None, create_status_html(f"GPU unavailable after {max_retries} attempts. Please try again.", timer.format(), is_generating=False).replace("✅", "❌"), 0
|
| 1237 |
+
return
|
| 1238 |
|
| 1239 |
# Stop timer and show final result
|
| 1240 |
timer.stop()
|