Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
-
from datetime import datetime
|
| 5 |
from typing import List, Dict, Optional
|
| 6 |
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from openai import OpenAI
|
| 9 |
import logging
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- Security Helper Functions ---
|
| 12 |
def verify_origin(request: Request):
|
|
@@ -250,9 +253,89 @@ def should_use_search(message: str) -> bool:
|
|
| 250 |
|
| 251 |
return False
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
# --- Enhanced Chatbot Endpoint ---
|
| 254 |
@app.post("/chat")
|
| 255 |
async def chat_endpoint(request: Request, _: None = Depends(verify_origin)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
if not client:
|
| 257 |
raise HTTPException(status_code=500, detail="LLM client not configured")
|
| 258 |
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
from typing import List, Dict, Optional
|
| 6 |
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from openai import OpenAI
|
| 9 |
import logging
|
| 10 |
+
import time
|
| 11 |
+
from collections import defaultdict
|
| 12 |
+
|
| 13 |
|
| 14 |
# --- Security Helper Functions ---
|
| 15 |
def verify_origin(request: Request):
|
|
|
|
| 253 |
|
| 254 |
return False
|
| 255 |
|
| 256 |
+
# Rate limiting dictionary
|
| 257 |
+
class RateLimiter:
|
| 258 |
+
def __init__(self, max_requests: int, time_window: timedelta):
|
| 259 |
+
self.max_requests = max_requests
|
| 260 |
+
self.time_window = time_window
|
| 261 |
+
self.requests: Dict[str, list] = defaultdict(list)
|
| 262 |
+
|
| 263 |
+
def _cleanup_old_requests(self, user_ip: str) -> None:
|
| 264 |
+
"""Remove requests that are outside the time window."""
|
| 265 |
+
current_time = time.time()
|
| 266 |
+
self.requests[user_ip] = [
|
| 267 |
+
timestamp for timestamp in self.requests[user_ip]
|
| 268 |
+
if current_time - timestamp < self.time_window.total_seconds()
|
| 269 |
+
]
|
| 270 |
+
|
| 271 |
+
def is_rate_limited(self, user_ip: str) -> bool:
|
| 272 |
+
"""Check if the user has exceeded their rate limit."""
|
| 273 |
+
self._cleanup_old_requests(user_ip)
|
| 274 |
+
|
| 275 |
+
# Get current count after cleanup
|
| 276 |
+
current_count = len(self.requests[user_ip])
|
| 277 |
+
|
| 278 |
+
# Add current request timestamp (incrementing the count)
|
| 279 |
+
current_time = time.time()
|
| 280 |
+
self.requests[user_ip].append(current_time)
|
| 281 |
+
|
| 282 |
+
# Check if user has exceeded the maximum requests
|
| 283 |
+
return (current_count + 1) > self.max_requests
|
| 284 |
+
|
| 285 |
+
def get_current_count(self, user_ip: str) -> int:
|
| 286 |
+
"""Get the current request count for an IP."""
|
| 287 |
+
self._cleanup_old_requests(user_ip)
|
| 288 |
+
return len(self.requests[user_ip])
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
# Initialize rate limiter with 100 requests per day
|
| 292 |
+
rate_limiter = RateLimiter(
|
| 293 |
+
max_requests=50,
|
| 294 |
+
time_window=timedelta(days=1)
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
def get_user_ip(request: Request) -> str:
|
| 298 |
+
"""Helper function to get user's IP address."""
|
| 299 |
+
forwarded = request.headers.get("X-Forwarded-For")
|
| 300 |
+
if forwarded:
|
| 301 |
+
return forwarded.split(",")[0]
|
| 302 |
+
return request.client.host
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
class ApiRotator:
|
| 306 |
+
def __init__(self, apis):
|
| 307 |
+
self.apis = apis
|
| 308 |
+
self.last_successful_index = None
|
| 309 |
+
|
| 310 |
+
def get_prioritized_apis(self):
|
| 311 |
+
if self.last_successful_index is not None:
|
| 312 |
+
# Move the last successful API to the front
|
| 313 |
+
rotated_apis = (
|
| 314 |
+
[self.apis[self.last_successful_index]] +
|
| 315 |
+
self.apis[:self.last_successful_index] +
|
| 316 |
+
self.apis[self.last_successful_index+1:]
|
| 317 |
+
)
|
| 318 |
+
return rotated_apis
|
| 319 |
+
return self.apis
|
| 320 |
+
|
| 321 |
+
def update_last_successful(self, index):
|
| 322 |
+
self.last_successful_index = index
|
| 323 |
+
|
| 324 |
+
|
| 325 |
# --- Enhanced Chatbot Endpoint ---
|
| 326 |
@app.post("/chat")
|
| 327 |
async def chat_endpoint(request: Request, _: None = Depends(verify_origin)):
|
| 328 |
+
user_ip = get_user_ip(request)
|
| 329 |
+
|
| 330 |
+
if rate_limiter.is_rate_limited(user_ip):
|
| 331 |
+
current_count = rate_limiter.get_current_count(user_ip)
|
| 332 |
+
raise HTTPException(
|
| 333 |
+
status_code=429,
|
| 334 |
+
detail={
|
| 335 |
+
"error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
|
| 336 |
+
"url": "https://t.me/chrunoss"
|
| 337 |
+
}
|
| 338 |
+
)
|
| 339 |
if not client:
|
| 340 |
raise HTTPException(status_code=500, detail="LLM client not configured")
|
| 341 |
|