wsj1995 commited on
Commit
c2dd9ba
·
verified ·
1 Parent(s): 9fb0d37

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from PIL import Image
3
+ import oss2
4
+ import os
5
+ import gradio as gr
6
+ import uuid
7
+ import base64
8
+ from oss2.credentials import EnvironmentVariableCredentialsProvider,StaticCredentialsProvider
9
+
10
+ # 下载网络图片
11
+ def download_image(url):
12
+ # 通过 URL 下载图片
13
+ response = requests.get(url)
14
+ if response.status_code == 200:
15
+ return Image.open(BytesIO(response.content))
16
+ else:
17
+ raise Exception(f"Failed to download image, status code: {response.status_code}")
18
+
19
+
20
+ def remove_metadata(image, output_path=None):
21
+ # 获取图片的格式
22
+ img_format = image.format.lower()
23
+
24
+ # 如果没有指定输出路径,自动生成路径
25
+ if output_path is None:
26
+ output_path = f"downloaded_image_no_metadata.{img_format}"
27
+
28
+ # 根据图片的格式保存图片,不包含元数据
29
+ image.save(output_path, img_format)
30
+
31
+ return output_path
32
+
33
+ # 上传到阿里云 OSS
34
+ def upload_to_oss(file_path, object_name, bucket_name, access_key_id, access_key_secret, security_token, endpoint):
35
+ try:
36
+ auth = oss2.ProviderAuthV4(StaticCredentialsProvider(access_key_id,access_key_secret,security_token))
37
+ # auth = oss2.ProviderAuthV4(access_key_id, access_key_secret, security_token)
38
+ # 填写Bucket名称。
39
+ bucket = oss2.Bucket(auth, f'https://{endpoint}', bucket_name, region=endpoint.split('.')[0].split('-', 1)[1])
40
+ result = bucket.put_object_from_file(object_name, file_path)
41
+ # 返回上传的文件 URL
42
+ return f"https://{bucket_name}.{endpoint}/{object_name}"
43
+ except oss2.exceptions.OssError as e:
44
+ print(f"OSS上传失败: {e}")
45
+ return None
46
+
47
+ # 删除本地文件
48
+ def delete_local_file(file_path):
49
+ if os.path.exists(file_path):
50
+ os.remove(file_path)
51
+ print(f"{file_path} 已删除。")
52
+ else:
53
+ print(f"{file_path} 文件不存在。")
54
+
55
+
56
+ def run(url,quality,output_format,storage,access_key_id,access_key_secret,securityToken,endpoint,bucket_name,upload_filename=uuid.uuid4()):
57
+
58
+ # 网络图片 URL 和本地保存路径
59
+ tmp_file_name = uuid.uuid4()
60
+ temp_original = f'{tmp_file_name}_original_image.jpg'
61
+ temp_compressed = f'{tmp_file_name}_compressed_image.jpg'
62
+
63
+ # 步骤 1:下载图片到本地
64
+ downloaded_image = download_image(url)
65
+
66
+ if downloaded_image:
67
+ if access_key_id and endpoint and bucket_name:
68
+ # 步骤 3:上传到阿里云 OSS
69
+ oss_url = upload_to_oss(compressed_image, upload_filename, bucket_name, access_key_id, access_key_secret,securityToken, endpoint)
70
+
71
+ # 步骤 4:删除本地的临时文件
72
+ delete_local_file(temp_original) # 删除原始图片
73
+ delete_local_file(temp_compressed) # 删除压缩后的图片
74
+ if oss_url:
75
+ return f"图片上传成功,URL:{oss_url}"
76
+ else:
77
+ return "上传到 OSS 失败"
78
+ else:
79
+ with open(compressed_image,'rb') as f:
80
+ image_base64 = base64.b64encode(f.read())
81
+ delete_local_file(temp_original) # 删除原始图片
82
+ delete_local_file(temp_compressed) # 删除压缩后的图片
83
+ return image_base64
84
+ else:
85
+ return '图片下载失败'
86
+
87
+ with gr.Blocks() as demo:
88
+
89
+ with gr.Column(elem_id="col-container"):
90
+ with gr.Row():
91
+ url = gr.Text(
92
+ label="图片地址",
93
+ show_label=False,
94
+ max_lines=1,
95
+ placeholder="input image url",
96
+ container=False,
97
+ )
98
+ with gr.Row():
99
+ access_key_id = gr.Textbox(
100
+ label="AccessKeyId",
101
+ placeholder="云存储 AccessKeyId",
102
+ )
103
+ with gr.Row():
104
+ access_key_secret = gr.Textbox(
105
+ label="AccessKeySecret",
106
+ placeholder="云存储 AccessKeySecret",
107
+ )
108
+ with gr.Row():
109
+ securityToken = gr.Textbox(
110
+ label="SecurityToken",
111
+ placeholder="云存储 securityToken",
112
+ )
113
+ with gr.Row():
114
+ endpoint = gr.Textbox(
115
+ label="Endpoint",
116
+ placeholder="云存储 endpoint",
117
+ )
118
+ with gr.Row():
119
+ bucket_name = gr.Textbox(
120
+ label="BucketName",
121
+ placeholder="云存储 bucket_name",
122
+ )
123
+ with gr.Row():
124
+ upload_filename = gr.Textbox(
125
+ label="uploadFielName",
126
+ placeholder="云存储 文件名称",
127
+ )
128
+
129
+ with gr.Row():
130
+ output = gr.Textbox(
131
+ label="Output",
132
+ placeholder="Result will be displayed here",
133
+ lines=10,
134
+ interactive=False
135
+ )
136
+
137
+ with gr.Row():
138
+ run_button = gr.Button("Run", scale=0)
139
+
140
+ gr.on(
141
+ triggers=[run_button.click],
142
+ fn = run,
143
+ inputs = [
144
+ url,
145
+ access_key_id,
146
+ access_key_secret,
147
+ securityToken,
148
+ endpoint,
149
+ bucket_name,
150
+ upload_filename
151
+ ],
152
+ outputs = [output]
153
+ )
154
+
155
+ demo.queue().launch()