Zhu-FaceOnLive commited on
Commit
3b17d42
Β·
verified Β·
1 Parent(s): 4d188a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -14,9 +14,22 @@ def image_to_base64(image):
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):
18
  global count
19
- url = os.environ.get("SERVER_URL")
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  try:
22
  image = Image.open(file)
@@ -51,15 +64,35 @@ def search_face(file):
51
  try:
52
  res = r.json().get('img_array')
53
  out_array = []
 
 
 
54
  for item in res:
55
- out_array.append((base64_to_image(item["image"]), item["url"] + "*********"))
56
- count += 1
 
 
 
 
 
 
 
 
 
57
  return out_array, count
58
  except:
59
  raise gr.Error("Try again.")
60
 
 
 
61
 
62
- with gr.Blocks() as demo:
 
 
 
 
 
 
63
  gr.Markdown(
64
  """
65
  # Search Your Face Online For Free
@@ -78,9 +111,9 @@ with gr.Blocks() as demo:
78
  output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
79
  countwg = gr.Number(label="Count")
80
 
81
- gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, cache_mode='lazy', fn=search_face, outputs=[output, countwg])
82
 
83
- search_face_button.click(search_face, inputs=image, outputs=[output, countwg], api_name=False)
84
 
85
  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>')
86
 
 
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")
22
+
23
+ url = free_url
24
+ if token:
25
+ try:
26
+ r = requests.post(url=token_server_url + '/verify-token', json={"token": token})
27
+ if r.json().get('status') == 'success':
28
+ url = premium_url
29
+ else:
30
+ raise gr.Error("Invalid token! For free search, use empty string for token")
31
+ except:
32
+ raise gr.Error("Invalid token!")
33
 
34
  try:
35
  image = Image.open(file)
 
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
 
86
+ def search_face_free(file):
87
+ search_face(file, '')
88
 
89
+ custom_css = """
90
+ caption.caption {
91
+ user-select: text;
92
+ cursor: text;
93
+ }
94
+ """
95
+ with gr.Blocks(css=custom_css) as demo:
96
  gr.Markdown(
97
  """
98
  # Search Your Face Online For Free
 
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