geeek commited on
Commit
485d145
·
verified ·
1 Parent(s): d9e3ae2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -21
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
- # 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
- )
 
 
 
 
 
 
 
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