WeShop commited on
Commit
e422362
·
1 Parent(s): a40c321
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -52,9 +52,10 @@ def generate_signature(key, did, timestamp):
52
  return h.hexdigest()
53
 
54
 
55
- def url_to_image(url):
56
  headers = {
57
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
 
58
  }
59
  try:
60
  response = requests.get(url, headers=headers, timeout=30)
@@ -66,11 +67,12 @@ def url_to_image(url):
66
  return None
67
 
68
 
69
- def start_task(task_id, timestamp, signature, did):
70
  headers = {
71
  'Did': did,
72
  'X-Timestamp': timestamp,
73
- 'X-Signature': signature
 
74
  }
75
  data = {
76
  "agentVersion": agent_version,
@@ -83,11 +85,12 @@ def start_task(task_id, timestamp, signature, did):
83
  return data, msg
84
 
85
 
86
- def create_task(image_url, timestamp, signature, did):
87
  headers = {
88
  'Did': did,
89
  'X-Timestamp': timestamp,
90
- 'X-Signature': signature
 
91
  }
92
  data = {
93
  "agentVersion": agent_version,
@@ -99,11 +102,12 @@ def create_task(image_url, timestamp, signature, did):
99
  return data, msg
100
 
101
 
102
- def save_task(image_url, timestamp, signature, show_image, task_id, location_id, did):
103
  headers = {
104
  'Did': did,
105
  'X-Timestamp': timestamp,
106
- 'X-Signature': signature
 
107
  }
108
  data = {
109
  "agentVersion": agent_version,
@@ -118,11 +122,12 @@ def save_task(image_url, timestamp, signature, show_image, task_id, location_id,
118
  return data, msg
119
 
120
 
121
- def query_task(task_id, execution_id, timestamp, signature, did):
122
  headers = {
123
  'Did': did,
124
  'X-Timestamp': timestamp,
125
- 'X-Signature': signature
 
126
  }
127
  data = {
128
  "agentVersion": agent_version,
@@ -135,7 +140,7 @@ def query_task(task_id, execution_id, timestamp, signature, did):
135
  return data, msg
136
 
137
 
138
- def upload_image(image, signature, timestamp, upload_type, did):
139
  if image is None:
140
  return None
141
  if upload_type == 'image':
@@ -151,7 +156,8 @@ def upload_image(image, signature, timestamp, upload_type, did):
151
  headers = {
152
  'Did': did,
153
  'X-Timestamp': timestamp,
154
- 'X-Signature': signature
 
155
  }
156
  response = requests.post(base_url + upload_url, files=files, headers=headers)
157
  data, msg = parse_response(response)
@@ -164,13 +170,15 @@ def load_description(file_path):
164
  return content
165
 
166
 
167
- def generate_image(main_image, background_image, did):
168
  if not did:
169
  did = str(uuid.uuid4())
170
  if main_image is None or background_image is None:
171
  m = "Please upload both the main image and the background reference image before generating."
172
  return gr.Warning(m), did
173
 
 
 
174
  timestamp = str(int(time.time()))
175
  signature = generate_signature(
176
  key=secret_key,
@@ -182,7 +190,8 @@ def generate_image(main_image, background_image, did):
182
  signature=signature,
183
  timestamp=timestamp,
184
  upload_type='image',
185
- did=did
 
186
  )
187
  if not upload_image_data:
188
  return gr.Warning(upload_image_msg), did
@@ -194,7 +203,8 @@ def generate_image(main_image, background_image, did):
194
  image_url=image_url,
195
  timestamp=timestamp,
196
  signature=signature,
197
- did=did
 
198
  )
199
  if not create_task_data:
200
  return gr.Warning(create_task_msg), did
@@ -208,7 +218,8 @@ def generate_image(main_image, background_image, did):
208
  signature=signature,
209
  timestamp=timestamp,
210
  upload_type='background_image',
211
- did=did
 
212
  )
213
  if not upload_image_data:
214
  return gr.Warning(upload_image_msg), did
@@ -220,7 +231,8 @@ def generate_image(main_image, background_image, did):
220
  show_image=show_image,
221
  task_id=task_id,
222
  location_id=upload_image_data,
223
- did=did
 
224
  )
225
  if not save_task_data:
226
  return gr.Warning(save_task_msg), did
@@ -232,7 +244,8 @@ def generate_image(main_image, background_image, did):
232
  task_id=save_task_id,
233
  timestamp=timestamp,
234
  signature=signature,
235
- did=did
 
236
  )
237
  if not start_task_data:
238
  return gr.Warning(start_task_msg), did
@@ -248,7 +261,8 @@ def generate_image(main_image, background_image, did):
248
  execution_id=execution_id,
249
  timestamp=timestamp,
250
  signature=signature,
251
- did=did
 
252
  )
253
  if not query_task_data:
254
  return gr.Warning(query_task_msg), did
@@ -264,7 +278,7 @@ def generate_image(main_image, background_image, did):
264
  elif status == "Success" or status == "Blocked":
265
  img = results[0].get("image")
266
  if img and str(img).strip() != "":
267
- return url_to_image(img), did
268
  end_time = int(time.time())
269
  if end_time - start_time > 3600:
270
  m = 'Query task timeout.'
 
52
  return h.hexdigest()
53
 
54
 
55
+ def url_to_image(url, ip):
56
  headers = {
57
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
58
+ 'X-Forwarded-For': ip
59
  }
60
  try:
61
  response = requests.get(url, headers=headers, timeout=30)
 
67
  return None
68
 
69
 
70
+ def start_task(task_id, timestamp, signature, did, ip):
71
  headers = {
72
  'Did': did,
73
  'X-Timestamp': timestamp,
74
+ 'X-Signature': signature,
75
+ 'X-Forwarded-For': ip
76
  }
77
  data = {
78
  "agentVersion": agent_version,
 
85
  return data, msg
86
 
87
 
88
+ def create_task(image_url, timestamp, signature, did, ip):
89
  headers = {
90
  'Did': did,
91
  'X-Timestamp': timestamp,
92
+ 'X-Signature': signature,
93
+ 'X-Forwarded-For': ip
94
  }
95
  data = {
96
  "agentVersion": agent_version,
 
102
  return data, msg
103
 
104
 
105
+ def save_task(image_url, timestamp, signature, show_image, task_id, location_id, did, ip):
106
  headers = {
107
  'Did': did,
108
  'X-Timestamp': timestamp,
109
+ 'X-Signature': signature,
110
+ 'X-Forwarded-For': ip
111
  }
112
  data = {
113
  "agentVersion": agent_version,
 
122
  return data, msg
123
 
124
 
125
+ def query_task(task_id, execution_id, timestamp, signature, did, ip):
126
  headers = {
127
  'Did': did,
128
  'X-Timestamp': timestamp,
129
+ 'X-Signature': signature,
130
+ 'X-Forwarded-For': ip
131
  }
132
  data = {
133
  "agentVersion": agent_version,
 
140
  return data, msg
141
 
142
 
143
+ def upload_image(image, signature, timestamp, upload_type, did, ip):
144
  if image is None:
145
  return None
146
  if upload_type == 'image':
 
156
  headers = {
157
  'Did': did,
158
  'X-Timestamp': timestamp,
159
+ 'X-Signature': signature,
160
+ 'X-Forwarded-For': ip
161
  }
162
  response = requests.post(base_url + upload_url, files=files, headers=headers)
163
  data, msg = parse_response(response)
 
170
  return content
171
 
172
 
173
+ def generate_image(main_image, background_image, did, request: gr.Request):
174
  if not did:
175
  did = str(uuid.uuid4())
176
  if main_image is None or background_image is None:
177
  m = "Please upload both the main image and the background reference image before generating."
178
  return gr.Warning(m), did
179
 
180
+ client_ip = request.client.host
181
+
182
  timestamp = str(int(time.time()))
183
  signature = generate_signature(
184
  key=secret_key,
 
190
  signature=signature,
191
  timestamp=timestamp,
192
  upload_type='image',
193
+ did=did,
194
+ ip=client_ip
195
  )
196
  if not upload_image_data:
197
  return gr.Warning(upload_image_msg), did
 
203
  image_url=image_url,
204
  timestamp=timestamp,
205
  signature=signature,
206
+ did=did,
207
+ ip=client_ip
208
  )
209
  if not create_task_data:
210
  return gr.Warning(create_task_msg), did
 
218
  signature=signature,
219
  timestamp=timestamp,
220
  upload_type='background_image',
221
+ did=did,
222
+ ip=client_ip
223
  )
224
  if not upload_image_data:
225
  return gr.Warning(upload_image_msg), did
 
231
  show_image=show_image,
232
  task_id=task_id,
233
  location_id=upload_image_data,
234
+ did=did,
235
+ ip=client_ip
236
  )
237
  if not save_task_data:
238
  return gr.Warning(save_task_msg), did
 
244
  task_id=save_task_id,
245
  timestamp=timestamp,
246
  signature=signature,
247
+ did=did,
248
+ ip=client_ip
249
  )
250
  if not start_task_data:
251
  return gr.Warning(start_task_msg), did
 
261
  execution_id=execution_id,
262
  timestamp=timestamp,
263
  signature=signature,
264
+ did=did,
265
+ ip=client_ip
266
  )
267
  if not query_task_data:
268
  return gr.Warning(query_task_msg), did
 
278
  elif status == "Success" or status == "Blocked":
279
  img = results[0].get("image")
280
  if img and str(img).strip() != "":
281
+ return url_to_image(img, ip=client_ip), did
282
  end_time = int(time.time())
283
  if end_time - start_time > 3600:
284
  m = 'Query task timeout.'