Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -23,11 +23,12 @@ torch.backends.cudnn.benchmark = False
|
|
23 |
torch.backends.cuda.matmul.allow_tf32 = False
|
24 |
torch.backends.cudnn.allow_tf32 = False
|
25 |
|
26 |
-
# Data structures to track IP
|
27 |
ip_access_records = defaultdict(lambda: {
|
28 |
'total_access_time': timedelta(),
|
29 |
'blocked_until': None,
|
30 |
-
'session_start': None
|
|
|
31 |
})
|
32 |
|
33 |
SESSION_TIMEOUT = timedelta(hours=1)
|
@@ -64,7 +65,8 @@ def update_ip_access_time(client_ip):
|
|
64 |
record['session_start'] = now
|
65 |
|
66 |
# Remove access times older than 24 hours
|
67 |
-
|
|
|
68 |
record['total_access_time'] = timedelta()
|
69 |
|
70 |
record['last_access_time'] = now
|
@@ -138,37 +140,33 @@ analyzer = backend.DeepfakeAnalyzer()
|
|
138 |
def generate_captcha():
|
139 |
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
-
def verify_captcha(user_input):
|
145 |
-
global captcha_solution
|
146 |
if user_input.strip() == captcha_solution:
|
147 |
-
return "Captcha verified. Video analysis will proceed.", True
|
148 |
else:
|
149 |
-
|
150 |
-
return f"Incorrect CAPTCHA. Please try again.", False, captcha_solution
|
151 |
|
152 |
@spaces.GPU(duration=1000)
|
153 |
-
def analyze_video(video_file, captcha_input, request: gr.Request):
|
154 |
client_ip = get_client_ip(request)
|
155 |
|
156 |
# Check if IP is blocked
|
157 |
if is_ip_blocked(client_ip):
|
158 |
-
log_api_access(successful_captcha=False, video_file=video_file
|
159 |
return {"error": "Your IP has been blocked due to excessive usage. Please try again later."}
|
160 |
|
161 |
# Verify CAPTCHA
|
162 |
-
message, success
|
163 |
if not success:
|
164 |
-
log_api_access(successful_captcha=False, video_file=video_file
|
165 |
return {"error": message}
|
166 |
|
167 |
# Update IP access time
|
168 |
update_ip_access_time(client_ip)
|
169 |
|
170 |
# Log API access attempt with successful CAPTCHA
|
171 |
-
log_api_access(successful_captcha=True, video_file=video_file
|
172 |
|
173 |
try:
|
174 |
# Video truncation and analysis
|
@@ -199,9 +197,12 @@ def main_interface():
|
|
199 |
gr.Markdown("# AllMark - Deepfake Analyzer")
|
200 |
gr.Markdown("Upload a video to proceed with analysis.")
|
201 |
|
202 |
-
#
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
205 |
captcha_input = gr.Textbox(label="Enter CAPTCHA Here", visible=False)
|
206 |
captcha_output = gr.Textbox(label="CAPTCHA Status", interactive=False, visible=False)
|
207 |
analyze_button = gr.Button("Analyze Video", visible=False)
|
@@ -210,16 +211,34 @@ def main_interface():
|
|
210 |
# Function to show CAPTCHA after video upload
|
211 |
def show_captcha(video):
|
212 |
if video is not None:
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
else:
|
215 |
-
return
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
# Handle analysis
|
220 |
analyze_button.click(
|
221 |
analyze_video,
|
222 |
-
inputs=[video_input, captcha_input],
|
223 |
outputs=analysis_output
|
224 |
)
|
225 |
|
|
|
23 |
torch.backends.cuda.matmul.allow_tf32 = False
|
24 |
torch.backends.cudnn.allow_tf32 = False
|
25 |
|
26 |
+
# Data structures to track IP addresses
|
27 |
ip_access_records = defaultdict(lambda: {
|
28 |
'total_access_time': timedelta(),
|
29 |
'blocked_until': None,
|
30 |
+
'session_start': None,
|
31 |
+
'last_access_time': None
|
32 |
})
|
33 |
|
34 |
SESSION_TIMEOUT = timedelta(hours=1)
|
|
|
65 |
record['session_start'] = now
|
66 |
|
67 |
# Remove access times older than 24 hours
|
68 |
+
last_access_time = record.get('last_access_time')
|
69 |
+
if last_access_time and now - last_access_time > timedelta(hours=24):
|
70 |
record['total_access_time'] = timedelta()
|
71 |
|
72 |
record['last_access_time'] = now
|
|
|
140 |
def generate_captcha():
|
141 |
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
|
142 |
|
143 |
+
# Function to verify CAPTCHA per user session
|
144 |
+
def verify_captcha(user_input, captcha_solution):
|
|
|
|
|
|
|
145 |
if user_input.strip() == captcha_solution:
|
146 |
+
return "Captcha verified. Video analysis will proceed.", True
|
147 |
else:
|
148 |
+
return "Incorrect CAPTCHA. Please try again.", False
|
|
|
149 |
|
150 |
@spaces.GPU(duration=1000)
|
151 |
+
def analyze_video(video_file, captcha_input, captcha_solution, request: gr.Request):
|
152 |
client_ip = get_client_ip(request)
|
153 |
|
154 |
# Check if IP is blocked
|
155 |
if is_ip_blocked(client_ip):
|
156 |
+
log_api_access(successful_captcha=False, video_file=os.path.basename(video_file) if video_file else "N/A", client_ip=client_ip, blocked=True)
|
157 |
return {"error": "Your IP has been blocked due to excessive usage. Please try again later."}
|
158 |
|
159 |
# Verify CAPTCHA
|
160 |
+
message, success = verify_captcha(captcha_input, captcha_solution)
|
161 |
if not success:
|
162 |
+
log_api_access(successful_captcha=False, video_file=os.path.basename(video_file) if video_file else "N/A", client_ip=client_ip)
|
163 |
return {"error": message}
|
164 |
|
165 |
# Update IP access time
|
166 |
update_ip_access_time(client_ip)
|
167 |
|
168 |
# Log API access attempt with successful CAPTCHA
|
169 |
+
log_api_access(successful_captcha=True, video_file=os.path.basename(video_file) if video_file else "N/A", client_ip=client_ip)
|
170 |
|
171 |
try:
|
172 |
# Video truncation and analysis
|
|
|
197 |
gr.Markdown("# AllMark - Deepfake Analyzer")
|
198 |
gr.Markdown("Upload a video to proceed with analysis.")
|
199 |
|
200 |
+
# State variables for per-session data
|
201 |
+
captcha_solution = gr.State()
|
202 |
+
|
203 |
+
# Video input with display size restriction
|
204 |
+
video_input = gr.Video(label="Upload Video", height=300) # Adjust height as needed
|
205 |
+
captcha_text = gr.Textbox(label="CAPTCHA", interactive=False, visible=False)
|
206 |
captcha_input = gr.Textbox(label="Enter CAPTCHA Here", visible=False)
|
207 |
captcha_output = gr.Textbox(label="CAPTCHA Status", interactive=False, visible=False)
|
208 |
analyze_button = gr.Button("Analyze Video", visible=False)
|
|
|
211 |
# Function to show CAPTCHA after video upload
|
212 |
def show_captcha(video):
|
213 |
if video is not None:
|
214 |
+
# Generate a new CAPTCHA for this session
|
215 |
+
new_captcha = generate_captcha()
|
216 |
+
return (
|
217 |
+
gr.update(visible=True, value=new_captcha),
|
218 |
+
gr.update(visible=True),
|
219 |
+
gr.update(visible=True),
|
220 |
+
gr.update(visible=True),
|
221 |
+
new_captcha # Update the session state
|
222 |
+
)
|
223 |
else:
|
224 |
+
return (
|
225 |
+
gr.update(visible=False),
|
226 |
+
gr.update(visible=False),
|
227 |
+
gr.update(visible=False),
|
228 |
+
gr.update(visible=False),
|
229 |
+
gr.no_update
|
230 |
+
)
|
231 |
+
|
232 |
+
video_input.change(
|
233 |
+
show_captcha,
|
234 |
+
inputs=video_input,
|
235 |
+
outputs=[captcha_text, captcha_input, captcha_output, analyze_button, captcha_solution]
|
236 |
+
)
|
237 |
|
238 |
# Handle analysis
|
239 |
analyze_button.click(
|
240 |
analyze_video,
|
241 |
+
inputs=[video_input, captcha_input, captcha_solution],
|
242 |
outputs=analysis_output
|
243 |
)
|
244 |
|