Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import uuid
|
|
7 |
import json
|
8 |
import base64
|
9 |
from io import BytesIO
|
|
|
10 |
from oss2.credentials import EnvironmentVariableCredentialsProvider,StaticCredentialsProvider
|
11 |
|
12 |
# 下载网络图片
|
@@ -18,6 +19,14 @@ def download_image(url):
|
|
18 |
except requests.exceptions.RequestException as e:
|
19 |
raise RuntimeError(f"图片下载失败: {e}")
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def remove_metadata(image, output_path=None):
|
22 |
# 获取图片的格式
|
23 |
img_format = image.format.lower()
|
@@ -87,6 +96,8 @@ def run(params):
|
|
87 |
model_type = params['model_type']
|
88 |
compress = params.get('compress',False)
|
89 |
quality = params.get('quality',85)
|
|
|
|
|
90 |
# url,version_id,access_key_id,access_key_secret,securityToken,endpoint,bucket_name,upload_filename,callback_url,callback_header_key,callback_header_secret
|
91 |
# 网络图片 URL 和本地保存路径
|
92 |
tmp_file_name = uuid.uuid4()
|
@@ -100,9 +111,9 @@ def run(params):
|
|
100 |
output_image_path = compress_image(downloaded_image,result_image)
|
101 |
else:
|
102 |
output_image_path = remove_metadata(downloaded_image,result_image)
|
103 |
-
print("元数据移除完成")
|
104 |
if access_key_id and endpoint and bucket_name:
|
105 |
-
print('上传OSS')
|
106 |
# 步骤 3:上传到阿里云 OSS
|
107 |
oss_url = upload_to_oss(output_image_path, upload_filename, bucket_name, access_key_id, access_key_secret,securityToken, endpoint)
|
108 |
|
@@ -117,14 +128,14 @@ def run(params):
|
|
117 |
f"{callback_header_key}": callback_header_secret
|
118 |
})
|
119 |
print(res.status_code)
|
120 |
-
return f"图片上传成功,URL:{oss_url}"
|
121 |
else:
|
122 |
-
return "上传到 OSS 失败"
|
123 |
else:
|
124 |
-
print('不上传OSS')
|
125 |
with open(output_image_path,'rb') as f:
|
126 |
image_base64 = base64.b64encode(f.read())
|
127 |
-
print("运行完成")
|
128 |
delete_local_file(output_image_path) # 删除压缩后的图片
|
129 |
return image_base64
|
130 |
else:
|
|
|
7 |
import json
|
8 |
import base64
|
9 |
from io import BytesIO
|
10 |
+
from datetime import datetime
|
11 |
from oss2.credentials import EnvironmentVariableCredentialsProvider,StaticCredentialsProvider
|
12 |
|
13 |
# 下载网络图片
|
|
|
19 |
except requests.exceptions.RequestException as e:
|
20 |
raise RuntimeError(f"图片下载失败: {e}")
|
21 |
|
22 |
+
|
23 |
+
|
24 |
+
def formatNow():
|
25 |
+
now = datetime.now()
|
26 |
+
return now.strftime("%Y-%m-%d %H:%M:%S")
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
def remove_metadata(image, output_path=None):
|
31 |
# 获取图片的格式
|
32 |
img_format = image.format.lower()
|
|
|
96 |
model_type = params['model_type']
|
97 |
compress = params.get('compress',False)
|
98 |
quality = params.get('quality',85)
|
99 |
+
env_text = params.get('env','-')
|
100 |
+
logPrefix = f"{formatNow()}-{env_text}-"
|
101 |
# url,version_id,access_key_id,access_key_secret,securityToken,endpoint,bucket_name,upload_filename,callback_url,callback_header_key,callback_header_secret
|
102 |
# 网络图片 URL 和本地保存路径
|
103 |
tmp_file_name = uuid.uuid4()
|
|
|
111 |
output_image_path = compress_image(downloaded_image,result_image)
|
112 |
else:
|
113 |
output_image_path = remove_metadata(downloaded_image,result_image)
|
114 |
+
print(f"{logPrefix}元数据移除完成")
|
115 |
if access_key_id and endpoint and bucket_name:
|
116 |
+
print(f'{logPrefix}上传OSS')
|
117 |
# 步骤 3:上传到阿里云 OSS
|
118 |
oss_url = upload_to_oss(output_image_path, upload_filename, bucket_name, access_key_id, access_key_secret,securityToken, endpoint)
|
119 |
|
|
|
128 |
f"{callback_header_key}": callback_header_secret
|
129 |
})
|
130 |
print(res.status_code)
|
131 |
+
return f"{logPrefix}图片上传成功,URL:{oss_url}"
|
132 |
else:
|
133 |
+
return f"{logPrefix}上传到 OSS 失败"
|
134 |
else:
|
135 |
+
print(f'{logPrefix}不上传OSS')
|
136 |
with open(output_image_path,'rb') as f:
|
137 |
image_base64 = base64.b64encode(f.read())
|
138 |
+
print(f"{logPrefix}运行完成")
|
139 |
delete_local_file(output_image_path) # 删除压缩后的图片
|
140 |
return image_base64
|
141 |
else:
|