Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
token = os.environ["TOKEN"]
|
6 |
model = os.environ["MODEL"]
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Load the model from Hugging Face Spaces
|
9 |
demo = gr.load(model, src="spaces", token=token)
|
10 |
|
11 |
-
# Launch
|
12 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import time
|
4 |
+
from collections import defaultdict
|
5 |
|
6 |
+
# Environment variables for authentication and model
|
7 |
token = os.environ["TOKEN"]
|
8 |
model = os.environ["MODEL"]
|
9 |
|
10 |
+
# Request tracking for rate limiting
|
11 |
+
request_log = defaultdict(list)
|
12 |
+
|
13 |
+
def rate_limited_wrapper(*args, **kwargs):
|
14 |
+
user_identifier = "user_ip_placeholder" # Replace with logic to retrieve user's IP or unique ID
|
15 |
+
current_time = time.time()
|
16 |
+
|
17 |
+
# Remove requests older than 60 seconds from the log
|
18 |
+
request_log[user_identifier] = [t for t in request_log[user_identifier] if current_time - t < 60]
|
19 |
+
|
20 |
+
# Check the number of requests in the last minute
|
21 |
+
if len(request_log[user_identifier]) >= 5: # Example: Limit to 5 requests per minute
|
22 |
+
return "Rate limit exceeded. Please try again later."
|
23 |
+
|
24 |
+
# Add the current request to the log
|
25 |
+
request_log[user_identifier].append(current_time)
|
26 |
+
|
27 |
+
# Forward request to the loaded model
|
28 |
+
return demo(*args, **kwargs)
|
29 |
+
|
30 |
# Load the model from Hugging Face Spaces
|
31 |
demo = gr.load(model, src="spaces", token=token)
|
32 |
|
33 |
+
# Launch the interface with the rate-limited wrapper
|
34 |
+
demo.launch(
|
35 |
+
show_api=False,
|
36 |
+
show_error=False,
|
37 |
+
quiet=True,
|
38 |
+
debug=False
|
39 |
+
)
|