menyifang commited on
Commit
bfd6cec
·
1 Parent(s): 9e2ed4d
Files changed (2) hide show
  1. app.py +40 -0
  2. oss_utils.py +104 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from oss_utils import ossService, get_random_string
4
+
5
+ def refresh_video(uuid, request_id):
6
+ if uuid is None or uuid == '':
7
+ uuid = get_random_string()
8
+ print(f'[refresh_video] generate a uuid {uuid}')
9
+
10
+ print(f'[refresh_video] uuid: {uuid}')
11
+ print(f'[refresh_video] request_id: {request_id}')
12
+
13
+ notes = '正在处理您的视频生成任务,需等待4.8分钟左右,点击刷新获取最新生成进度'
14
+ output = 'output/20241025174546.mp4'
15
+ return notes, output, uuid
16
+
17
+
18
+ with gr.Blocks() as demo:
19
+ with gr.Row():
20
+ with gr.Column(scale=0.8, min_width=0.8):
21
+ # user_notes = gr.Textbox(show_label=False, text_align='left', elem_id='text_style11')
22
+ user_notes = gr.Textbox(show_label=False, text_align='left')
23
+ with gr.Column(scale=0.2, min_width=0.2):
24
+ refresh_button = gr.Button(value="刷新", elem_id='button_param1')
25
+
26
+ uuid = gr.Text(label="modelscope_uuid", visible=False)
27
+ request_id = gr.Text(label="modelscope_request_id", visible=False)
28
+
29
+ refresh_button.click(
30
+ fn=refresh_video,
31
+ queue=False,
32
+ inputs=[uuid, request_id],
33
+ outputs=[user_notes, uuid]
34
+ )
35
+
36
+
37
+
38
+ if __name__ == "__main__":
39
+ demo.queue(max_size=100)
40
+ demo.launch(share=False)
oss_utils.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ import sys
4
+ import string
5
+ import time
6
+ import datetime
7
+ import oss2
8
+ import random
9
+ import requests
10
+ import shutil
11
+ import os
12
+
13
+ use_internal_network = False
14
+
15
+ OSSKEY = os.getenv('OSSAccessKeyId')
16
+ OSSPASSWD = os.getenv('OSSAccessKeySecret')
17
+ OSSEndpoint = os.getenv('OSSEndpoint')
18
+ OSSBucketName = os.getenv('OSSBucketName')
19
+ OSSObjectName = os.getenv('OSSObjectName')
20
+
21
+ def get_random_string():
22
+ now = datetime.datetime.now()
23
+ date = now.strftime('%Y%m%d')
24
+ time = now.strftime('%H%M%S')
25
+ microsecond = now.strftime('%f')
26
+ microsecond = microsecond[:6] # 取前6位,即微秒
27
+
28
+ rand_num = ''.join([str(random.randint(0, 9)) for _ in range(6)])
29
+ random_string = ''.join(random.choices(string.ascii_uppercase, k=6)) # ascii_lowercase
30
+ return date + "-" + time + "-" + microsecond + "-" + random_string
31
+
32
+ class ossService():
33
+ def __init__(self):
34
+ # print(f'AK: ******{OSSAccessKeyId[-3:]}')
35
+ # print(f'SK: ******{OSSAccessKeySecret[-3:]}')
36
+ self.AccessKeyId = OSSKEY
37
+ self.AccessKeySecret = OSSPASSWD
38
+ self.Endpoint = OSSEndpoint
39
+ if use_internal_network: #内网加-internal
40
+ self.Endpoint = OSSEndpoint[:-len(".aliyuncs.com")] + "-internal.aliyuncs.com"
41
+ self.BucketName = OSSBucketName # "vigen-invi" # "vigen-video"
42
+ self.ObjectName = OSSObjectName # "video_generation" # "VideoGeneration"
43
+ self.Prefix = "oss://" + self.BucketName
44
+
45
+ print('AK:', self.AccessKeyId)
46
+ print('SK:', self.AccessKeySecret)
47
+ print('Endpoint:', self.Endpoint)
48
+ print('BucketName:', self.BucketName)
49
+ print('ObjectName:', self.ObjectName)
50
+ print('Prefix:', self.Prefix)
51
+
52
+ auth = oss2.Auth(self.AccessKeyId, self.AccessKeySecret)
53
+ self.bucket = oss2.Bucket(auth, self.Endpoint, self.BucketName)
54
+
55
+ # # oss_url: eg: oss://BucketName/ObjectName/xxx.mp4
56
+ # def sign(self, oss_url, timeout=3600):
57
+ # try:
58
+ # oss_path = oss_url[len("oss://" + self.BucketName + "/"):]
59
+ # return 1, self.bucket.sign_url('GET', oss_path, timeout, slash_safe=True)
60
+ # except Exception as e:
61
+ # print("sign error: {}".format(e))
62
+ # return 0, ""
63
+
64
+ # oss_url: eg: oss://BucketName/ObjectName/xxx.mp4
65
+ # timeout: eg: 3600000
66
+ # params: eg: {'x-oss-process': style}
67
+ def sign(self, oss_url, timeout=86400, params=None):
68
+ try:
69
+ oss_path = oss_url[len("oss://" + self.BucketName + "/"):]
70
+ return 1, self.bucket.sign_url('GET', oss_path, timeout, slash_safe=True, params=params), 'Success.'
71
+ except Exception as e:
72
+ print("sign error: {}".format(e))
73
+ return 0, "", f'{e}'
74
+
75
+ # eg: ObjectName/xxx.mp4
76
+ def uploadOssFile(self, oss_full_path, local_full_path):
77
+ try:
78
+ self.bucket.put_object_from_file(oss_full_path, local_full_path)
79
+ status,sign_url,message = self.sign(self.Prefix+"/"+oss_full_path, timeout=3600)
80
+ return status,sign_url,message
81
+ except oss2.exceptions.OssError as e:
82
+ print("oss upload error: ", e)
83
+ return 0, "", f'{e}'
84
+
85
+ # eg: ObjectName/xxx.mp4
86
+ def downloadOssFile(self, oss_full_path, local_full_path):
87
+ try:
88
+ self.bucket.get_object_to_file(oss_full_path, local_full_path)
89
+ return 1, 'Success.'
90
+ except oss2.exceptions.OssError as e:
91
+ print("oss download error: ", e)
92
+ return 0, f'{e}'
93
+
94
+
95
+ def downloadFile(self, file_full_url, local_full_path):
96
+ response = requests.get(file_full_url)
97
+ if response.status_code == 200:
98
+ with open(local_full_path, "wb") as f:
99
+ f.write(response.content)
100
+ return 1, 'Success.'
101
+ else:
102
+ print("oss download error. ")
103
+ return 0, f'{response}'
104
+