Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -148,12 +148,12 @@ def update_image_input(mode):
|
|
148 |
else:
|
149 |
return gr.update(visible=False)
|
150 |
|
151 |
-
def wait_for_model_with_retry(
|
152 |
"""Wait for model to be ready with retry logic"""
|
153 |
for attempt in range(max_retries):
|
154 |
try:
|
155 |
# Try to get model info
|
156 |
-
model =
|
157 |
return True
|
158 |
except Exception as e:
|
159 |
if attempt < max_retries - 1:
|
@@ -212,14 +212,11 @@ def generate_video(mode, prompt, image, aspect_ratio, seed, api_key_input, progr
|
|
212 |
|
213 |
progress(0.2, desc="Checking model availability...")
|
214 |
|
215 |
-
#
|
216 |
-
|
217 |
-
api_token=token,
|
218 |
-
timeout=300 # 5 minutes timeout
|
219 |
-
)
|
220 |
|
221 |
# Wait for model to be ready
|
222 |
-
model_ready = wait_for_model_with_retry(
|
223 |
if not model_ready:
|
224 |
return None, "β³ Model is still booting up. Please try again in a few minutes."
|
225 |
|
@@ -227,54 +224,47 @@ def generate_video(mode, prompt, image, aspect_ratio, seed, api_key_input, progr
|
|
227 |
|
228 |
# Run Replicate with retry logic
|
229 |
max_attempts = 3
|
|
|
|
|
230 |
for attempt in range(max_attempts):
|
231 |
try:
|
232 |
-
#
|
233 |
-
prediction = client.predictions.create(
|
234 |
-
version="bytedance/seedance-1-lite:latest",
|
235 |
-
input=input_params,
|
236 |
-
webhook_completed=None
|
237 |
-
)
|
238 |
-
|
239 |
-
# Poll for completion with extended timeout
|
240 |
start_time = time.time()
|
241 |
-
timeout_seconds = 300 # 5 minutes
|
242 |
-
|
243 |
-
while prediction.status not in ["succeeded", "failed", "canceled"]:
|
244 |
-
if time.time() - start_time > timeout_seconds:
|
245 |
-
return None, "β±οΈ Generation timed out. The server might be under heavy load. Please try again."
|
246 |
-
|
247 |
-
time.sleep(2) # Poll every 2 seconds
|
248 |
-
prediction.reload()
|
249 |
-
|
250 |
-
# Update progress
|
251 |
-
elapsed = time.time() - start_time
|
252 |
-
progress_val = min(0.3 + (elapsed / timeout_seconds) * 0.4, 0.7)
|
253 |
-
progress(progress_val, desc=f"Generating video... ({int(elapsed)}s)")
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
time.sleep(30) # Wait 30 seconds before retry
|
261 |
-
continue
|
262 |
-
return None, f"β Generation failed: {error_msg}"
|
263 |
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
-
#
|
268 |
-
|
269 |
-
|
270 |
|
271 |
except replicate.exceptions.ReplicateError as e:
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
progress(0.3, desc=f"Timeout occurred, retrying... (Attempt {attempt + 2}/{max_attempts})")
|
274 |
time.sleep(10)
|
275 |
continue
|
276 |
else:
|
277 |
-
return None, f"β Replicate API error: {
|
278 |
except Exception as e:
|
279 |
if attempt < max_attempts - 1:
|
280 |
progress(0.3, desc=f"Error occurred, retrying... (Attempt {attempt + 2}/{max_attempts})")
|
@@ -283,6 +273,10 @@ def generate_video(mode, prompt, image, aspect_ratio, seed, api_key_input, progr
|
|
283 |
else:
|
284 |
return None, f"β Unexpected error: {str(e)}"
|
285 |
|
|
|
|
|
|
|
|
|
286 |
progress(0.7, desc="Downloading video...")
|
287 |
|
288 |
# Get video data
|
|
|
148 |
else:
|
149 |
return gr.update(visible=False)
|
150 |
|
151 |
+
def wait_for_model_with_retry(model_name, max_retries=5, initial_wait=10):
|
152 |
"""Wait for model to be ready with retry logic"""
|
153 |
for attempt in range(max_retries):
|
154 |
try:
|
155 |
# Try to get model info
|
156 |
+
model = replicate.models.get(model_name)
|
157 |
return True
|
158 |
except Exception as e:
|
159 |
if attempt < max_retries - 1:
|
|
|
212 |
|
213 |
progress(0.2, desc="Checking model availability...")
|
214 |
|
215 |
+
# Set up Replicate with the API token
|
216 |
+
replicate.api_token = token
|
|
|
|
|
|
|
217 |
|
218 |
# Wait for model to be ready
|
219 |
+
model_ready = wait_for_model_with_retry("bytedance/seedance-1-lite")
|
220 |
if not model_ready:
|
221 |
return None, "β³ Model is still booting up. Please try again in a few minutes."
|
222 |
|
|
|
224 |
|
225 |
# Run Replicate with retry logic
|
226 |
max_attempts = 3
|
227 |
+
output = None
|
228 |
+
|
229 |
for attempt in range(max_attempts):
|
230 |
try:
|
231 |
+
# Run Replicate - use the model directly without version specifier
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
+
# Create a generator for progress tracking
|
235 |
+
output_generator = replicate.run(
|
236 |
+
"bytedance/seedance-1-lite",
|
237 |
+
input=input_params
|
238 |
+
)
|
|
|
|
|
|
|
239 |
|
240 |
+
# If it's a generator, iterate through it
|
241 |
+
if hasattr(output_generator, '__iter__') and not isinstance(output_generator, (str, bytes)):
|
242 |
+
for event in output_generator:
|
243 |
+
elapsed = time.time() - start_time
|
244 |
+
progress_val = min(0.3 + (elapsed / 300) * 0.4, 0.7)
|
245 |
+
progress(progress_val, desc=f"Generating video... ({int(elapsed)}s)")
|
246 |
+
output = event # Keep the last event as output
|
247 |
+
else:
|
248 |
+
# If it's not a generator, use it directly
|
249 |
+
output = output_generator
|
250 |
|
251 |
+
# If we got output, break the retry loop
|
252 |
+
if output:
|
253 |
+
break
|
254 |
|
255 |
except replicate.exceptions.ReplicateError as e:
|
256 |
+
error_str = str(e)
|
257 |
+
if "cold boot" in error_str.lower() or "starting" in error_str.lower():
|
258 |
+
if attempt < max_attempts - 1:
|
259 |
+
progress(0.3, desc=f"Model is starting up, retrying... (Attempt {attempt + 2}/{max_attempts})")
|
260 |
+
time.sleep(30)
|
261 |
+
continue
|
262 |
+
elif "timeout" in error_str.lower() and attempt < max_attempts - 1:
|
263 |
progress(0.3, desc=f"Timeout occurred, retrying... (Attempt {attempt + 2}/{max_attempts})")
|
264 |
time.sleep(10)
|
265 |
continue
|
266 |
else:
|
267 |
+
return None, f"β Replicate API error: {error_str}"
|
268 |
except Exception as e:
|
269 |
if attempt < max_attempts - 1:
|
270 |
progress(0.3, desc=f"Error occurred, retrying... (Attempt {attempt + 2}/{max_attempts})")
|
|
|
273 |
else:
|
274 |
return None, f"β Unexpected error: {str(e)}"
|
275 |
|
276 |
+
# Check if we got output
|
277 |
+
if not output:
|
278 |
+
return None, "β Failed to generate video after multiple attempts."
|
279 |
+
|
280 |
progress(0.7, desc="Downloading video...")
|
281 |
|
282 |
# Get video data
|