Spaces:
Running
Running
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 | |
) |