Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -11,7 +11,8 @@ import cloudscraper
|
|
11 |
import json
|
12 |
from typing import Optional
|
13 |
import datetime
|
14 |
-
|
|
|
15 |
load_dotenv() #idk why this shi
|
16 |
|
17 |
app = FastAPI()
|
@@ -105,7 +106,7 @@ def generate_search(query: str, systemprompt: Optional[str] = None, stream: bool
|
|
105 |
async def search_gpt(q: str, stream: Optional[bool] = False, systemprompt: Optional[str] = None):
|
106 |
if not q:
|
107 |
raise HTTPException(status_code=400, detail="Query parameter 'q' is required")
|
108 |
-
|
109 |
if stream:
|
110 |
return StreamingResponse(
|
111 |
generate_search(q, systemprompt=systemprompt, stream=True),
|
@@ -161,7 +162,7 @@ async def get_completion(payload: Payload, request: Request):
|
|
161 |
status_code=400,
|
162 |
detail=f"Model '{model_to_use}' is not available. Check /models for the available model list."
|
163 |
)
|
164 |
-
|
165 |
# Proceed with the request handling
|
166 |
payload_dict = payload.dict()
|
167 |
payload_dict["model"] = model_to_use
|
@@ -224,7 +225,7 @@ async def generate_image(
|
|
224 |
# Validate the image endpoint
|
225 |
if not image_endpoint:
|
226 |
raise HTTPException(status_code=500, detail="Image endpoint not configured in environment variables.")
|
227 |
-
|
228 |
# Handle GET and POST prompts
|
229 |
if request.method == "POST":
|
230 |
try:
|
@@ -330,6 +331,92 @@ def load_model_ids(json_file_path):
|
|
330 |
except json.JSONDecodeError:
|
331 |
print("Error: Invalid JSON format in models.json.")
|
332 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
@app.on_event("startup")
|
334 |
async def startup_event():
|
335 |
global available_model_ids
|
|
|
11 |
import json
|
12 |
from typing import Optional
|
13 |
import datetime
|
14 |
+
from usage_tracker import UsageTracker
|
15 |
+
usage_tracker = UsageTracker()
|
16 |
load_dotenv() #idk why this shi
|
17 |
|
18 |
app = FastAPI()
|
|
|
106 |
async def search_gpt(q: str, stream: Optional[bool] = False, systemprompt: Optional[str] = None):
|
107 |
if not q:
|
108 |
raise HTTPException(status_code=400, detail="Query parameter 'q' is required")
|
109 |
+
usage_tracker.record_request(endpoint="/searchgpt")
|
110 |
if stream:
|
111 |
return StreamingResponse(
|
112 |
generate_search(q, systemprompt=systemprompt, stream=True),
|
|
|
162 |
status_code=400,
|
163 |
detail=f"Model '{model_to_use}' is not available. Check /models for the available model list."
|
164 |
)
|
165 |
+
usage_tracker.record_request(model=model_to_use, endpoint="/chat/completions")
|
166 |
# Proceed with the request handling
|
167 |
payload_dict = payload.dict()
|
168 |
payload_dict["model"] = model_to_use
|
|
|
225 |
# Validate the image endpoint
|
226 |
if not image_endpoint:
|
227 |
raise HTTPException(status_code=500, detail="Image endpoint not configured in environment variables.")
|
228 |
+
usage_tracker.record_request(endpoint="/images/generations")
|
229 |
# Handle GET and POST prompts
|
230 |
if request.method == "POST":
|
231 |
try:
|
|
|
331 |
except json.JSONDecodeError:
|
332 |
print("Error: Invalid JSON format in models.json.")
|
333 |
return []
|
334 |
+
@app.get("/usage")
|
335 |
+
async def get_usage(days: int = 7):
|
336 |
+
"""Retrieve usage statistics"""
|
337 |
+
return usage_tracker.get_usage_summary(days)
|
338 |
+
|
339 |
+
@app.get("/usage/page", response_class=HTMLResponse)
|
340 |
+
async def usage_page():
|
341 |
+
"""Serve an HTML page showing usage statistics"""
|
342 |
+
# Retrieve usage data
|
343 |
+
usage_data = usage_tracker.get_usage_summary()
|
344 |
+
|
345 |
+
# Create an HTML template with usage information
|
346 |
+
html_content = f"""
|
347 |
+
<!DOCTYPE html>
|
348 |
+
<html lang="en">
|
349 |
+
<head>
|
350 |
+
<meta charset="UTF-8">
|
351 |
+
<title>API Usage Statistics</title>
|
352 |
+
<style>
|
353 |
+
body {{ font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; }}
|
354 |
+
h1 {{ color: #333; }}
|
355 |
+
table {{ width: 100%; border-collapse: collapse; margin-bottom: 20px; }}
|
356 |
+
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
|
357 |
+
th {{ background-color: #f2f2f2; }}
|
358 |
+
</style>
|
359 |
+
</head>
|
360 |
+
<body>
|
361 |
+
<h1>API Usage Statistics</h1>
|
362 |
+
|
363 |
+
<h2>Total Requests: {usage_data['total_requests']}</h2>
|
364 |
+
|
365 |
+
<h3>Model Usage</h3>
|
366 |
+
<table>
|
367 |
+
<tr>
|
368 |
+
<th>Model</th>
|
369 |
+
<th>Total Requests</th>
|
370 |
+
<th>First Used</th>
|
371 |
+
<th>Last Used</th>
|
372 |
+
</tr>
|
373 |
+
{"".join([f"""
|
374 |
+
<tr>
|
375 |
+
<td>{model}</td>
|
376 |
+
<td>{model_data['total_requests']}</td>
|
377 |
+
<td>{model_data['first_used']}</td>
|
378 |
+
<td>{model_data['last_used']}</td>
|
379 |
+
</tr>""" for model, model_data in usage_data['models'].items()])}
|
380 |
+
</table>
|
381 |
+
|
382 |
+
<h3>API Endpoint Usage</h3>
|
383 |
+
<table>
|
384 |
+
<tr>
|
385 |
+
<th>Endpoint</th>
|
386 |
+
<th>Total Requests</th>
|
387 |
+
<th>First Used</th>
|
388 |
+
<th>Last Used</th>
|
389 |
+
</tr>
|
390 |
+
{"".join([f"""
|
391 |
+
<tr>
|
392 |
+
<td>{endpoint}</td>
|
393 |
+
<td>{endpoint_data['total_requests']}</td>
|
394 |
+
<td>{endpoint_data['first_used']}</td>
|
395 |
+
<td>{endpoint_data['last_used']}</td>
|
396 |
+
</tr>""" for endpoint, endpoint_data in usage_data['api_endpoints'].items()])}
|
397 |
+
</table>
|
398 |
+
|
399 |
+
<h3>Daily Usage (Last 7 Days)</h3>
|
400 |
+
<table>
|
401 |
+
<tr>
|
402 |
+
<th>Date</th>
|
403 |
+
<th>Entity</th>
|
404 |
+
<th>Requests</th>
|
405 |
+
</tr>
|
406 |
+
{"".join([f"""
|
407 |
+
{"".join([f"""
|
408 |
+
<tr>
|
409 |
+
<td>{date}</td>
|
410 |
+
<td>{entity}</td>
|
411 |
+
<td>{requests}</td>
|
412 |
+
</tr>""" for entity, requests in date_data.items()])}
|
413 |
+
""" for date, date_data in usage_data['recent_daily_usage'].items()])}
|
414 |
+
</table>
|
415 |
+
</body>
|
416 |
+
</html>
|
417 |
+
"""
|
418 |
+
|
419 |
+
return HTMLResponse(content=html_content)
|
420 |
@app.on_event("startup")
|
421 |
async def startup_event():
|
422 |
global available_model_ids
|