akhaliq HF Staff commited on
Commit
1b88239
·
1 Parent(s): c0afa65

update timeout

Browse files
Files changed (1) hide show
  1. app.py +79 -29
app.py CHANGED
@@ -3689,7 +3689,40 @@ class WanAnimateApp:
3689
  if self.api_key:
3690
  dashscope.api_key = self.api_key
3691
  self.url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis/"
3692
- self.get_url = "https://dashscope.aliyuncs.com/api/v1/tasks/"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3693
 
3694
  def predict(self, ref_img, video, model_id, model):
3695
  """
@@ -3757,39 +3790,50 @@ class WanAnimateApp:
3757
  "Content-Type": "application/json"
3758
  }
3759
 
3760
- max_attempts = 60 # 5 minutes max wait time
3761
  attempt = 0
3762
 
3763
  while attempt < max_attempts:
3764
- response = requests.get(get_url, headers=headers)
3765
- if response.status_code != 200:
3766
- error_msg = f"Failed to get task status: {response.status_code}: {response.text}"
3767
- print(f"[WanAnimate] {error_msg}")
3768
- return None, error_msg
3769
-
3770
- result = response.json()
3771
- print(f"[WanAnimate] Task status check {attempt + 1}: {result}")
3772
- task_status = result.get("output", {}).get("task_status")
3773
-
3774
- if task_status == "SUCCEEDED":
3775
- # Task completed successfully, return video URL
3776
- video_url = result["output"]["results"]["video_url"]
3777
- print(f"[WanAnimate] Animation completed successfully: {video_url}")
3778
- return video_url, "SUCCEEDED"
3779
- elif task_status == "FAILED":
3780
- # Task failed, return error message
3781
- error_msg = result.get("output", {}).get("message", "Unknown error")
3782
- code_msg = result.get("output", {}).get("code", "Unknown code")
3783
- full_error = f"Task failed: {error_msg} Code: {code_msg} TaskId: {task_id}"
3784
- print(f"[WanAnimate] {full_error}")
3785
- return None, full_error
3786
- else:
3787
- # Task is still running, wait and retry
3788
- time.sleep(5) # Wait 5 seconds before polling again
 
 
 
 
 
 
 
 
 
 
3789
  attempt += 1
 
3790
 
3791
  # Timeout reached
3792
- timeout_msg = f"Animation generation timed out after {max_attempts * 5} seconds. TaskId: {task_id}"
3793
  print(f"[WanAnimate] {timeout_msg}")
3794
  return None, timeout_msg
3795
 
@@ -3892,7 +3936,13 @@ def generate_animation_from_image_video(input_image_data, input_video_data, prom
3892
  print(f"[ImageVideo2Animation] {error_msg}")
3893
  return f"Error: {error_msg}"
3894
  else:
3895
- error_msg = f"Animation generation failed: {status}"
 
 
 
 
 
 
3896
  print(f"[ImageVideo2Animation] {error_msg}")
3897
  return f"Error: {error_msg}"
3898
 
 
3689
  if self.api_key:
3690
  dashscope.api_key = self.api_key
3691
  self.url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis/"
3692
+ self.get_url = "https://dashscope.aliyuncs.com/api/v1/tasks"
3693
+
3694
+ def check_task_status(self, task_id: str):
3695
+ """Check the status of a specific animation task by TaskId"""
3696
+ if not self.api_key:
3697
+ return None, "Error: DASHSCOPE_API_KEY environment variable is not set"
3698
+
3699
+ try:
3700
+ get_url = f"{self.get_url}/{task_id}"
3701
+ headers = {
3702
+ "Authorization": f"Bearer {self.api_key}",
3703
+ "Content-Type": "application/json"
3704
+ }
3705
+
3706
+ response = requests.get(get_url, headers=headers, timeout=30)
3707
+ if response.status_code != 200:
3708
+ error_msg = f"Failed to get task status: {response.status_code}: {response.text}"
3709
+ return None, error_msg
3710
+
3711
+ result = response.json()
3712
+ task_status = result.get("output", {}).get("task_status")
3713
+
3714
+ if task_status == "SUCCEEDED":
3715
+ video_url = result["output"]["results"]["video_url"]
3716
+ return video_url, "SUCCEEDED"
3717
+ elif task_status == "FAILED":
3718
+ error_msg = result.get("output", {}).get("message", "Unknown error")
3719
+ code_msg = result.get("output", {}).get("code", "Unknown code")
3720
+ return None, f"Task failed: {error_msg} Code: {code_msg}"
3721
+ else:
3722
+ return None, f"Task is still {task_status}"
3723
+
3724
+ except Exception as e:
3725
+ return None, f"Exception checking task status: {str(e)}"
3726
 
3727
  def predict(self, ref_img, video, model_id, model):
3728
  """
 
3790
  "Content-Type": "application/json"
3791
  }
3792
 
3793
+ max_attempts = 180 # 15 minutes max wait time (increased from 5 minutes)
3794
  attempt = 0
3795
 
3796
  while attempt < max_attempts:
3797
+ try:
3798
+ response = requests.get(get_url, headers=headers, timeout=30)
3799
+ if response.status_code != 200:
3800
+ error_msg = f"Failed to get task status: {response.status_code}: {response.text}"
3801
+ print(f"[WanAnimate] {error_msg}")
3802
+ return None, error_msg
3803
+
3804
+ result = response.json()
3805
+ task_status = result.get("output", {}).get("task_status")
3806
+
3807
+ # Log progress every 20 attempts (100 seconds) to show activity
3808
+ if attempt % 20 == 0 or task_status in ["SUCCEEDED", "FAILED"]:
3809
+ print(f"[WanAnimate] Task status check {attempt + 1}/{max_attempts}: {task_status} (TaskId: {task_id})")
3810
+
3811
+ if task_status == "SUCCEEDED":
3812
+ # Task completed successfully, return video URL
3813
+ video_url = result["output"]["results"]["video_url"]
3814
+ print(f"[WanAnimate] Animation completed successfully: {video_url}")
3815
+ return video_url, "SUCCEEDED"
3816
+ elif task_status == "FAILED":
3817
+ # Task failed, return error message
3818
+ error_msg = result.get("output", {}).get("message", "Unknown error")
3819
+ code_msg = result.get("output", {}).get("code", "Unknown code")
3820
+ full_error = f"Task failed: {error_msg} Code: {code_msg} TaskId: {task_id}"
3821
+ print(f"[WanAnimate] {full_error}")
3822
+ return None, full_error
3823
+ else:
3824
+ # Task is still running, wait and retry
3825
+ time.sleep(5) # Wait 5 seconds before polling again
3826
+ attempt += 1
3827
+
3828
+ except requests.exceptions.RequestException as e:
3829
+ print(f"[WanAnimate] Network error during status check {attempt + 1}: {str(e)}")
3830
+ # For network errors, wait a bit longer before retrying
3831
+ time.sleep(10)
3832
  attempt += 1
3833
+ continue
3834
 
3835
  # Timeout reached
3836
+ timeout_msg = f"Animation generation timed out after {max_attempts * 5} seconds ({max_attempts * 5 // 60} minutes). TaskId: {task_id}. The animation may still be processing - please check back later or try with a simpler input."
3837
  print(f"[WanAnimate] {timeout_msg}")
3838
  return None, timeout_msg
3839
 
 
3936
  print(f"[ImageVideo2Animation] {error_msg}")
3937
  return f"Error: {error_msg}"
3938
  else:
3939
+ # Provide more helpful error messages based on status
3940
+ if "timed out" in str(status).lower():
3941
+ error_msg = f"Animation generation timed out. This can happen with complex animations or during high server load. Please try again with simpler inputs or wait a few minutes before retrying. Details: {status}"
3942
+ elif "taskid" in str(status).lower():
3943
+ error_msg = f"Animation generation failed. You can check the status later using the TaskId from the error message. Details: {status}"
3944
+ else:
3945
+ error_msg = f"Animation generation failed: {status}"
3946
  print(f"[ImageVideo2Animation] {error_msg}")
3947
  return f"Error: {error_msg}"
3948