Spaces:
Running
Running
File size: 1,207 Bytes
17cf727 81d944c d9e3ae2 2d85383 d9e3ae2 2d85383 baa0685 d9e3ae2 43ec0d8 90d0791 baa0685 d9e3ae2 |
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 |
import gradio as gr
import os
import time
from collections import defaultdict
# Environment variables for authentication and model
token = os.environ["TOKEN"]
model = os.environ["MODEL"]
# Request tracking for rate limiting
request_log = defaultdict(list)
def rate_limited_wrapper(*args, **kwargs):
user_identifier = "user_ip_placeholder" # Replace with logic to retrieve user's IP or unique ID
current_time = time.time()
# Remove requests older than 60 seconds from the log
request_log[user_identifier] = [t for t in request_log[user_identifier] if current_time - t < 60]
# Check the number of requests in the last minute
if len(request_log[user_identifier]) >= 5: # Example: Limit to 5 requests per minute
return "Rate limit exceeded. Please try again later."
# Add the current request to the log
request_log[user_identifier].append(current_time)
# Forward request to the loaded model
return demo(*args, **kwargs)
# Load the model from Hugging Face Spaces
demo = gr.load(model, src="spaces", token=token)
# Launch the interface with the rate-limited wrapper
demo.launch(
show_api=False,
show_error=False,
quiet=True,
debug=False
) |