File size: 1,706 Bytes
17cf727
485d145
81d944c
d9e3ae2
 
2d85383
d9e3ae2
2d85383
baa0685
 
485d145
 
 
 
 
 
 
 
d9e3ae2
 
485d145
 
 
d9e3ae2
 
485d145
 
 
 
 
d9e3ae2
485d145
 
 
d9e3ae2
485d145
 
 
d9e3ae2
485d145
90d0791
baa0685
485d145
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from flask import Flask, request
import os
import time
from collections import defaultdict

# Environment variables for authentication and model
token = os.environ["TOKEN"]
model = os.environ["MODEL"]

# Create Flask app
app = Flask(__name__)

# Rate-limiting configuration
RATE_LIMIT_WINDOW = 60  # Time window in seconds
RATE_LIMIT_REQUESTS = 5  # Max requests per user per window

# Dictionary to store request logs
request_log = defaultdict(list)

# Function to apply rate limiting
def rate_limiter():
    user_ip = request.remote_addr  # Retrieve the user's IP address
    current_time = time.time()

    # Clean up old requests outside the rate limit window
    request_log[user_ip] = [
        timestamp for timestamp in request_log[user_ip]
        if current_time - timestamp < RATE_LIMIT_WINDOW
    ]

    # Check if the user exceeded the allowed requests
    if len(request_log[user_ip]) >= RATE_LIMIT_REQUESTS:
        return "❌ Rate limit exceeded. Please wait and try again later."

    # Log the current request timestamp
    request_log[user_ip].append(current_time)
    return None  # Indicate no rate limit violation

# Load the Gradio model from Hugging Face Spaces
demo = gr.load(model, src="spaces", token=token)

# Route to serve the Gradio interface with rate limiting
@app.route("/", methods=["GET", "POST"])
def gradio_interface():
    # Apply rate limiting
    rate_limit_message = rate_limiter()
    if rate_limit_message:
        return rate_limit_message  # Return rate limit error if exceeded

    # Launch Gradio app
    return demo.launch(prevent_thread_lock=True)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)  # Run the Flask app