Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
import hmac, hashlib, secrets, time
|
3 |
from openai import OpenAI
|
4 |
-
import
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
@@ -10,6 +11,8 @@ SECRET_KEY = os.getenv("SECRET_KEY")
|
|
10 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
11 |
endpoint = "https://models.inference.ai.azure.com"
|
12 |
client = OpenAI(base_url=endpoint,api_key=GITHUB_TOKEN)
|
|
|
|
|
13 |
|
14 |
# Track API statistics
|
15 |
request_count = 0
|
@@ -75,22 +78,28 @@ def generate_response(query:str) -> str:
|
|
75 |
return response.choices[0].message.content
|
76 |
except:
|
77 |
return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Chat Endpoint
|
80 |
@app.route("/v1/chat/completions", methods=["POST"])
|
|
|
81 |
def chat():
|
82 |
global request_count
|
83 |
data = request.json
|
84 |
-
api_key = data.get("api_key")
|
85 |
-
message = data.get("message", "").strip()
|
86 |
|
87 |
if not api_key or not validate_api_key(api_key):
|
88 |
return jsonify({"error": "Invalid API Key"}), 401
|
89 |
-
|
90 |
if not message:
|
91 |
return jsonify({"error": "Message cannot be empty"}), 400
|
92 |
|
93 |
-
# Basic AI response (Can integrate LLMs here)
|
94 |
response = generate_response(message)
|
95 |
request_count += 1
|
96 |
return jsonify({"response": response})
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
import hmac, hashlib, secrets, time, os
|
3 |
from openai import OpenAI
|
4 |
+
from flask_limiter import Limiter
|
5 |
+
from flask_limiter.util import get_remote_address
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
|
|
11 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
12 |
endpoint = "https://models.inference.ai.azure.com"
|
13 |
client = OpenAI(base_url=endpoint,api_key=GITHUB_TOKEN)
|
14 |
+
# Initialize Flask-Limiter (without global default limits)
|
15 |
+
limiter = Limiter(get_remote_address, app=app)
|
16 |
|
17 |
# Track API statistics
|
18 |
request_count = 0
|
|
|
78 |
return response.choices[0].message.content
|
79 |
except:
|
80 |
return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
|
81 |
+
|
82 |
+
# Custom Rate Limit Error Handler
|
83 |
+
@app.errorhandler(429)
|
84 |
+
def ratelimit_exceeded(e):
|
85 |
+
return jsonify({
|
86 |
+
"error": "Rate limit exceeded",
|
87 |
+
"message": "You have reached the maximum limit of 5 requests per minute. Please try again later."
|
88 |
+
}), 429
|
89 |
+
|
90 |
# Chat Endpoint
|
91 |
@app.route("/v1/chat/completions", methods=["POST"])
|
92 |
+
@limiter.limit("1 per minute") # Apply limit only to this function
|
93 |
def chat():
|
94 |
global request_count
|
95 |
data = request.json
|
96 |
+
api_key, message = data.get("api_key"), data.get("message", "").strip()
|
|
|
97 |
|
98 |
if not api_key or not validate_api_key(api_key):
|
99 |
return jsonify({"error": "Invalid API Key"}), 401
|
|
|
100 |
if not message:
|
101 |
return jsonify({"error": "Message cannot be empty"}), 400
|
102 |
|
|
|
103 |
response = generate_response(message)
|
104 |
request_count += 1
|
105 |
return jsonify({"response": response})
|