Update app.py
Browse files
app.py
CHANGED
@@ -23,13 +23,53 @@ JS_FUNC = os.getenv("JS_FUNC")
|
|
23 |
def base64_to_image(base64_str):
|
24 |
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
try:
|
28 |
file_1 = handle_file(file)
|
29 |
except Exception as e:
|
30 |
gr.Info("Please upload an image file.")
|
31 |
return []
|
32 |
|
|
|
|
|
|
|
|
|
33 |
result_text = backend.predict(
|
34 |
file=file_1,
|
35 |
token=token,
|
@@ -46,7 +86,7 @@ def search_face(file, token=None):
|
|
46 |
outarray.append((image, item['url']))
|
47 |
|
48 |
if result['status'] == 201:
|
49 |
-
gr.Info(STATUS_MESSAGES[result['status']], duration=
|
50 |
return outarray
|
51 |
|
52 |
def export_images(items):
|
|
|
23 |
def base64_to_image(base64_str):
|
24 |
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
25 |
|
26 |
+
user_attempts = {}
|
27 |
+
def clear_old_entries():
|
28 |
+
today = datetime.now().date()
|
29 |
+
# Create a list of keys to remove
|
30 |
+
keys_to_remove = [key for key, value in user_attempts.items() if value != today]
|
31 |
+
# Remove old entries
|
32 |
+
for key in keys_to_remove:
|
33 |
+
del user_attempts[key]
|
34 |
+
|
35 |
+
def if_limited(request):
|
36 |
+
clear_old_entries()
|
37 |
+
user_ip = None
|
38 |
+
if request.headers.get("x-forwarded-for"):
|
39 |
+
user_ip = request.headers["x-forwarded-for"].split(",")[0] # First IP in the list
|
40 |
+
|
41 |
+
cookie_value = request.headers.get("cookie", "")
|
42 |
+
if "user_id=" in cookie_value:
|
43 |
+
user_id = cookie_value.split("user_id=")[1].split(";")[0]
|
44 |
+
else:
|
45 |
+
user_id = None
|
46 |
+
print("##### Coming", user_id, user_ip)
|
47 |
+
# Get today's date
|
48 |
+
today = datetime.now().date()
|
49 |
+
|
50 |
+
# Check if the user has already tried today (by IP or cookie)
|
51 |
+
for key, value in user_attempts.items():
|
52 |
+
if (key == user_ip or key == user_id) and value == today:
|
53 |
+
return True
|
54 |
+
|
55 |
+
# Record the attempt (store both hashed IP and hashed cookie)
|
56 |
+
if user_ip:
|
57 |
+
user_attempts[user_ip] = today
|
58 |
+
if user_id:
|
59 |
+
user_attempts[user_id] = today
|
60 |
+
return False
|
61 |
+
|
62 |
+
def search_face(file, token, request: gr.Request):
|
63 |
try:
|
64 |
file_1 = handle_file(file)
|
65 |
except Exception as e:
|
66 |
gr.Info("Please upload an image file.")
|
67 |
return []
|
68 |
|
69 |
+
if token == "" and if_limited(request):
|
70 |
+
gr.Info("Your free credit for today has been used. Buy Premium Token.", duration=12)
|
71 |
+
return []
|
72 |
+
|
73 |
result_text = backend.predict(
|
74 |
file=file_1,
|
75 |
token=token,
|
|
|
86 |
outarray.append((image, item['url']))
|
87 |
|
88 |
if result['status'] == 201:
|
89 |
+
gr.Info(STATUS_MESSAGES[result['status']], duration=12)
|
90 |
return outarray
|
91 |
|
92 |
def export_images(items):
|