Zhu-FaceOnLive commited on
Commit
c95dcd6
Β·
verified Β·
1 Parent(s): 91bd18b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -15
app.py CHANGED
@@ -4,18 +4,43 @@ import requests
4
  import base64
5
  from io import BytesIO
6
  from PIL import Image
 
7
 
8
- count = 0
9
  def image_to_base64(image):
10
  buffered = BytesIO()
11
  image.save(buffered, format="JPEG", quality=90)
12
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
13
 
14
  def base64_to_image(base64_str):
15
- return Image.open(BytesIO(base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def search_face(file, token):
18
- global count
19
  free_url = os.environ.get("SERVER_URL_FREE")
20
  premium_url = os.environ.get("SERVER_URL_PREMIUM")
21
  token_server_url = os.environ.get("TOKEN_URL")
@@ -54,32 +79,28 @@ def search_face(file, token):
54
  elif status_code == 402:
55
  gr.Info("Wrong request.")
56
  elif status_code == 403:
57
- gr.Info("Requests all used in your token.")
58
  elif status_code == 404:
59
  gr.Info("Timeout, try again.")
60
 
61
  if status_code > 300:
62
- return [], count
63
 
64
  try:
65
  res = r.json().get('img_array')
66
- out_array = []
67
  suffix = "*********"
68
  if url == premium_url:
69
  suffix = ""
70
- for item in res:
71
- out_array.append((base64_to_image(item["image"]), item["url"] + suffix))
72
- count += 1
73
 
74
  if url == premium_url:
75
  try:
76
  r = requests.post(url=token_server_url + '/activate-token', json={"token": token})
77
- #if r.json().get('status') == 'success':
78
- # url = premium_url
79
  except:
80
  raise gr.Error("Invalid token!")
81
 
82
- return out_array, count
83
  except:
84
  raise gr.Error("Try again.")
85
 
@@ -106,14 +127,14 @@ with gr.Blocks(css=custom_css) as demo:
106
  image = gr.Image(type='filepath', height=480)
107
  token = gr.Textbox(placeholder="(Optional) Premium Token via the link below.", label="Premium Token")
108
  gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb' target='_blank'>Get Premium Token: Perform Deep Search & Full URLs</a>")
 
109
  search_face_button = gr.Button("Search Face")
110
  with gr.Column(scale=2):
111
  output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
112
- countwg = gr.Number(label="Count")
113
 
114
- gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, cache_mode='lazy', fn=search_face_free, outputs=[output, countwg])
115
 
116
- search_face_button.click(search_face, inputs=[image, token], outputs=[output, countwg], api_name=False)
117
 
118
  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>')
119
 
 
4
  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()
11
  image.save(buffered, format="JPEG", quality=90)
12
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
13
 
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")
 
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
 
 
127
  image = gr.Image(type='filepath', height=480)
128
  token = gr.Textbox(placeholder="(Optional) Premium Token via the link below.", label="Premium Token")
129
  gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb' target='_blank'>Get Premium Token: Perform Deep Search & Full URLs</a>")
130
+ gr.HTML("<a href='https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb' target='_blank'>Send DMCA & GDPR Takedown Notices On Your Behalf, Opt-Out From Search</a>")
131
  search_face_button = gr.Button("Search Face")
132
  with gr.Column(scale=2):
133
  output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
 
134
 
135
+ gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, cache_mode='lazy', fn=search_face_free, outputs=[output])
136
 
137
+ search_face_button.click(search_face, inputs=[image, token], outputs=[output], api_name=False)
138
 
139
  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>')
140