-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_gguf.py
More file actions
727 lines (612 loc) · 25.3 KB
/
app_gguf.py
File metadata and controls
727 lines (612 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
"""
GGUF Forge - Automatic GGUF Model Conversion Service
Main application entry point.
"""
import os
import sys
import secrets
import logging
import asyncio
from pathlib import Path
from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends, Request, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.security import APIKeyCookie
from passlib.context import CryptContext
from dotenv import load_dotenv
# --- Configuration & Setup ---
load_dotenv()
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger("GGUF_Forge")
# Custom filter to suppress frequent polling endpoints from access logs
class EndpointFilter(logging.Filter):
"""Filter out frequent polling endpoints from uvicorn access logs."""
def __init__(self, endpoints_to_skip: list):
super().__init__()
self.endpoints_to_skip = endpoints_to_skip
def filter(self, record: logging.LogRecord) -> bool:
message = record.getMessage()
for endpoint in self.endpoints_to_skip:
if endpoint in message:
return False
return True
# Apply filter to uvicorn access logger
uvicorn_access_logger = logging.getLogger("uvicorn.access")
uvicorn_access_logger.addFilter(EndpointFilter([
"/api/status/all",
"/api/status/model/",
"/api/requests/all",
"/api/requests/my",
"/api/tickets/all",
"/api/tickets/my",
"/api/tickets/"
]))
# Handle paths for PyInstaller (Frozen) vs Dev
if getattr(sys, 'frozen', False):
BASE_DIR = Path(sys.executable).parent
BUNDLE_DIR = Path(sys._MEIPASS)
else:
BASE_DIR = Path(__file__).parent.absolute()
BUNDLE_DIR = BASE_DIR
TEMPLATES_DIR = BUNDLE_DIR / "templates"
STATIC_DIR = BUNDLE_DIR / "static"
# Model download path - configurable via MODEL_DOWNLOAD_PATH or HF_HOME environment variable
# Priority: MODEL_DOWNLOAD_PATH > HF_HOME > default .cache folder
model_download_env = os.getenv("MODEL_DOWNLOAD_PATH", "")
hf_home_env = os.getenv("HF_HOME", "")
if model_download_env:
# Use explicit MODEL_DOWNLOAD_PATH - support both relative and absolute paths
model_path = Path(model_download_env)
if model_path.is_absolute():
CACHE_DIR = model_path
else:
CACHE_DIR = (BASE_DIR / model_path).resolve()
elif hf_home_env:
# Use HuggingFace home directory
CACHE_DIR = Path(hf_home_env) / "gguf-forge"
else:
# Default: .cache subdirectory
CACHE_DIR = BASE_DIR / ".cache"
# Llama.cpp directory - configurable via LLAMA_CPP_DIR environment variable
llama_cpp_env = os.getenv("LLAMA_CPP_DIR", "")
if llama_cpp_env:
# Use environment variable - support both relative and absolute paths
llama_cpp_path = Path(llama_cpp_env)
if llama_cpp_path.is_absolute():
LLAMA_CPP_DIR = llama_cpp_path
else:
# Relative path - resolve relative to BASE_DIR
LLAMA_CPP_DIR = (BASE_DIR / llama_cpp_path).resolve()
else:
# Default: llama.cpp subdirectory
LLAMA_CPP_DIR = BASE_DIR / "llama.cpp"
DB_PATH = BASE_DIR / "gguf_app.db"
CACHE_DIR.mkdir(parents=True, exist_ok=True)
# Llama.cpp Constants - available quant types
QUANTS = ["Q2_K", "Q3_K_S", "Q3_K_M", "Q3_K_L", "Q4_0", "Q4_K_S", "Q4_K_M", "Q5_0", "Q5_K_S", "Q5_K_M", "Q6_K", "Q8_0"]
PARALLEL_QUANT_JOBS = int(os.getenv("PARALLEL_QUANT_JOBS", "1")) # Default 1 for safer resource usage
# Server configuration
SERVER_HOST = os.getenv("HOST", "0.0.0.0")
SERVER_PORT = int(os.getenv("PORT", "8000"))
# Security
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
cookie_sec = APIKeyCookie(name="session_token", auto_error=False)
# HuggingFace OAuth Configuration
OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID", "")
OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET", "")
OAUTH_REDIRECT_URI = os.getenv("OAUTH_REDIRECT_URI", "http://localhost:8000/auth/callback")
# --- Initialize Modules ---
from database import init_db, get_db_connection, set_db_path
from security import RateLimiter, BotDetector, SpamProtection
from managers import set_paths as set_manager_paths
from workflow import set_workflow_config, running_workflows, ModelQueue, set_model_queue, get_model_queue
from websocket_manager import manager as ws_manager
# Set paths for modules
set_db_path(DB_PATH)
set_manager_paths(BASE_DIR, LLAMA_CPP_DIR)
set_workflow_config(CACHE_DIR, LLAMA_CPP_DIR, QUANTS, PARALLEL_QUANT_JOBS)
# Initialize security instances
rate_limiter = RateLimiter(requests_per_minute=120, requests_per_second=15)
bot_detector = BotDetector()
spam_protection = SpamProtection(max_requests_per_hour=10, max_pending_per_user=5)
# --- User Authentication Helpers ---
async def get_current_user(request: Request):
"""Get current user - checks both admin users and OAuth users.
Returns a dict-like row with additional 'is_oauth' and 'avatar_url' fields
to avoid needing separate get_oauth_user calls.
"""
token = request.cookies.get("session_token")
if not token:
return None
conn = await get_db_connection()
# Check admin users first (legacy password-based admins)
await conn.execute("SELECT *, 'admin' as user_type, 0 as is_oauth, NULL as avatar_url FROM users WHERE api_key = ?", (token,))
row = await conn.fetchone()
if row:
await conn.close()
return row
# Check OAuth users - role is now stored in database
await conn.execute("SELECT *, 'oauth' as user_type, 1 as is_oauth FROM oauth_users WHERE session_token = ?", (token,))
oauth_user = await conn.fetchone()
await conn.close()
return oauth_user
async def get_oauth_user(request: Request):
"""Get OAuth user only (not admin).
DEPRECATED: Use get_current_user() and check 'is_oauth' field instead.
Kept for backwards compatibility.
"""
user = await get_current_user(request)
if user and user.get('is_oauth'):
return user
return None
async def require_admin(request: Request):
user = await get_current_user(request)
if not user or user['role'] != 'admin':
from fastapi import HTTPException
raise HTTPException(status_code=403, detail="Admin access required")
return user
# --- App Lifespan ---
@asynccontextmanager
async def lifespan(app: FastAPI):
await init_db()
# Initialize and start the model queue worker
queue = ModelQueue()
set_model_queue(queue)
queue.start_worker()
logger.info("Model queue system initialized")
conn = await get_db_connection()
# Startup cleanup: Check for stuck 'processing' jobs from crashed server
processing_statuses = ['pending', 'initializing', 'downloading', 'converting', 'quantizing', 'uploading']
await conn.execute(
f"SELECT * FROM models WHERE status IN ({','.join(['?']*len(processing_statuses))})",
tuple(processing_statuses)
)
stuck_jobs = await conn.fetchall()
if stuck_jobs:
logger.warning(f"Found {len(stuck_jobs)} stuck processing jobs from previous session")
for job in stuck_jobs:
model_id = job['id']
hf_repo_id = job['hf_repo_id']
old_status = job['status']
# Update the model status to indicate it was interrupted
await conn.execute(
"UPDATE models SET status = ?, error_details = ? WHERE id = ?",
("interrupted", f"Server shutdown while status was '{old_status}'. Job can be restarted.", model_id)
)
# Check if there was an associated request that needs to be reset
await conn.execute(
"SELECT * FROM requests WHERE hf_repo_id = ? AND status = 'approved'",
(hf_repo_id,)
)
existing_request = await conn.fetchone()
if existing_request:
await conn.execute(
"UPDATE requests SET status = 'pending' WHERE id = ?",
(existing_request['id'],)
)
logger.info(f"Reset request #{existing_request['id']} for {hf_repo_id} back to pending")
logger.info(f"Marked stuck job {model_id} ({hf_repo_id}) as interrupted")
await conn.commit()
logger.info("Startup cleanup complete")
# Create admin user if not exists
await conn.execute("SELECT * FROM users WHERE role = 'admin'")
admin = await conn.fetchone()
if not admin:
key = secrets.token_urlsafe(16)
pwd = secrets.token_urlsafe(8)
hashed = pwd_context.hash(pwd)
await conn.execute("INSERT INTO users (username, hashed_password, role, api_key) VALUES (?, ?, ?, ?)",
("admin", hashed, "admin", key))
await conn.commit()
creds_text = f"""
==================================================
ADMIN CREDENTIALS (GENERATED)
==================================================
Username: admin
Password: {pwd}
API Key: {key}
==================================================
"""
print(creds_text)
try:
with open(BASE_DIR / "creds.txt", "w") as f:
f.write(creds_text)
except Exception as e:
print(f"Failed to write creds.txt: {e}")
await conn.close()
# Background cleanup loop for in-memory rate/spam limiters (prevents memory bloat on bot traffic)
async def _security_cleanup_loop():
while True:
try:
await rate_limiter.cleanup()
await spam_protection.cleanup()
except Exception:
logger.exception("Security cleanup loop error")
await asyncio.sleep(60)
cleanup_task = asyncio.create_task(_security_cleanup_loop())
try:
yield
finally:
cleanup_task.cancel()
try:
await cleanup_task
except Exception:
pass
# Close database connection pool on shutdown
from database import close_pool
await close_pool()
# --- FastAPI App ---
app = FastAPI(lifespan=lifespan)
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
templates = Jinja2Templates(directory=str(TEMPLATES_DIR))
# --- Security Middleware ---
@app.middleware("http")
async def security_middleware(request: Request, call_next):
"""Apply rate limiting and bot detection to all requests."""
# Get client IP (handle proxies)
forwarded_for = request.headers.get("X-Forwarded-For")
if forwarded_for:
client_ip = forwarded_for.split(",")[0].strip()
else:
client_ip = request.client.host if request.client else "unknown"
path = request.url.path
# Skip security checks for static files
if path.startswith("/static"):
return await call_next(request)
# Skip rate limiting for frequent polling endpoints
# These are called 2-4 times per second for live updates
polling_endpoints = [
"/api/status/all",
"/api/status/model/", # Dynamic: /api/status/model/{id}
"/api/requests/all",
"/api/requests/my",
"/api/tickets/all",
"/api/tickets/my",
"/api/tickets/", # Dynamic: /api/tickets/{id}/messages
]
skip_rate_limit = any(path == ep or path.startswith(ep) for ep in polling_endpoints)
if not skip_rate_limit:
allowed, reason = await rate_limiter.is_allowed(client_ip)
if not allowed:
logger.warning(f"Rate limit: {client_ip} - {path} - {reason}")
return JSONResponse(
status_code=429,
content={"detail": reason}
)
# Bot detection for non-API routes
user_agent = request.headers.get("User-Agent", "")
is_bot, bot_reason = bot_detector.is_suspicious(user_agent, path)
if is_bot and not path.startswith("/api/"):
logger.warning(f"Bot detected: {client_ip} - {user_agent[:50]} - {bot_reason}")
return JSONResponse(
status_code=403,
content={"detail": "Access denied"}
)
return await call_next(request)
# --- Configure and Include Routes ---
from routes import auth, models, requests, tickets, settings
# Configure route modules with dependencies
auth.configure(templates, pwd_context, OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_REDIRECT_URI)
models.configure(require_admin)
requests.configure(require_admin, get_current_user, spam_protection)
tickets.configure(require_admin, get_current_user)
settings.configure(require_admin)
# Include routers
app.include_router(auth.router)
app.include_router(models.router)
app.include_router(requests.router)
app.include_router(tickets.router)
app.include_router(settings.router)
# --- WebSocket Endpoint ---
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""WebSocket endpoint for real-time updates."""
# --- Basic security for WebSockets (middleware doesn't run for WS) ---
# Get client IP (handle proxies)
forwarded_for = websocket.headers.get("x-forwarded-for")
if forwarded_for:
client_ip = forwarded_for.split(",")[0].strip()
else:
client_ip = websocket.client.host if websocket.client else "unknown"
user_agent = websocket.headers.get("user-agent", "")
# Rate limit WS handshakes to reduce bot churn
allowed, reason = await rate_limiter.is_allowed(client_ip)
if not allowed:
try:
await websocket.close(code=1008, reason=reason)
finally:
return
# Bot detection (treat WS like a browser route)
is_bot, bot_reason = bot_detector.is_suspicious(user_agent, "/ws")
if is_bot:
logger.warning(f"Bot detected (ws): {client_ip} - {user_agent[:50]} - {bot_reason}")
try:
await websocket.close(code=1008, reason="Access denied")
finally:
return
# Resolve user from session cookie (can't reuse Request-based dependency here)
async def get_ws_user():
token = websocket.cookies.get("session_token")
if not token:
return None
conn = await get_db_connection()
try:
# Admin users (legacy)
await conn.execute("SELECT *, 'admin' as user_type FROM users WHERE api_key = ?", (token,))
row = await conn.fetchone()
if row:
return row
# OAuth users
await conn.execute("SELECT *, 'oauth' as user_type FROM oauth_users WHERE session_token = ?", (token,))
return await conn.fetchone()
finally:
await conn.close()
user = await get_ws_user()
# Parse channels from query params
requested_channels = websocket.query_params.getlist("channel")
if not requested_channels:
requested_channels = ["models"] # Default to models channel
# Restrict channels based on user role
allowed_channels = {"models"}
if user:
allowed_channels.add("my_requests")
if user.get("role") == "admin":
allowed_channels.update({"requests", "tickets"})
channels = [c for c in requested_channels if c in allowed_channels]
if not channels:
channels = ["models"]
await ws_manager.connect(websocket, channels)
try:
while True:
# Keep connection alive, handle incoming messages if needed
data = await websocket.receive_text()
# Client can send ping to keep alive
if data == "ping":
await websocket.send_text('{"type": "pong"}')
except WebSocketDisconnect:
await ws_manager.disconnect(websocket)
except Exception:
await ws_manager.disconnect(websocket)
# --- Main Routes ---
@app.get("/", response_class=HTMLResponse)
async def dashboard(request: Request):
user = await get_current_user(request)
# User now includes is_oauth and avatar_url fields - no need for separate query
return templates.TemplateResponse("index.html", {
"request": request,
"user": user['username'] if user else None,
"role": user['role'] if user else 'guest',
"oauth_avatar": user.get('avatar_url') if user else None,
"is_oauth": bool(user.get('is_oauth')) if user else False
})
@app.get("/settings", response_class=HTMLResponse)
async def settings_page(request: Request):
"""Settings page for user preferences."""
user = await get_current_user(request)
if not user:
return RedirectResponse(url="/login", status_code=303)
return templates.TemplateResponse("settings.html", {
"request": request,
"user": user['username'] if user else None,
"role": user['role'] if user else 'guest',
"oauth_avatar": user.get('avatar_url') if user else None,
"is_oauth": bool(user.get('is_oauth')) if user else False
})
@app.get("/api/health")
async def health_check():
"""Health check endpoint with database status."""
from database import test_connection, DB_TYPE
db_ok, db_msg = await test_connection()
return {
"status": "healthy" if db_ok else "degraded",
"database": {
"type": DB_TYPE,
"connected": db_ok,
"message": db_msg
},
"version": "1.0"
}
@app.get("/api/admin/db-info")
async def get_db_info(request: Request):
"""Admin only: Get database information."""
user = await require_admin(request)
from database import DB_TYPE, test_connection
db_ok, db_msg = await test_connection()
info = {
"type": DB_TYPE,
"connected": db_ok,
"message": db_msg
}
if DB_TYPE == "sqlite":
info["path"] = str(DB_PATH)
elif DB_TYPE == "mssql":
from database import MSSQL_HOST, MSSQL_PORT, MSSQL_DATABASE
info["host"] = MSSQL_HOST
info["port"] = MSSQL_PORT
info["database"] = MSSQL_DATABASE
return info
@app.get("/api/admin/check-update")
async def check_for_update(request: Request):
"""Admin only: Check if a git update is available."""
user = await require_admin(request)
try:
# Fetch latest from origin (without merging)
fetch_proc = await asyncio.create_subprocess_exec(
"git", "fetch", "origin",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
await fetch_proc.wait()
# Get current branch
branch_proc = await asyncio.create_subprocess_exec(
"git", "rev-parse", "--abbrev-ref", "HEAD",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await branch_proc.communicate()
current_branch = stdout.decode().strip()
# Get local commit hash
local_proc = await asyncio.create_subprocess_exec(
"git", "rev-parse", "HEAD",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await local_proc.communicate()
local_commit = stdout.decode().strip()[:7]
# Get remote commit hash
remote_proc = await asyncio.create_subprocess_exec(
"git", "rev-parse", f"origin/{current_branch}",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await remote_proc.communicate()
remote_commit = stdout.decode().strip()[:7]
# Count commits behind
behind_proc = await asyncio.create_subprocess_exec(
"git", "rev-list", "--count", f"HEAD..origin/{current_branch}",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await behind_proc.communicate()
commits_behind = int(stdout.decode().strip()) if stdout else 0
# Get latest commit message from origin
msg_proc = await asyncio.create_subprocess_exec(
"git", "log", "-1", "--format=%s", f"origin/{current_branch}",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await msg_proc.communicate()
latest_message = stdout.decode().strip()[:100]
return {
"update_available": commits_behind > 0,
"commits_behind": commits_behind,
"local_commit": local_commit,
"remote_commit": remote_commit,
"branch": current_branch,
"latest_message": latest_message if commits_behind > 0 else None
}
except Exception as e:
logger.exception("Failed to check for updates")
return {
"update_available": False,
"error": str(e)
}
@app.post("/api/admin/update-app")
async def update_app(request: Request):
"""Admin only: Pull latest updates and restart the server."""
user = await require_admin(request)
try:
# Get current branch
branch_proc = await asyncio.create_subprocess_exec(
"git", "rev-parse", "--abbrev-ref", "HEAD",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await branch_proc.communicate()
current_branch = stdout.decode().strip()
# Fetch and reset to origin (same as update scripts)
fetch_proc = await asyncio.create_subprocess_exec(
"git", "fetch", "--all",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
await fetch_proc.wait()
reset_proc = await asyncio.create_subprocess_exec(
"git", "reset", "--hard", f"origin/{current_branch}",
cwd=BASE_DIR,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await reset_proc.communicate()
if reset_proc.returncode != 0:
return {"status": "error", "message": f"Git reset failed: {stderr.decode()}"}
# Schedule server restart after response is sent
async def restart_server():
await asyncio.sleep(1) # Give time for response to be sent
logger.info("Restarting server after update...")
os.execv(sys.executable, [sys.executable] + sys.argv)
asyncio.create_task(restart_server())
return {
"status": "success",
"message": "Update complete. Server restarting...",
"branch": current_branch
}
except Exception as e:
logger.exception("Failed to update app")
return {"status": "error", "message": str(e)}
@app.get("/api/quants")
async def get_available_quants():
"""Get list of available quantization types."""
return {
"quants": QUANTS,
"descriptions": {
"Q2_K": "2-bit quantization (smallest, lowest quality)",
"Q3_K_S": "3-bit small quantization",
"Q3_K_M": "3-bit medium quantization",
"Q3_K_L": "3-bit large quantization",
"Q4_0": "4-bit legacy quantization",
"Q4_K_S": "4-bit small quantization (recommended for low memory)",
"Q4_K_M": "4-bit medium quantization (good balance)",
"Q5_0": "5-bit legacy quantization",
"Q5_K_S": "5-bit small quantization",
"Q5_K_M": "5-bit medium quantization (good quality)",
"Q6_K": "6-bit quantization (high quality)",
"Q8_0": "8-bit quantization (highest quality, largest size)"
}
}
@app.get("/api/dashboard/init")
async def dashboard_init(request: Request):
"""Consolidated endpoint for initial dashboard data.
Returns all data needed to initialize the dashboard in a single request,
reducing initial page load from 4 HTTP requests to 1.
"""
user = await get_current_user(request)
is_admin = user and user.get('role') == 'admin'
conn = await get_db_connection()
# Always get models (public)
await conn.execute("SELECT * FROM models ORDER BY created_at DESC LIMIT 50")
models = await conn.fetchall()
result = {
"models": [m.to_dict() for m in models],
"requests": [],
"tickets": [],
"my_requests": []
}
if is_admin:
# Admin gets pending requests and open tickets
await conn.execute("SELECT * FROM requests WHERE status = 'pending' ORDER BY created_at DESC")
requests = await conn.fetchall()
result["requests"] = [r.to_dict() for r in requests]
await conn.execute("""
SELECT t.*, r.hf_repo_id, r.requested_by
FROM tickets t
JOIN requests r ON t.request_id = r.id
WHERE t.status = 'open'
ORDER BY t.created_at DESC
""")
tickets = await conn.fetchall()
result["tickets"] = [t.to_dict() for t in tickets]
elif user:
# Regular user gets their own requests
await conn.execute(
"SELECT * FROM requests WHERE requested_by = ? ORDER BY created_at DESC",
(user['username'],)
)
my_requests = await conn.fetchall()
result["my_requests"] = [r.to_dict() for r in my_requests]
await conn.close()
return result
if __name__ == "__main__":
import uvicorn
print(f"Starting GGUF Forge on {SERVER_HOST}:{SERVER_PORT}...")
uvicorn.run("app_gguf:app", host=SERVER_HOST, port=SERVER_PORT, reload=False)