ginipick commited on
Commit
2504a64
Β·
verified Β·
1 Parent(s): bf310fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -44
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(replicate_client, 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_client.models.get(model_name)
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
- # Create Replicate client with extended timeout
216
- client = replicate.Client(
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(client, "bytedance/seedance-1-lite")
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
- # Use prediction API for better control
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
- if prediction.status == "failed":
256
- error_msg = getattr(prediction, 'error', 'Unknown error')
257
- if "cold boot" in str(error_msg).lower() or "starting" in str(error_msg).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) # Wait 30 seconds before retry
261
- continue
262
- return None, f"❌ Generation failed: {error_msg}"
263
 
264
- elif prediction.status == "canceled":
265
- return None, "❌ Generation was canceled."
 
 
 
 
 
 
 
 
266
 
267
- # Success - get output
268
- output = prediction.output
269
- break
270
 
271
  except replicate.exceptions.ReplicateError as e:
272
- if "timeout" in str(e).lower() and attempt < max_attempts - 1:
 
 
 
 
 
 
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: {str(e)}"
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