Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,129 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
from io import BytesIO
|
4 |
-
import zipfile
|
5 |
-
import tempfile
|
6 |
-
import random
|
7 |
-
import json
|
8 |
-
from gradio_client import Client, handle_file
|
9 |
-
import base64
|
10 |
-
from PIL import Image
|
11 |
-
from datetime import datetime
|
12 |
-
from io import BytesIO
|
13 |
-
|
14 |
-
BACKEND = os.getenv("BACKEND")
|
15 |
-
JS2 = os.getenv("JS2")
|
16 |
-
MAIN_HTML = os.getenv("MAIN_HTML")
|
17 |
-
MAIN_JS = os.getenv("MAIN_JS")
|
18 |
-
HEAD_HTML = os.getenv("HEAD_HTML")
|
19 |
-
CSS = os.getenv("CSS")
|
20 |
-
USER = os.getenv("USER")
|
21 |
-
PASS = os.getenv("PASS")
|
22 |
-
|
23 |
-
STATUS_MESSAGES = {
|
24 |
-
301: "Invalid Premium ID!"
|
25 |
-
}
|
26 |
-
|
27 |
-
backend = Client(BACKEND, auth=[USER, PASS])
|
28 |
-
|
29 |
-
def base64_to_image(base64_str):
|
30 |
-
if ";base64," in base64_str:
|
31 |
-
base64_str = base64_str.split(";base64,")[1].strip()
|
32 |
-
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
33 |
-
|
34 |
-
user_attempts = {}
|
35 |
-
def clear_old_entries():
|
36 |
-
today = datetime.now().date()
|
37 |
-
# Create a list of keys to remove
|
38 |
-
keys_to_remove = [key for key, value in user_attempts.items() if value != today]
|
39 |
-
# Remove old entries
|
40 |
-
for key in keys_to_remove:
|
41 |
-
del user_attempts[key]
|
42 |
-
|
43 |
-
def if_limited(request):
|
44 |
-
clear_old_entries()
|
45 |
-
user_ip = None
|
46 |
-
if request.headers.get("x-forwarded-for"):
|
47 |
-
user_ip = request.headers["x-forwarded-for"].split(",")[0] # First IP in the list
|
48 |
-
|
49 |
-
cookie_value = request.headers.get("cookie", "")
|
50 |
-
if "user_id=" in cookie_value:
|
51 |
-
user_id = cookie_value.split("user_id=")[1].split(";")[0]
|
52 |
-
else:
|
53 |
-
user_id = None
|
54 |
-
print("##### Coming", user_id, user_ip)
|
55 |
-
# Get today's date
|
56 |
-
today = datetime.now().date()
|
57 |
-
|
58 |
-
# Check if the user has already tried today (by IP or cookie)
|
59 |
-
for key, value in user_attempts.items():
|
60 |
-
if (key == user_ip or key == user_id) and value == today:
|
61 |
-
return True
|
62 |
-
|
63 |
-
# Record the attempt (store both hashed IP and hashed cookie)
|
64 |
-
if user_ip:
|
65 |
-
user_attempts[user_ip] = today
|
66 |
-
if user_id:
|
67 |
-
user_attempts[user_id] = today
|
68 |
-
return False
|
69 |
-
|
70 |
-
def base64_to_image(base64_str):
|
71 |
-
if ";base64," in base64_str:
|
72 |
-
base64_str = base64_str.split(";base64,")[1].strip()
|
73 |
-
image_data = base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
74 |
-
image = Image.open(BytesIO(image_data)).convert('RGB')
|
75 |
-
buffer = BytesIO()
|
76 |
-
image.save(buffer, format="JPEG", quality=90)
|
77 |
-
buffer.seek(0)
|
78 |
-
return buffer.read()
|
79 |
-
|
80 |
-
def search_image(base64_image, token_txt, request: gr.Request):
|
81 |
-
global backend
|
82 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
83 |
-
try:
|
84 |
-
temp_file.write(base64_to_image(base64_image))
|
85 |
-
file = temp_file.name
|
86 |
-
file1 = handle_file(file)
|
87 |
-
except Exception as e:
|
88 |
-
print(e)
|
89 |
-
gr.Info("Please upload an image file.")
|
90 |
-
return []
|
91 |
-
|
92 |
-
if token_txt == "" and if_limited(request):
|
93 |
-
gr.Info("⏳ Wait for your next free search, or 🚀 Go Premium Search at https://faceseek.online!", duration=10)
|
94 |
-
return []
|
95 |
-
|
96 |
-
try:
|
97 |
-
result_text = backend.predict(
|
98 |
-
file=file1,
|
99 |
-
token=token_txt,
|
100 |
-
api_name="/search_face"
|
101 |
-
)
|
102 |
-
except:
|
103 |
-
backend = Client(BACKEND, auth=[USER, PASS])
|
104 |
-
result_text = backend.predict(
|
105 |
-
file=file1,
|
106 |
-
token=token_txt,
|
107 |
-
api_name="/search_face"
|
108 |
-
)
|
109 |
-
|
110 |
-
result = json.loads(result_text)
|
111 |
-
outarray = []
|
112 |
-
if result['status'] > 300:
|
113 |
-
gr.Info(STATUS_MESSAGES[result['status']], duration=10)
|
114 |
-
return '{"result": []}'
|
115 |
-
return result_text
|
116 |
-
|
117 |
-
with gr.Blocks(css=CSS, head=HEAD_HTML, title="FaceSeek - Face Search Online") as iface:
|
118 |
-
html = gr.HTML(MAIN_HTML, max_height=720)
|
119 |
-
base64_txt = gr.Textbox(label="Base64-Image", elem_id="base64_image", visible=False)
|
120 |
-
token_txt = gr.Textbox(label="Token-Text", elem_id="premium_token", visible=False)
|
121 |
-
out_txt = gr.Textbox(label="Result", visible=False)
|
122 |
-
|
123 |
-
search_button = gr.Button("🔍 Free Face Search", elem_id="search_btn", visible=False)
|
124 |
-
|
125 |
-
search_button.click(search_image, inputs=[base64_txt, token_txt], outputs=out_txt).success(None, inputs=[out_txt], js=JS2)
|
126 |
-
iface.load(None, inputs=None, outputs=html, js=MAIN_JS)
|
127 |
-
|
128 |
-
# Launch the interface
|
129 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|