sujalrajpoot commited on
Commit
476a74f
·
verified ·
1 Parent(s): c50e236

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -17,13 +17,32 @@ client = OpenAI(base_url=endpoint,api_key=GITHUB_TOKEN)
17
  api_usage = {} # Stores {api_key: {"count": X, "reset_time": timestamp}}
18
 
19
  # ✅ Request Limit Configuration
20
- REQUEST_LIMIT = 2 # Max requests per day
21
  SPECIAL_API_KEY = "TrueSyncAI-888d3e23fa5801834aa385118e05bd72-394215579bc4e6ac" # The restricted API key
22
 
23
  # Track API statistics
24
  request_count = 0
25
  start_time = time.time()
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  @app.route('/')
28
  def home():
29
  return """
@@ -57,6 +76,27 @@ def status():
57
  "uptime": uptime_str
58
  })
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # Generate API Key
61
  @app.route("/generate_api_key", methods=["POST"])
62
  def generate_api_key():
@@ -65,25 +105,6 @@ def generate_api_key():
65
  api_key = f"TrueSyncAI-{random_part}-{signature}"
66
 
67
  return jsonify({"api_key": api_key})
68
-
69
- # Validate API Key
70
- def validate_api_key(api_key):
71
- parts = api_key.split("-")
72
- if len(parts) != 3 or parts[0] != "TrueSyncAI":
73
- return False
74
-
75
- random_part, received_signature = parts[1], parts[2]
76
- expected_signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
77
- return expected_signature == received_signature
78
-
79
- def generate_response(query:str) -> str:
80
- try:
81
- model_name = "gpt-4o"
82
- response = client.chat.completions.create(
83
- messages=[{"role": "system","content": "You are TrueSyncAI, a pioneering AI startup founded by Sujal Rajpoot and Anuj Rajpoot. As TrueSyncAI, you are designed to be intelligent, engaging, and helpful in conversations. You should provide insightful, accurate, concise and context-aware responses while maintaining a friendly and professional tone. Your goal is to enhance the user’s experience by adapting to their needs, assisting with various tasks, and learning from interactions to improve over time. Always ensure clarity, relevance, concise and accuracy in your responses, and align with TrueSyncAI’s vision of bridging the gap between virtual intelligence and reality."},{"role": "user","content": query}],temperature=0.7,max_tokens=4096,top_p=0.9,model=model_name,stream=False)
84
- return response.choices[0].message.content
85
- except:
86
- return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
87
 
88
  # Chat Endpoint
89
  @app.route("/v1/chat/completions", methods=["POST"])
 
17
  api_usage = {} # Stores {api_key: {"count": X, "reset_time": timestamp}}
18
 
19
  # ✅ Request Limit Configuration
20
+ REQUEST_LIMIT = 10 # Max requests per day
21
  SPECIAL_API_KEY = "TrueSyncAI-888d3e23fa5801834aa385118e05bd72-394215579bc4e6ac" # The restricted API key
22
 
23
  # Track API statistics
24
  request_count = 0
25
  start_time = time.time()
26
 
27
+ # Validate API Key
28
+ def validate_api_key(api_key):
29
+ parts = api_key.split("-")
30
+ if len(parts) != 3 or parts[0] != "TrueSyncAI":
31
+ return False
32
+
33
+ random_part, received_signature = parts[1], parts[2]
34
+ expected_signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
35
+ return expected_signature == received_signature
36
+
37
+ def generate_response(query:str) -> str:
38
+ try:
39
+ model_name = "gpt-4o"
40
+ response = client.chat.completions.create(
41
+ messages=[{"role": "system","content": "You are TrueSyncAI, a pioneering AI startup founded by Sujal Rajpoot and Anuj Rajpoot. As TrueSyncAI, you are designed to be intelligent, engaging, and helpful in conversations. You should provide insightful, accurate, concise and context-aware responses while maintaining a friendly and professional tone. Your goal is to enhance the user’s experience by adapting to their needs, assisting with various tasks, and learning from interactions to improve over time. Always ensure clarity, relevance, concise and accuracy in your responses, and align with TrueSyncAI’s vision of bridging the gap between virtual intelligence and reality."},{"role": "user","content": query}],temperature=0.7,max_tokens=4096,top_p=0.9,model=model_name,stream=False)
42
+ return response.choices[0].message.content
43
+ except:
44
+ return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
45
+
46
  @app.route('/')
47
  def home():
48
  return """
 
76
  "uptime": uptime_str
77
  })
78
 
79
+ @app.route("/usage", methods=["GET"])
80
+ def usage():
81
+ api_key = request.args.get("api_key")
82
+
83
+ if not api_key or not validate_api_key(api_key):
84
+ return jsonify({"error": "Invalid API Key"}), 401
85
+
86
+ # Get usage data or return default values
87
+ now = datetime.utcnow()
88
+ user_data = api_usage.get(api_key, {"count": 0, "reset_time": now + timedelta(days=1)})
89
+
90
+ remaining_requests = max(0, REQUEST_LIMIT - user_data["count"])
91
+ reset_time = user_data["reset_time"].strftime("%Y-%m-%d %H:%M:%S UTC")
92
+
93
+ return jsonify({
94
+ "api_key": api_key,
95
+ "requests_used": user_data["count"],
96
+ "remaining_requests": remaining_requests,
97
+ "reset_time": reset_time
98
+ })
99
+
100
  # Generate API Key
101
  @app.route("/generate_api_key", methods=["POST"])
102
  def generate_api_key():
 
105
  api_key = f"TrueSyncAI-{random_part}-{signature}"
106
 
107
  return jsonify({"api_key": api_key})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # Chat Endpoint
110
  @app.route("/v1/chat/completions", methods=["POST"])