File size: 4,290 Bytes
592df58
 
d83731c
 
592df58
d83731c
 
592df58
 
d83731c
 
 
592df58
 
 
 
 
 
 
aac43f7
 
592df58
edf8837
 
 
 
aac43f7
592df58
 
 
 
 
 
73f75a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b3f0c6
73f75a5
 
592df58
 
73f75a5
 
 
 
 
 
 
 
 
 
0b3f0c6
 
 
 
 
 
 
 
 
73f75a5
 
 
0b3f0c6
edf8837
73f75a5
 
 
 
 
592df58
 
b714e41
9178891
edf8837
 
592df58
 
 
 
edf8837
592df58
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import gradio as gr
from io import BytesIO
import zipfile
import tempfile
import random  
import json
from gradio_client import Client, handle_file
import base64
from PIL import Image
from datetime import datetime
from io import BytesIO

BACKEND = os.getenv("BACKEND")
JS2 = os.getenv("JS2")
MAIN_HTML = os.getenv("MAIN_HTML")
MAIN_JS = os.getenv("MAIN_JS")
HEAD_HTML = os.getenv("HEAD_HTML")
CSS = os.getenv("CSS")
USER = os.getenv("USER")
PASS = os.getenv("PASS")

STATUS_MESSAGES = {
    301: "Invalid Premium ID!"
}

backend = Client(BACKEND, auth=[USER, PASS])

def base64_to_image(base64_str):
    if ";base64," in base64_str:
        base64_str = base64_str.split(";base64,")[1].strip()
    return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))

user_attempts = {}
def clear_old_entries():
    today = datetime.now().date()
    # Create a list of keys to remove
    keys_to_remove = [key for key, value in user_attempts.items() if value != today]
    # Remove old entries
    for key in keys_to_remove:
        del user_attempts[key]

def if_limited(request):
    clear_old_entries()
    user_ip = None
    if request.headers.get("x-forwarded-for"):
        user_ip = request.headers["x-forwarded-for"].split(",")[0]  # First IP in the list

    cookie_value = request.headers.get("cookie", "")
    if "user_id=" in cookie_value:
        user_id = cookie_value.split("user_id=")[1].split(";")[0]
    else:
        user_id = None
    print("##### Coming", user_id, user_ip)
    # Get today's date
    today = datetime.now().date()

    # Check if the user has already tried today (by IP or cookie)
    for key, value in user_attempts.items():
        if (key == user_ip or key == user_id) and value == today:
            return True

    # Record the attempt (store both hashed IP and hashed cookie)
    if user_ip:
        user_attempts[user_ip] = today
    if user_id:
        user_attempts[user_id] = today
    return False

def base64_to_image(base64_str):
    if ";base64," in base64_str:
        base64_str = base64_str.split(";base64,")[1].strip()
    image_data = base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
    image = Image.open(BytesIO(image_data)).convert('RGB')
    buffer = BytesIO()
    image.save(buffer, format="JPEG", quality=90)
    buffer.seek(0)
    return buffer.read()

def search_image(base64_image, token_txt, request: gr.Request):
    global backend
    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
        try:
            temp_file.write(base64_to_image(base64_image))
            file = temp_file.name
            file1 = handle_file(file)
        except Exception as e:
            print(e)
            gr.Info("Please upload an image file.")
            return []

        if token_txt == "" and if_limited(request):
            gr.Info("⏳ Wait for your next free search, or 🚀 Go Premium Search at https://faceseek.online!", duration=10)
            return []

        try:
            result_text = backend.predict(
                file=file1,
                token=token_txt, 
                api_name="/search_face"
            )
        except:
            backend = Client(BACKEND, auth=[USER, PASS])
            result_text = backend.predict(
                file=file1,
                token=token_txt, 
                api_name="/search_face"
            )

        result = json.loads(result_text)
        outarray = []
        if result['status'] > 300:
            gr.Info(STATUS_MESSAGES[result['status']], duration=10)
            return '{"result": []}'
    return result_text

with gr.Blocks(css=CSS, head=HEAD_HTML, title="FaceSeek - Face Search Online") as iface:
    html = gr.HTML(MAIN_HTML, max_height=720)
    base64_txt = gr.Textbox(label="Base64-Image", elem_id="base64_image", visible=False)
    token_txt = gr.Textbox(label="Token-Text", elem_id="premium_token", visible=False)
    out_txt = gr.Textbox(label="Result", visible=False)

    search_button = gr.Button("🔍 Free Face Search", elem_id="search_btn", visible=False)

    search_button.click(search_image, inputs=[base64_txt, token_txt], outputs=out_txt).success(None, inputs=[out_txt], js=JS2)
    iface.load(None, inputs=None, outputs=html, js=MAIN_JS)

# Launch the interface
iface.launch()