Zhu-FaceOnLive commited on
Commit
00847f1
Β·
verified Β·
1 Parent(s): df2ac0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -69
app.py CHANGED
@@ -5,6 +5,27 @@ import base64
5
  from io import BytesIO
6
  from PIL import Image
7
  import hashlib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def image_to_base64(image):
10
  buffered = BytesIO()
@@ -14,98 +35,105 @@ def image_to_base64(image):
14
  def base64_to_image(base64_str):
15
  return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
16
 
17
- def check_db(img_array, suffix, token_server_url):
18
  hashes = []
19
  out_array = []
20
  for item in img_array:
21
  try:
22
  image_data = base64_to_image(item["image"])
23
-
24
- hash_object = hashlib.sha256(image_data)
25
- image_hash = hash_object.hexdigest()
26
-
27
  hashes.append(image_hash)
28
  out_array.append((Image.open(BytesIO(image_data)), item["url"] + suffix))
29
-
30
  except base64.binascii.Error as e:
31
  raise ValueError(f"Invalid base64 string: {str(e)}")
32
  except Exception as e:
33
  raise ValueError(f"Error processing image: {str(e)}")
34
 
35
  try:
36
- r = requests.post(url=token_server_url + '/lookup-hashes', json={"hashes": hashes})
37
- out_array = [value for i, value in enumerate(out_array) if i not in r.json().get('res')]
38
  except:
39
  raise gr.Error("Token Server Error!")
40
 
41
  return out_array
42
 
43
- def search_face(file, token):
44
- free_url = os.environ.get("SERVER_URL_FREE")
45
- premium_url = os.environ.get("SERVER_URL_PREMIUM")
46
- token_server_url = os.environ.get("TOKEN_URL")
47
-
48
- url = free_url
49
- if token:
50
- try:
51
- r = requests.post(url=token_server_url + '/verify-token', json={"token": token})
52
- if r.json().get('status') == 'success':
53
- url = premium_url
54
- else:
55
- raise gr.Error("Invalid token! For free search, use empty string for token")
56
- except:
57
- raise gr.Error("Invalid token!")
58
 
 
59
  try:
60
- image = Image.open(file)
61
- image_base64 = image_to_base64(image)
62
- r = requests.post(url=url, headers={"X-RapidAPI-Key": os.environ.get("API_KEY")}, json={"image": image_base64})
63
  except:
64
- raise gr.Error("Please select image file!")
65
-
66
- status_code = r.status_code
67
- if status_code == 301:
68
- gr.Info("Too many faces in the photo.")
69
- elif status_code == 302:
70
- gr.Info("Face is not clear enough.")
71
- elif status_code == 303:
72
- gr.Info("No matches found. Try Deep Search with Premium Token.")
73
- elif status_code == 304:
74
- gr.Info("No face in the photo.")
75
- elif status_code == 305:
76
- gr.Info("Search blocked for privacy issue.")
77
- elif status_code == 401:
78
- gr.Info("Invalid image format.")
79
- elif status_code == 402:
80
- gr.Info("Wrong request.")
81
- elif status_code == 403:
82
- gr.Info("Please try again later.")
83
- elif status_code == 404:
84
- gr.Info("Timeout, try again.")
85
-
86
- if status_code > 300:
87
- return []
88
-
89
- try:
90
- res = r.json().get('img_array')
91
- suffix = "*********"
92
- if url == premium_url:
93
- suffix = ""
94
-
95
- out_array = check_db(res, suffix, token_server_url)
96
-
97
- if url == premium_url:
98
- try:
99
- r = requests.post(url=token_server_url + '/activate-token', json={"token": token})
100
- except:
101
- raise gr.Error("Invalid token!")
 
 
102
 
103
  return out_array
104
  except:
105
  raise gr.Error("Try again.")
106
 
107
- def search_face_free(file):
108
- return search_face(file, '')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  custom_css = """
111
  caption.caption {
@@ -165,17 +193,17 @@ with gr.Blocks(css=custom_css, head=head) as demo:
165
  with gr.Column(scale=1):
166
  image = gr.Image(type='filepath', height=480)
167
  token = gr.Textbox(placeholder="(Optional) Get Premium Token Below.", label="Premium Token")
168
- gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb' target='_blank'>Get Premium Token: Perform Deep Search & Full URLs</a>")
169
  gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676d7e7597f8b3b699f84820' target='_blank'>Protect Your Privacy – Start Your Takedown Now</a>")
170
  search_face_button = gr.Button("Search Face")
171
  with gr.Column(scale=2):
172
  output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
173
 
174
- gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, cache_mode='lazy', fn=search_face_free, outputs=[output])
175
 
176
  search_face_button.click(search_face, inputs=[image, token], outputs=[output], api_name=False)
177
 
178
- gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Search-Online"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Search-Online&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
179
  html = gr.HTML()
180
  demo.load(None, inputs=None, outputs=html, js=js)
181
 
 
5
  from io import BytesIO
6
  from PIL import Image
7
  import hashlib
8
+ import concurrent.futures
9
+
10
+ # Constants
11
+ FREE_URL = os.environ.get("SERVER_URL_FREE")
12
+ PREMIUM_URL = os.environ.get("SERVER_URL_PREMIUM")
13
+ PREMIUM_URL2 = os.environ.get("SERVER_URL_PREMIUM2")
14
+ TOKEN_SERVER_URL = os.environ.get("TOKEN_URL")
15
+ API_KEY = os.environ.get("API_KEY")
16
+
17
+ STATUS_MESSAGES = {
18
+ 301: "Too many faces in the photo.",
19
+ 302: "Face is not clear enough.",
20
+ 303: "No matches found.",
21
+ 304: "No face in the photo.",
22
+ 305: "Search blocked for privacy issue.",
23
+ 401: "Invalid image format.",
24
+ 402: "Wrong request.",
25
+ 403: "Please try again later.",
26
+ 404: "Timeout, try again."
27
+ }
28
+ FREE_SUFFIX = "*********"
29
 
30
  def image_to_base64(image):
31
  buffered = BytesIO()
 
35
  def base64_to_image(base64_str):
36
  return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
37
 
38
+ def check_db(img_array, suffix):
39
  hashes = []
40
  out_array = []
41
  for item in img_array:
42
  try:
43
  image_data = base64_to_image(item["image"])
44
+ image_hash = hashlib.sha256(image_data).hexdigest()
 
 
 
45
  hashes.append(image_hash)
46
  out_array.append((Image.open(BytesIO(image_data)), item["url"] + suffix))
 
47
  except base64.binascii.Error as e:
48
  raise ValueError(f"Invalid base64 string: {str(e)}")
49
  except Exception as e:
50
  raise ValueError(f"Error processing image: {str(e)}")
51
 
52
  try:
53
+ response = requests.post(url=TOKEN_SERVER_URL + '/lookup-hashes', json={"hashes": hashes})
54
+ out_array = [value for i, value in enumerate(out_array) if i not in response.json().get('res')]
55
  except:
56
  raise gr.Error("Token Server Error!")
57
 
58
  return out_array
59
 
60
+ def verify_token(token):
61
+ try:
62
+ response = requests.post(url=TOKEN_SERVER_URL + '/verify-token', json={"token": token})
63
+ if response.json().get('status') == 'success':
64
+ return PREMIUM_URL
65
+ else:
66
+ raise gr.Error("Invalid token! For free search, use empty string for token")
67
+ except:
68
+ raise gr.Error("Invalid token!")
 
 
 
 
 
 
69
 
70
+ def activate_token(token):
71
  try:
72
+ requests.post(url=TOKEN_SERVER_URL + '/activate-token', json={"token": token})
 
 
73
  except:
74
+ raise gr.Error("Invalid token!")
75
+
76
+ def get_image_base64(file):
77
+ try:
78
+ image = Image.open(file)
79
+ return image_to_base64(image)
80
+ except Exception as e:
81
+ raise gr.Error("Please select image file!")
82
+
83
+ def send_requests(url, file):
84
+ with concurrent.futures.ThreadPoolExecutor() as executor:
85
+ future1 = executor.submit(requests.post, url, headers={"X-RapidAPI-Key": API_KEY}, json={"image": get_image_base64(file)})
86
+ if url == PREMIUM_URL:
87
+ future2 = executor.submit(requests.post, PREMIUM_URL2, files={"image": open(file, 'rb')})
88
+ response1 = future1.result()
89
+ response2 = future2.result()
90
+ return response1, response2
91
+ else:
92
+ response1 = future1.result()
93
+ return response1, None
94
+
95
+ def process_response(response, soc_res, url, token):
96
+ status_code = response.status_code
97
+
98
+ if not soc_res:
99
+ if status_code in STATUS_MESSAGES:
100
+ gr.Info(STATUS_MESSAGES[status_code])
101
+
102
+ if status_code > 300:
103
+ return []
104
+
105
+ try:
106
+ res = response.json().get('img_array')
107
+ if soc_res:
108
+ res = soc_res[0] + res + soc_res[1]
109
+ suffix = "" if url == PREMIUM_URL else FREE_SUFFIX
110
+ out_array = check_db(res, suffix)
111
+
112
+ if url == PREMIUM_URL:
113
+ activate_token(token)
114
 
115
  return out_array
116
  except:
117
  raise gr.Error("Try again.")
118
 
119
+ def process_response2(response):
120
+ if response.status_code == 200:
121
+ part1 = response.json().get('part1')
122
+ part2 = response.json().get('part2')
123
+
124
+ if not part1 and not part2:
125
+ return None
126
+ return (part1, part2)
127
+ return None
128
+
129
+ def search_face(file, token=None):
130
+ url = FREE_URL
131
+ if token:
132
+ url = verify_token(token)
133
+
134
+ response1, response2 = send_requests(url, file)
135
+ soc_res = process_response2(response2) if response2 else None
136
+ return process_response(response1, soc_res, url, token)
137
 
138
  custom_css = """
139
  caption.caption {
 
193
  with gr.Column(scale=1):
194
  image = gr.Image(type='filepath', height=480)
195
  token = gr.Textbox(placeholder="(Optional) Get Premium Token Below.", label="Premium Token")
196
+ gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb' target='_blank'>Get Premium Token: Deep Search Including Social Media & Full URL Reveal</a>")
197
  gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676d7e7597f8b3b699f84820' target='_blank'>Protect Your Privacy – Start Your Takedown Now</a>")
198
  search_face_button = gr.Button("Search Face")
199
  with gr.Column(scale=2):
200
  output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
201
 
202
+ gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, cache_mode='lazy', fn=search_face, outputs=[output])
203
 
204
  search_face_button.click(search_face, inputs=[image, token], outputs=[output], api_name=False)
205
 
206
+ gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Search-Online"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Search-Online&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
207
  html = gr.HTML()
208
  demo.load(None, inputs=None, outputs=html, js=js)
209