kelseye commited on
Commit
708bb0f
·
verified ·
1 Parent(s): a752363

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -3
app.py CHANGED
@@ -8,13 +8,68 @@ import time
8
  import gradio as gr
9
  import requests
10
 
 
 
 
11
  import dashscope
12
- from dashscope.utils.oss_utils import check_and_upload_local
13
 
14
  DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
15
  dashscope.api_key = DASHSCOPE_API_KEY
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  class WanAnimateApp:
19
  def __init__(self, url, get_url):
20
  self.url = url
@@ -28,8 +83,8 @@ class WanAnimateApp:
28
  model,
29
  ):
30
  # Upload files to OSS if needed and get URLs
31
- _, image_url = check_and_upload_local(model_id, ref_img, DASHSCOPE_API_KEY)
32
- _, video_url = check_and_upload_local(model_id, video, DASHSCOPE_API_KEY)
33
 
34
  # Prepare the request payload
35
  payload = {
 
8
  import gradio as gr
9
  import requests
10
 
11
+ from pathlib import Path
12
+ from datetime import datetime, timedelta
13
+
14
  import dashscope
15
+ # from dashscope.utils.oss_utils import check_and_upload_local
16
 
17
  DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
18
  dashscope.api_key = DASHSCOPE_API_KEY
19
 
20
 
21
+
22
+ def get_upload_policy(api_key, model_name):
23
+ """获取文件上传凭证"""
24
+ url = "https://dashscope.aliyuncs.com/api/v1/uploads"
25
+ headers = {
26
+ "Authorization": f"Bearer {api_key}",
27
+ "Content-Type": "application/json"
28
+ }
29
+ params = {
30
+ "action": "getPolicy",
31
+ "model": model_name
32
+ }
33
+
34
+ response = requests.get(url, headers=headers, params=params)
35
+ if response.status_code != 200:
36
+ raise Exception(f"Failed to get upload policy: {response.text}")
37
+
38
+ return response.json()['data']
39
+
40
+ def upload_file_to_oss(policy_data, file_path):
41
+ """将文件上传到临时存储OSS"""
42
+ file_name = Path(file_path).name
43
+ key = f"{policy_data['upload_dir']}/{file_name}"
44
+
45
+ with open(file_path, 'rb') as file:
46
+ files = {
47
+ 'OSSAccessKeyId': (None, policy_data['oss_access_key_id']),
48
+ 'Signature': (None, policy_data['signature']),
49
+ 'policy': (None, policy_data['policy']),
50
+ 'x-oss-object-acl': (None, policy_data['x_oss_object_acl']),
51
+ 'x-oss-forbid-overwrite': (None, policy_data['x_oss_forbid_overwrite']),
52
+ 'key': (None, key),
53
+ 'success_action_status': (None, '200'),
54
+ 'file': (file_name, file)
55
+ }
56
+
57
+ response = requests.post(policy_data['upload_host'], files=files)
58
+ if response.status_code != 200:
59
+ raise Exception(f"Failed to upload file: {response.text}")
60
+
61
+ return f"oss://{key}"
62
+
63
+ def upload_file_and_get_url(api_key, model_name, file_path):
64
+ """上传文件并获取URL"""
65
+ # 1. 获取上传凭证,上传凭证接口有限流,超出限流将导致请求失败
66
+ policy_data = get_upload_policy(api_key, model_name)
67
+ # 2. 上传文件到OSS
68
+ oss_url = upload_file_to_oss(policy_data, file_path)
69
+
70
+ return oss_url
71
+
72
+
73
  class WanAnimateApp:
74
  def __init__(self, url, get_url):
75
  self.url = url
 
83
  model,
84
  ):
85
  # Upload files to OSS if needed and get URLs
86
+ image_url = upload_file_and_get_url(DASHSCOPE_API_KEY, model_id, ref_img)
87
+ video_url = upload_file_and_get_url(DASHSCOPE_API_KEY, model_id, video)
88
 
89
  # Prepare the request payload
90
  payload = {