Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 18,517 Bytes
aa5e463 26495e8 cec65a4 bf5b392 aa5e463 cec65a4 aa5e463 26495e8 bf5b392 aa5e463 a6c2b21 aa5e463 e3f7f9a 26495e8 d4d8310 26495e8 bf5b392 26495e8 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 b967741 aa5e463 a749315 c261f1c a749315 c261f1c a749315 cec65a4 26495e8 aa5e463 26495e8 aa5e463 26495e8 aa5e463 26495e8 aa5e463 cec65a4 aa5e463 2ec4a9a aa5e463 2ec4a9a e64875c 2ec4a9a e64875c 2ec4a9a 844ddfc a749315 26495e8 a749315 26495e8 844ddfc cec65a4 a749315 844ddfc 2ec4a9a 9eaf22a 2ec4a9a a749315 26495e8 844ddfc cec65a4 844ddfc ca62031 844ddfc ca62031 844ddfc ca62031 844ddfc cec65a4 844ddfc cec65a4 aa5e463 cec65a4 d4d8310 cec65a4 aa5e463 cec65a4 26495e8 cec65a4 26495e8 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 d4d8310 aa5e463 cec65a4 aa5e463 cec65a4 aa5e463 cec65a4 26495e8 cec65a4 26495e8 aa5e463 d4d8310 aa5e463 26495e8 aa5e463 bf5b392 750d70f bf5b392 750d70f bf5b392 750d70f e3f7f9a bf5b392 750d70f bf5b392 750d70f bf5b392 750d70f bf5b392 e3f7f9a bf5b392 e3f7f9a bf5b392 e3f7f9a bf5b392 e3f7f9a |
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 |
import os
import sqlite3
import base64
import json
from datetime import datetime
from typing import Optional
from fastapi import FastAPI, Request, Cookie, HTTPException, WebSocket, Header
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from huggingface_hub import whoami
import httpx
import websockets
app = FastAPI()
templates = Jinja2Templates(directory=".")
# Track active WebSocket connections
active_websockets = set()
# OAuth configuration from HF Spaces environment
OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET")
OAUTH_SCOPES = os.getenv("OAUTH_SCOPES", "openid profile")
SPACE_HOST = os.getenv("SPACE_HOST", "localhost:7860")
OPENID_PROVIDER_URL = os.getenv("OPENID_PROVIDER_URL", "https://huggingface.co")
# FAL API Key from environment
FAL_API_KEY = os.getenv("FAL_API_KEY", "")
# Database setup
DB_PATH = "/data/usage.db"
def init_db():
"""Initialize SQLite database"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS generations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
is_pro BOOLEAN NOT NULL,
generation_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
# Create index for faster queries
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_username_date
ON generations(username, generation_date)
""")
conn.commit()
conn.close()
init_db()
def get_daily_usage(username: str, date: str = None) -> int:
"""Get number of generations used today by user"""
if date is None:
date = datetime.now().date().isoformat()
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute(
"SELECT COUNT(*) FROM generations WHERE username = ? AND generation_date = ?",
(username, date)
)
count = cursor.fetchone()[0]
conn.close()
return count
def record_generation(username: str, is_pro: bool):
"""Record a new generation - called every time user clicks 'Start Generation'"""
date = datetime.now().date().isoformat()
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO generations (username, is_pro, generation_date) VALUES (?, ?, ?)",
(username, is_pro, date)
)
conn.commit()
conn.close()
def can_start_generation(username: str, is_pro: bool) -> tuple[bool, int, int]:
"""Check if user can start a new generation. Returns (can_start, used, limit)"""
used = get_daily_usage(username)
limit = 15 if is_pro else 2
return used < limit, used, limit
def get_origin_from_request(request: Request) -> str:
"""Get the origin (scheme + host) from the request, detecting HTTPS from proxy headers"""
# Check proxy headers for original protocol (common when behind reverse proxy)
proto = request.headers.get("X-Forwarded-Proto", "")
ssl = request.headers.get("X-Forwarded-Ssl", "")
# Get host from headers (handles both direct access and proxy)
host = request.headers.get("X-Forwarded-Host") or request.headers.get("Host") or ""
# Determine scheme
if proto == "https" or ssl == "on":
scheme = "https"
elif ".hf.space" in host or "huggingface.co" in host:
# Force HTTPS for Hugging Face domains (they always serve over HTTPS)
scheme = "https"
else:
scheme = request.url.scheme or "https"
# Build origin URL
if host:
return f"{scheme}://{host}"
# Fallback to SPACE_HOST environment variable with HTTPS
return f"https://{SPACE_HOST}"
def get_token_from_request(cookie_token: Optional[str], auth_header: Optional[str]) -> Optional[str]:
"""Extract access token from either cookie or Authorization header"""
# Try Authorization header first (Bearer token)
if auth_header:
parts = auth_header.split()
if len(parts) == 2 and parts[0].lower() == "bearer":
return parts[1]
# Fall back to cookie
return cookie_token
async def exchange_code_for_token(code: str, redirect_uri: str) -> dict:
"""Exchange OAuth code for access token"""
token_url = f"{OPENID_PROVIDER_URL}/oauth/token"
credentials = f"{OAUTH_CLIENT_ID}:{OAUTH_CLIENT_SECRET}"
b64_credentials = base64.b64encode(credentials.encode()).decode()
headers = {
"Authorization": f"Basic {b64_credentials}",
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
"client_id": OAUTH_CLIENT_ID
}
async with httpx.AsyncClient() as client:
response = await client.post(token_url, data=data, headers=headers)
response.raise_for_status()
return response.json()
async def get_user_info(access_token: str) -> dict:
"""Get user info from access token using whoami"""
try:
user_data = whoami(token=access_token)
return {
"username": user_data.get("name"),
"is_pro": user_data.get("isPro", False),
"avatar": user_data.get("avatarUrl"),
"fullname": user_data.get("fullname", user_data.get("name")),
"email": user_data.get("email")
}
except Exception as e:
print(f"Failed to get user info: {e}")
raise HTTPException(status_code=401, detail="Failed to get user information")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
"""Home page - client-side auth with popup OAuth"""
# Return template - authentication will be handled client-side
return templates.TemplateResponse("index.html", {
"request": request,
"oauth_client_id": OAUTH_CLIENT_ID
})
@app.get("/api/auth/login")
async def auth_login(request: Request, state: Optional[str] = None):
"""OAuth login - stores state in cookie and redirects to HF OAuth"""
# Dynamically detect origin from request
origin = get_origin_from_request(request)
redirect_uri = f"{origin}/oauth/callback"
# Generate or use provided state
oauth_state = state or os.urandom(16).hex()
# Build OAuth authorize URL
auth_url = f"https://huggingface.co/oauth/authorize"
auth_url += f"?response_type=code"
auth_url += f"&client_id={OAUTH_CLIENT_ID}"
auth_url += f"&redirect_uri={redirect_uri}"
auth_url += f"&scope=openid profile"
auth_url += f"&state={oauth_state}"
# Create response that redirects to HF OAuth
response = RedirectResponse(url=auth_url, status_code=302)
# Store state in cookie for validation in callback
# Note: samesite="none" is required for iframe/cross-site contexts
if not state: # Only set cookie if state wasn't provided
response.set_cookie(
key="hf_oauth_state",
value=oauth_state,
httponly=True,
samesite="none", # Required for iframe/third-party context
secure=True,
max_age=300, # 5 minutes
path="/"
)
return response
@app.post("/api/auth/exchange")
async def auth_exchange(request: Request, code: str, state: str, hf_oauth_state: Optional[str] = Cookie(None)):
"""Exchange OAuth code for access token - called from callback page"""
# Validate state from cookie
if not hf_oauth_state or state != hf_oauth_state:
raise HTTPException(status_code=400, detail="Invalid or expired OAuth state")
origin = get_origin_from_request(request)
redirect_uri = f"{origin}/oauth/callback"
try:
token_data = await exchange_code_for_token(code, redirect_uri)
access_token = token_data.get("access_token")
if not access_token:
raise HTTPException(status_code=400, detail="No access token received")
# Get user info
user_info = await get_user_info(access_token)
# Return token and user info
response = JSONResponse({
"token": access_token,
"namespace": user_info["username"]
})
response.delete_cookie("hf_oauth_state")
# Also set access_token cookie for WebSocket authentication
response.set_cookie(
key="access_token",
value=access_token,
httponly=True,
samesite="none",
secure=True,
max_age=30 * 24 * 60 * 60,
path="/"
)
return response
except Exception as e:
response = JSONResponse(
{"error": str(e)},
status_code=400
)
response.delete_cookie("hf_oauth_state")
raise HTTPException(status_code=400, detail=f"Token exchange failed: {str(e)}")
@app.get("/oauth/callback", response_class=HTMLResponse)
async def oauth_callback(request: Request):
"""OAuth callback page - exchanges code for token client-side"""
callback_html = """
<!DOCTYPE html>
<html>
<head><title>Authenticating...</title></head>
<body style="font-family: sans-serif; padding: 40px; text-align: center;">
<h2>Authenticating...</h2>
<p>Please wait while we complete your login.</p>
<script>
(async function() {
const params = new URLSearchParams(window.location.search);
const code = params.get('code');
const state = params.get('state');
const error = params.get('error');
if (error) {
document.body.innerHTML = '<h2>Authentication failed</h2><p>' + error + '</p>';
setTimeout(() => window.location.href = '/', 3000);
return;
}
if (!code || !state) {
document.body.innerHTML = '<h2>Authentication failed</h2><p>Missing authorization code</p>';
setTimeout(() => window.location.href = '/', 3000);
return;
}
try {
// Exchange code for token
const response = await fetch('/api/auth/exchange?code=' + code + '&state=' + state, {
method: 'POST',
credentials: 'same-origin'
});
if (!response.ok) {
const data = await response.json().catch(() => ({ detail: 'Unknown error' }));
throw new Error(data.detail || data.error || 'Failed to exchange code for token');
}
const data = await response.json();
// Store in localStorage
const authState = {
token: data.token,
user: { username: data.namespace }
};
localStorage.setItem('HF_AUTH_STATE', JSON.stringify(authState));
// Redirect back to home
window.location.href = '/';
} catch (err) {
document.body.innerHTML = '<h2>Authentication failed</h2><p>' + err.message + '</p><p><a href="/">Return to app</a></p>';
}
})();
</script>
</body>
</html>
"""
return HTMLResponse(content=callback_html)
@app.get("/api/whoami")
async def whoami_endpoint(authorization: Optional[str] = Header(None)):
"""Validate token and return user info"""
if not authorization:
raise HTTPException(status_code=401, detail="No authorization header")
# Extract Bearer token
parts = authorization.split()
if len(parts) != 2 or parts[0].lower() != "bearer":
raise HTTPException(status_code=401, detail="Invalid authorization header format")
access_token = parts[1]
try:
user_info = await get_user_info(access_token)
# Get usage info
can_start, used, limit = can_start_generation(user_info["username"], user_info["is_pro"])
return {
"username": user_info["username"],
"fullname": user_info["fullname"],
"is_pro": user_info["is_pro"],
"avatar": user_info.get("avatar"),
"can_start": can_start,
"sessions_used": used,
"sessions_limit": limit
}
except Exception as e:
raise HTTPException(status_code=401, detail=f"Invalid token: {str(e)}")
@app.post("/api/start-session")
async def start_session(access_token: Optional[str] = Cookie(None), authorization: Optional[str] = Header(None)):
"""Start a new generation - counts towards daily limit"""
token = get_token_from_request(access_token, authorization)
if not token:
raise HTTPException(status_code=401, detail="Not authenticated")
try:
user_info = await get_user_info(token)
except:
raise HTTPException(status_code=401, detail="Invalid session")
can_start, used, limit = can_start_generation(user_info["username"], user_info["is_pro"])
if not can_start:
raise HTTPException(
status_code=429,
detail=f"Daily limit reached. You've used {used}/{limit} generations today."
)
# Record this generation
record_generation(user_info["username"], user_info["is_pro"])
# Get updated count
new_count = get_daily_usage(user_info["username"])
return {
"success": True,
"sessions_used": new_count,
"sessions_limit": limit
}
@app.get("/api/check-limits")
async def check_limits(access_token: Optional[str] = Cookie(None), authorization: Optional[str] = Header(None)):
"""Check current usage limits"""
token = get_token_from_request(access_token, authorization)
if not token:
raise HTTPException(status_code=401, detail="Not authenticated")
try:
user_info = await get_user_info(token)
except:
raise HTTPException(status_code=401, detail="Invalid session")
can_start, used, limit = can_start_generation(user_info["username"], user_info["is_pro"])
return {
"can_start": can_start,
"sessions_used": used,
"sessions_limit": limit,
"is_pro": user_info["is_pro"]
}
@app.post("/api/logout")
async def logout():
"""Logout user"""
response = JSONResponse({"success": True})
response.delete_cookie("access_token")
return response
@app.get("/health")
async def health():
"""Health check endpoint"""
return {
"status": "ok",
"oauth_enabled": bool(OAUTH_CLIENT_ID),
"fal_api_key_configured": bool(FAL_API_KEY)
}
@app.websocket("/ws/video-gen")
async def websocket_video_gen(websocket: WebSocket, user_fal_key: Optional[str] = None):
"""WebSocket proxy to FAL API - keeps API key secret"""
from fastapi import WebSocket
import websockets
import json
await websocket.accept()
# Track this connection
active_websockets.add(websocket)
print(f"WebSocket connected. Active connections: {len(active_websockets)}")
# Get user from cookie
access_token = websocket.cookies.get("access_token")
if not access_token:
await websocket.close(code=1008, reason="Not authenticated")
return
try:
user_info = await get_user_info(access_token)
except:
await websocket.close(code=1008, reason="Invalid session")
return
# If user provided their own FAL key, use it (bypass limits)
if user_fal_key:
fal_key_to_use = user_fal_key
else:
# Check if user can start session with server FAL key
can_start, used, limit = can_start_generation(user_info["username"], user_info["is_pro"])
if not can_start:
await websocket.close(code=1008, reason=f"Daily limit reached ({used}/{limit})")
return
if not FAL_API_KEY:
await websocket.close(code=1011, reason="FAL API key not configured")
return
fal_key_to_use = FAL_API_KEY
# Fetch temporary FAL token
try:
async with httpx.AsyncClient() as client:
response = await client.post(
"https://rest.alpha.fal.ai/tokens/",
headers={
"Content-Type": "application/json",
"Authorization": f"Key {fal_key_to_use}"
},
json={
"allowed_apps": ["krea-wan-14b"],
"token_expiration": 5000
}
)
response.raise_for_status()
fal_token = response.json()
except Exception as e:
await websocket.close(code=1011, reason=f"Failed to get FAL token: {str(e)}")
return
# Connect to FAL WebSocket
fal_ws_url = f"wss://fal.run/fal-ai/krea-wan-14b/ws?fal_jwt_token={fal_token}"
try:
async with websockets.connect(fal_ws_url) as fal_ws:
# Relay messages between client and FAL
async def client_to_fal():
try:
while True:
# Receive from client
data = await websocket.receive_bytes()
# Forward to FAL
await fal_ws.send(data)
except Exception as e:
print(f"Client to FAL error: {e}")
async def fal_to_client():
try:
while True:
# Receive from FAL
message = await fal_ws.recv()
# Forward to client
if isinstance(message, str):
await websocket.send_text(message)
else:
await websocket.send_bytes(message)
except Exception as e:
print(f"FAL to client error: {e}")
# Run both directions concurrently
import asyncio
await asyncio.gather(
client_to_fal(),
fal_to_client()
)
except Exception as e:
print(f"WebSocket proxy error: {e}")
await websocket.close(code=1011, reason=str(e))
finally:
# Remove from active connections
active_websockets.discard(websocket)
print(f"WebSocket disconnected. Active connections: {len(active_websockets)}") |