Delete app.py
Browse files
app.py
DELETED
@@ -1,336 +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 |
-
|
13 |
-
BUY_PREMIUM = "π₯ Get Premium Token to Unlock"
|
14 |
-
|
15 |
-
STATUS_MESSAGES = {
|
16 |
-
201: "Get a Premium Token for full link, deeper results including social profiles.",
|
17 |
-
301: "Invalid token! Get Premium Token using link in page"
|
18 |
-
}
|
19 |
-
|
20 |
-
BACKEND = os.getenv("BACKEND")
|
21 |
-
JS_FUNC = os.getenv("JS_FUNC")
|
22 |
-
USER = os.getenv("USER")
|
23 |
-
PASS = os.getenv("PASS")
|
24 |
-
|
25 |
-
backend = Client(BACKEND, auth=[USER, PASS])
|
26 |
-
|
27 |
-
def base64_to_image(base64_str):
|
28 |
-
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
|
29 |
-
|
30 |
-
user_attempts = {}
|
31 |
-
def clear_old_entries():
|
32 |
-
today = datetime.now().date()
|
33 |
-
# Create a list of keys to remove
|
34 |
-
keys_to_remove = [key for key, value in user_attempts.items() if value != today]
|
35 |
-
# Remove old entries
|
36 |
-
for key in keys_to_remove:
|
37 |
-
del user_attempts[key]
|
38 |
-
|
39 |
-
def if_limited(request):
|
40 |
-
clear_old_entries()
|
41 |
-
user_ip = None
|
42 |
-
if request.headers.get("x-forwarded-for"):
|
43 |
-
user_ip = request.headers["x-forwarded-for"].split(",")[0] # First IP in the list
|
44 |
-
|
45 |
-
cookie_value = request.headers.get("cookie", "")
|
46 |
-
if "user_id=" in cookie_value:
|
47 |
-
user_id = cookie_value.split("user_id=")[1].split(";")[0]
|
48 |
-
else:
|
49 |
-
user_id = None
|
50 |
-
print("##### Coming", user_id, user_ip)
|
51 |
-
# Get today's date
|
52 |
-
today = datetime.now().date()
|
53 |
-
|
54 |
-
# Check if the user has already tried today (by IP or cookie)
|
55 |
-
for key, value in user_attempts.items():
|
56 |
-
if (key == user_ip or key == user_id) and value == today:
|
57 |
-
return True
|
58 |
-
|
59 |
-
# Record the attempt (store both hashed IP and hashed cookie)
|
60 |
-
if user_ip:
|
61 |
-
user_attempts[user_ip] = today
|
62 |
-
if user_id:
|
63 |
-
user_attempts[user_id] = today
|
64 |
-
return False
|
65 |
-
|
66 |
-
def search_face(file, token_txt, request: gr.Request):
|
67 |
-
global backend
|
68 |
-
try:
|
69 |
-
file1 = handle_file(file)
|
70 |
-
except Exception as e:
|
71 |
-
gr.Info("Please upload an image file.")
|
72 |
-
return []
|
73 |
-
|
74 |
-
if token == "" and if_limited(request):
|
75 |
-
gr.Info("β³ Wait for your next free search, or π Go Premium for deep search!", duration=12)
|
76 |
-
return []
|
77 |
-
|
78 |
-
country_code = "PPE, PFC, PLS, FFC, FPE"
|
79 |
-
|
80 |
-
try:
|
81 |
-
result_text = backend.predict(
|
82 |
-
file=file1,
|
83 |
-
token=token_txt,
|
84 |
-
country_code=country_code,
|
85 |
-
api_name="/search_face"
|
86 |
-
)
|
87 |
-
except:
|
88 |
-
try:
|
89 |
-
backend = Client(BACKEND, auth=[USER, PASS])
|
90 |
-
result_text = backend.predict(
|
91 |
-
file=file1,
|
92 |
-
token=token_txt,
|
93 |
-
country_code=country_code,
|
94 |
-
api_name="/search_face"
|
95 |
-
)
|
96 |
-
except:
|
97 |
-
return []
|
98 |
-
|
99 |
-
result = json.loads(result_text)
|
100 |
-
outarray = []
|
101 |
-
if result['status'] > 300:
|
102 |
-
raise gr.Error(STATUS_MESSAGES[result['status']])
|
103 |
-
|
104 |
-
for item in result['result']:
|
105 |
-
image = Image.open(BytesIO(base64_to_image(item['image'])))
|
106 |
-
outarray.append((image, item['url']))
|
107 |
-
|
108 |
-
if result['status'] == 201:
|
109 |
-
gr.Info(STATUS_MESSAGES[result['status']], duration=12)
|
110 |
-
return outarray
|
111 |
-
|
112 |
-
def export_images(items):
|
113 |
-
if not items:
|
114 |
-
return None
|
115 |
-
# Create a zip file in memory
|
116 |
-
zip_buffer = BytesIO()
|
117 |
-
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
118 |
-
url_text = ""
|
119 |
-
i = 1
|
120 |
-
for item in items:
|
121 |
-
if item[1] == BUY_PREMIUM:
|
122 |
-
continue
|
123 |
-
with open(item[0], 'rb') as img_file:
|
124 |
-
zip_file.writestr(f'image_{i}.jpg', img_file.read())
|
125 |
-
url_text += f"image_{i}.jpg: {item[1]}\n"
|
126 |
-
i += 1
|
127 |
-
zip_file.writestr("urls.txt", url_text)
|
128 |
-
zip_buffer.seek(0)
|
129 |
-
|
130 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as temp_file:
|
131 |
-
temp_file.write(zip_buffer.getvalue())
|
132 |
-
temp_file_path = temp_file.name
|
133 |
-
|
134 |
-
return temp_file_path
|
135 |
-
|
136 |
-
custom_css = """
|
137 |
-
caption.caption {
|
138 |
-
user-select: text;
|
139 |
-
cursor: text;
|
140 |
-
}
|
141 |
-
|
142 |
-
div#export_file {
|
143 |
-
max-height: 63.39px;
|
144 |
-
}
|
145 |
-
|
146 |
-
.svelte-snayfm {
|
147 |
-
height: auto;
|
148 |
-
}
|
149 |
-
|
150 |
-
.icon.svelte-snayfm {
|
151 |
-
width: 48px;
|
152 |
-
height: 48px;
|
153 |
-
}
|
154 |
-
|
155 |
-
.button-gradient {
|
156 |
-
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
|
157 |
-
background-size: 400% 400%;
|
158 |
-
border: none;
|
159 |
-
padding: 14px 28px;
|
160 |
-
font-size: 16px;
|
161 |
-
font-weight: bold;
|
162 |
-
color: white;
|
163 |
-
border-radius: 10px;
|
164 |
-
cursor: pointer;
|
165 |
-
transition: 0.3s ease-in-out;
|
166 |
-
animation: gradientAnimation 2s infinite linear;
|
167 |
-
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
|
168 |
-
}
|
169 |
-
|
170 |
-
@keyframes gradientAnimation {
|
171 |
-
0% { background-position: 0% 50%; }
|
172 |
-
25% { background-position: 50% 100%; }
|
173 |
-
50% { background-position: 100% 50%; }
|
174 |
-
75% { background-position: 50% 0%; }
|
175 |
-
100% { background-position: 0% 50%; }
|
176 |
-
}
|
177 |
-
|
178 |
-
.button-gradient:hover {
|
179 |
-
transform: scale(1.05);
|
180 |
-
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
|
181 |
-
}
|
182 |
-
|
183 |
-
@keyframes labelGradientFlow {
|
184 |
-
0% { background-position: 0% 50%; }
|
185 |
-
50% { background-position: 100% 50%; }
|
186 |
-
100% { background-position: 0% 50%; }
|
187 |
-
}
|
188 |
-
|
189 |
-
label.svelte-i3tvor.float {
|
190 |
-
background: linear-gradient(90deg, #555555, #333333, #555555); /* Dark gray gradient */
|
191 |
-
background-size: 200% 200%;
|
192 |
-
color: #f1f1f1;
|
193 |
-
font-weight: bold;
|
194 |
-
text-align: center;
|
195 |
-
animation: labelGradientFlow 4s infinite ease-in-out;
|
196 |
-
box-shadow: 0 0 6px rgba(50, 50, 50, 0.7); /* Subtle dark glow */
|
197 |
-
transition: all 0.3s ease-in-out;
|
198 |
-
}
|
199 |
-
"""
|
200 |
-
|
201 |
-
js = """
|
202 |
-
function aff() {
|
203 |
-
const links = document.querySelectorAll('a');
|
204 |
-
|
205 |
-
const currentUrl = new URL(window.location.href);
|
206 |
-
const currentParams = currentUrl.searchParams.toString();
|
207 |
-
|
208 |
-
links.forEach(link => {
|
209 |
-
const href = link.getAttribute('href');
|
210 |
-
if (href && (href.startsWith('https://faceonlive.pocketsflow.com') || href.startsWith('https://faceonlive.com'))) {
|
211 |
-
const targetUrl = new URL(href);
|
212 |
-
// Append current page parameters to the link
|
213 |
-
currentParams.split('&').forEach(param => {
|
214 |
-
if (param) {
|
215 |
-
const [key, value] = param.split('=');
|
216 |
-
targetUrl.searchParams.set(key, value);
|
217 |
-
}
|
218 |
-
});
|
219 |
-
link.setAttribute('href', targetUrl.toString());
|
220 |
-
}
|
221 |
-
});
|
222 |
-
|
223 |
-
return ''
|
224 |
-
}
|
225 |
-
"""
|
226 |
-
|
227 |
-
head = """
|
228 |
-
<!-- Google tag (gtag.js) -->
|
229 |
-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8YPXF4536P"></script>
|
230 |
-
<script>
|
231 |
-
window.dataLayer = window.dataLayer || [];
|
232 |
-
function gtag(){dataLayer.push(arguments);}
|
233 |
-
gtag('js', new Date());
|
234 |
-
|
235 |
-
gtag('config', 'G-8YPXF4536P');
|
236 |
-
</script>
|
237 |
-
"""
|
238 |
-
|
239 |
-
output = gr.Gallery(label="Search may take a few minutes.", columns=[3], object_fit="contain", height="480px", interactive=False)
|
240 |
-
col2 = gr.Column(scale=2, visible=False)
|
241 |
-
|
242 |
-
def init_ui():
|
243 |
-
return gr.update(visible=True), gr.update(visible=False)
|
244 |
-
|
245 |
-
def search_ui():
|
246 |
-
return gr.update(visible=False), gr.update(visible=True)
|
247 |
-
|
248 |
-
def search_face_examples(image):
|
249 |
-
return search_face(image), gr.update(visible=False), gr.update(visible=True)
|
250 |
-
|
251 |
-
def set_url_token(request: gr.Request):
|
252 |
-
if request and request.query_params:
|
253 |
-
params = dict(request.query_params)
|
254 |
-
if "ptoken" in params:
|
255 |
-
return params["ptoken"]
|
256 |
-
return ""
|
257 |
-
|
258 |
-
def update_button(token):
|
259 |
-
if token:
|
260 |
-
return gr.update(visible=False), gr.update(value="π Unlock Deep Search Now!", elem_classes="button-gradient")
|
261 |
-
else:
|
262 |
-
return gr.update(visible=True), gr.update(value="π Free Face Search", elem_classes="")
|
263 |
-
|
264 |
-
MARKDOWN0 = """
|
265 |
-
# Free Face Search Online - β€οΈLike above if this space helps
|
266 |
-
#### [Learn more about our Reverse Face Search](https://faceseek.online)
|
267 |
-
#### [Face Search API and Affiliate Program (50%).](https://portfolio.faceonlive.com/#face_search)
|
268 |
-
"""
|
269 |
-
MARKDOWN2 = """
|
270 |
-
### [Why Deep Search with Premium Token?](https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb)
|
271 |
-
β
**Search Social Media, Deep Web & Uncover Hidden Profiles**
|
272 |
-
β
**Outperforming PimEyes + FaceCheck.ID Combined!**
|
273 |
-
"""
|
274 |
-
|
275 |
-
MARKDOWN3_2 = """
|
276 |
-
<div align="right"><a href="https://faceonlive.pocketsflow.com/checkout?productId=677fe2b5aeced2814bc47dd1" target='_blank' style='font-size: 16px;'>Opt-Out From Search</a></div><br/>
|
277 |
-
<div align="right"><a href="https://faceonlive.com/deepfake-detector" target='_blank' style='font-size: 16px;'>AI Generated Image & Deepfake Detector</div>
|
278 |
-
"""
|
279 |
-
|
280 |
-
PREMIUM_CHECKOUT = "https://faceonlive.pocketsflow.com/checkout?productId=676c15b1971244a587ca07cb"
|
281 |
-
|
282 |
-
with gr.Blocks(css=custom_css, head=head, delete_cache=(3600, 3600)) as demo:
|
283 |
-
gr.Markdown(MARKDOWN0)
|
284 |
-
with gr.Row():
|
285 |
-
with gr.Column(scale=1) as col1:
|
286 |
-
image = gr.Image(type='filepath', height=360)
|
287 |
-
with gr.Row():
|
288 |
-
with gr.Column():
|
289 |
-
token = gr.Textbox(placeholder="(Optional) Input Premium Token here.", label="Premium Token")
|
290 |
-
with gr.Column():
|
291 |
-
md_premium1 = gr.Markdown(MARKDOWN2)
|
292 |
-
gr.HTML("<div id='limit'></div>")
|
293 |
-
with gr.Row():
|
294 |
-
with gr.Column():
|
295 |
-
limit_button = gr.Button("π Free Face Search")
|
296 |
-
face_search_button = gr.Button("Face Search", visible=False, elem_id="submit_btn")
|
297 |
-
with gr.Column():
|
298 |
-
premium_search_button = gr.Button("π Unlock Deep Search Now!", elem_classes="button-gradient", link=PREMIUM_CHECKOUT)
|
299 |
-
with gr.Row():
|
300 |
-
with gr.Column():
|
301 |
-
gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, fn=search_face_examples, outputs=[output, col1, col2])
|
302 |
-
with gr.Column():
|
303 |
-
gr.HTML(MARKDOWN3_2)
|
304 |
-
|
305 |
-
with col2.render():
|
306 |
-
gr.Markdown("> ### **β οΈ Reminder:** Export images before refreshing the page by clicking the 'Export Images' button.")
|
307 |
-
output.render()
|
308 |
-
with gr.Row():
|
309 |
-
with gr.Column():
|
310 |
-
md_premium2 = gr.Markdown(MARKDOWN2)
|
311 |
-
with gr.Column():
|
312 |
-
export_button = gr.Button("Export Images")
|
313 |
-
export_file = gr.File(label="Download", elem_id="export_file")
|
314 |
-
with gr.Row():
|
315 |
-
with gr.Column():
|
316 |
-
premium_link_button = gr.Button("π Unlock Deep Search Now!", elem_classes="button-gradient", link=PREMIUM_CHECKOUT)
|
317 |
-
with gr.Column():
|
318 |
-
new_search_button = gr.Button("π New Search")
|
319 |
-
gr.HTML(MARKDOWN3_2)
|
320 |
-
|
321 |
-
limit_button.click(None, js=JS_FUNC)
|
322 |
-
face_search_button.click(search_ui, inputs=[], outputs=[col1, col2], api_name=False)
|
323 |
-
face_search_button.click(search_face, inputs=[image, token], outputs=[output], api_name=False)
|
324 |
-
export_button.click(export_images, inputs=[output], outputs=export_file, api_name=False)
|
325 |
-
new_search_button.click(init_ui, inputs=[], outputs=[col1, col2], api_name=False)
|
326 |
-
token.change(update_button, inputs=[token], outputs=[premium_search_button, limit_button])
|
327 |
-
|
328 |
-
with gr.Row():
|
329 |
-
with gr.Column(scale=1):
|
330 |
-
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/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Search-Online&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
|
331 |
-
with gr.Column(scale=5):
|
332 |
-
html = gr.HTML()
|
333 |
-
demo.load(None, inputs=None, outputs=html, js=js)
|
334 |
-
demo.load(set_url_token, inputs=None, outputs=[token])
|
335 |
-
|
336 |
-
demo.queue(api_open=False, default_concurrency_limit=8).launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|