Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -23,30 +23,81 @@ def base64_to_image(base64_str):
|
|
23 |
base64_str = base64_str.split(";base64,")[1].strip()
|
24 |
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
temp_file.write(base64_to_image(base64_image))
|
30 |
file = temp_file.name
|
31 |
-
handle_file(file)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
return result_text
|
51 |
|
52 |
with gr.Blocks(css=CSS, head=HEAD_HTML, title="DeepSeek? FaceSeek!") as iface:
|
|
|
23 |
base64_str = base64_str.split(";base64,")[1].strip()
|
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 base64_to_image(base64_str):
|
63 |
+
if ";base64," in base64_str:
|
64 |
+
base64_str = base64_str.split(";base64,")[1].strip()
|
65 |
+
image_data = base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
66 |
+
image = Image.open(BytesIO(image_data)).convert('RGB')
|
67 |
+
buffer = BytesIO()
|
68 |
+
image.save(buffer, format="JPEG", quality=90)
|
69 |
+
buffer.seek(0)
|
70 |
+
return buffer.read()
|
71 |
+
|
72 |
+
def search_image(base64_image, token_txt, request: gr.Request):
|
73 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
74 |
+
try:
|
75 |
temp_file.write(base64_to_image(base64_image))
|
76 |
file = temp_file.name
|
77 |
+
file1 = handle_file(file)
|
78 |
+
except Exception as e:
|
79 |
+
print(e)
|
80 |
+
gr.Info("Please upload an image file.")
|
81 |
+
return []
|
82 |
+
|
83 |
+
if token_txt == "" and if_limited(request):
|
84 |
+
gr.Info("⏳ Wait for your next free search, or 🚀 Go Premium Search at https://faceseek.online!", duration=10)
|
85 |
+
return []
|
86 |
+
|
87 |
+
if token_txt == "KSB311":
|
88 |
+
token_txt = ""
|
89 |
+
|
90 |
+
result_text = backend.predict(
|
91 |
+
file=file1,
|
92 |
+
token=token_txt,
|
93 |
+
api_name="/search_face"
|
94 |
+
)
|
95 |
|
96 |
+
result = json.loads(result_text)
|
97 |
+
outarray = []
|
98 |
+
if result['status'] > 300:
|
99 |
+
gr.Info(STATUS_MESSAGES[result['status']], duration=10)
|
100 |
+
return '{"result": []}'
|
101 |
return result_text
|
102 |
|
103 |
with gr.Blocks(css=CSS, head=HEAD_HTML, title="DeepSeek? FaceSeek!") as iface:
|