Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
from collections import defaultdict
|
|
@@ -7,33 +8,48 @@ from collections import defaultdict
|
|
| 7 |
token = os.environ["TOKEN"]
|
| 8 |
model = os.environ["MODEL"]
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
request_log = defaultdict(list)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
current_time = time.time()
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
request_log[
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return "Rate limit exceeded. Please try again later."
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
request_log[
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
# Load the model from Hugging Face Spaces
|
| 31 |
demo = gr.load(model, src="spaces", token=token)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from flask import Flask, request
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
from collections import defaultdict
|
|
|
|
| 8 |
token = os.environ["TOKEN"]
|
| 9 |
model = os.environ["MODEL"]
|
| 10 |
|
| 11 |
+
# Create Flask app
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Rate-limiting configuration
|
| 15 |
+
RATE_LIMIT_WINDOW = 60 # Time window in seconds
|
| 16 |
+
RATE_LIMIT_REQUESTS = 5 # Max requests per user per window
|
| 17 |
+
|
| 18 |
+
# Dictionary to store request logs
|
| 19 |
request_log = defaultdict(list)
|
| 20 |
|
| 21 |
+
# Function to apply rate limiting
|
| 22 |
+
def rate_limiter():
|
| 23 |
+
user_ip = request.remote_addr # Retrieve the user's IP address
|
| 24 |
current_time = time.time()
|
| 25 |
|
| 26 |
+
# Clean up old requests outside the rate limit window
|
| 27 |
+
request_log[user_ip] = [
|
| 28 |
+
timestamp for timestamp in request_log[user_ip]
|
| 29 |
+
if current_time - timestamp < RATE_LIMIT_WINDOW
|
| 30 |
+
]
|
|
|
|
| 31 |
|
| 32 |
+
# Check if the user exceeded the allowed requests
|
| 33 |
+
if len(request_log[user_ip]) >= RATE_LIMIT_REQUESTS:
|
| 34 |
+
return "❌ Rate limit exceeded. Please wait and try again later."
|
| 35 |
|
| 36 |
+
# Log the current request timestamp
|
| 37 |
+
request_log[user_ip].append(current_time)
|
| 38 |
+
return None # Indicate no rate limit violation
|
| 39 |
|
| 40 |
+
# Load the Gradio model from Hugging Face Spaces
|
| 41 |
demo = gr.load(model, src="spaces", token=token)
|
| 42 |
|
| 43 |
+
# Route to serve the Gradio interface with rate limiting
|
| 44 |
+
@app.route("/", methods=["GET", "POST"])
|
| 45 |
+
def gradio_interface():
|
| 46 |
+
# Apply rate limiting
|
| 47 |
+
rate_limit_message = rate_limiter()
|
| 48 |
+
if rate_limit_message:
|
| 49 |
+
return rate_limit_message # Return rate limit error if exceeded
|
| 50 |
+
|
| 51 |
+
# Launch Gradio app
|
| 52 |
+
return demo.launch(prevent_thread_lock=True)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
app.run(host="0.0.0.0", port=7860) # Run the Flask app
|