sujalrajpoot commited on
Commit
2deac23
·
verified ·
1 Parent(s): b13bba1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -15
app.py CHANGED
@@ -1,8 +1,6 @@
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,8 +9,6 @@ SECRET_KEY = os.getenv("SECRET_KEY")
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,28 +74,22 @@ def generate_response(query:str) -> str:
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("5 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})
 
1
  from flask import Flask, request, jsonify
2
  import hmac, hashlib, secrets, time, os
3
  from openai import OpenAI
 
 
4
 
5
  app = Flask(__name__)
6
 
 
9
  GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
10
  endpoint = "https://models.inference.ai.azure.com"
11
  client = OpenAI(base_url=endpoint,api_key=GITHUB_TOKEN)
 
 
12
 
13
  # Track API statistics
14
  request_count = 0
 
74
  return response.choices[0].message.content
75
  except:
76
  return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
77
+
 
 
 
 
 
 
 
 
78
  # Chat Endpoint
79
  @app.route("/v1/chat/completions", methods=["POST"])
 
80
  def chat():
81
  global request_count
82
  data = request.json
83
+ api_key = data.get("api_key")
84
+ message = data.get("message", "").strip()
85
 
86
  if not api_key or not validate_api_key(api_key):
87
  return jsonify({"error": "Invalid API Key"}), 401
88
+
89
  if not message:
90
  return jsonify({"error": "Message cannot be empty"}), 400
91
 
92
+ # Basic AI response (Can integrate LLMs here)
93
  response = generate_response(message)
94
  request_count += 1
95
  return jsonify({"response": response})