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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -50
app.py CHANGED
@@ -1,55 +1,8 @@
1
  import gradio as gr
2
- from flask import Flask, request
3
  import os
4
- import time
5
- from collections import defaultdict
6
 
7
- # Environment variables for authentication and model
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
 
1
  import gradio as gr
 
2
  import os
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ token = os.environ["TOKEN"]
6
+ model=os.environ["MODEL"]
 
 
 
 
 
 
 
7
  demo = gr.load(model, src="spaces", token=token)
8
+ demo.launch(show_api=False, show_error=False, quiet=True, debug=False)