Spaces:
Running
Running
File size: 12,659 Bytes
30ddf21 | 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 | """
server.py
---------
FastAPI REST API exposing KTU paper search + branded download + PDF notes watermarker
(with automatic competitor watermark removal) + live status pinging + secure Telegram,
Gemini, Groq, OpenRouter & HF ZeroGPU Vision Brain relay.
"""
from __future__ import annotations
import base64
import io
import json
import os
import random
import time
import httpx
from fastapi import FastAPI, HTTPException, Query, Request
from fastapi.responses import StreamingResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import ktu_repo as repo
import watermark as wm
app = FastAPI(title="KTU Papers API", version="1.6")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ---- branding config ----
BRAND_TEXT = os.getenv("BRAND_TEXT", "Downloaded from Keralatimetable.in")
LOGO_PATH = os.getenv("BRAND_LOGO_PATH", "keralattlogo.png")
LOGO_BYTES = open(LOGO_PATH, "rb").read() if LOGO_PATH and os.path.exists(LOGO_PATH) else None
ADMIN_KEY = os.getenv("ADMIN_KEY", "change-me")
# ---- Telegram & AI Secrets (Stored in Hugging Face Space Secrets, NEVER in GitHub) ----
TG_BOT_TOKEN = os.getenv("TG_BOT_TOKEN")
TG_CHAT_ID = os.getenv("TG_CHAT_ID", "-1004463800271")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
HF_TOKEN = os.getenv("HF_TOKEN", "")
# ---- Hugging Face URLs ----
DATASET_RAW_URL = "https://huggingface.co/datasets/KeralaTimetable/ktu-pyq-archive/resolve/main"
VISION_BRAIN_URLS_RAW = os.getenv(
"VISION_BRAIN_URLS",
"https://keralatimetable-ktu-vision-brain.hf.space/gradio_api/call/audit"
)
class DownloadReq(BaseModel):
handles: list[str]
watermark: bool = True
class WatermarkNotesReq(BaseModel):
pdfs_b64: list[str]
filename: str = "ktu_notes.pdf"
logo_opacity: float | None = None
logo_scale: float = 0.65
clean_existing: bool = True
extra_remove_keywords: list[str] | None = None
@app.get("/brand-logo")
def get_brand_logo():
"""Serves keralattlogo.png so watermark-tool.html can render a live opacity preview."""
if not LOGO_BYTES:
raise HTTPException(404, "keralattlogo.png not found on server")
return Response(content=LOGO_BYTES, media_type="image/png")
@app.get("/search")
def search(q: str = Query(..., min_length=2), year: int | None = None, limit: int = 50):
"""Search papers by course code or keyword with exact-match priority & deduplication."""
raw_results = repo.search(q, year=year, limit=limit + 100)
q_clean = q.upper().replace(" ", "")
exact_matches = []
for r in raw_results:
cc = r.get("course_code") or ""
if cc.upper().replace(" ", "") == q_clean:
exact_matches.append(r)
if exact_matches:
filtered_results = exact_matches
else:
filtered_results = raw_results
unique_results = []
seen_handles = set()
for r in filtered_results:
handle = r.get("handle")
if handle not in seen_handles:
seen_handles.add(handle)
unique_results.append(r)
final_results = unique_results[:limit]
return {"query": q, "year": year, "count": len(final_results), "results": final_results}
@app.get("/paper/{prefix}/{num}")
def paper(prefix: str, num: str):
"""Get one paper's details + resolve its direct PDF url."""
handle = f"{prefix}/{num}"
rows = repo.search(handle, limit=1)
pdf_url = repo.resolve_pdf_url(handle)
if not pdf_url:
raise HTTPException(404, "paper not found or no PDF")
return {"handle": handle, "pdf_url": pdf_url}
@app.get("/live-ping")
async def live_ping(url: str):
"""Bypasses browser CORS restrictions to run a backend ping test to KTU."""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
}
start_time = time.time()
try:
async with httpx.AsyncClient(timeout=6.0, verify=False) as client:
response = await client.get(url, headers=headers)
ping_ms = int((time.time() - start_time) * 1000)
if response.status_code < 400:
return {"status": "Online", "ping": ping_ms, "detail": "Operational"}
else:
return {"status": "Offline", "ping": "Error", "detail": f"HTTP {response.status_code}"}
except httpx.ConnectTimeout:
return {"status": "Offline", "ping": "Timeout", "detail": "Connection Timeout"}
except httpx.ConnectError as e:
if "SSL" in str(e) or "certificate" in str(e):
return {"status": "SSL Issue", "ping": "Blocked", "detail": "SSL Error"}
return {"status": "Offline", "ping": "Error", "detail": "Network Error"}
except Exception:
return {"status": "Offline", "ping": "Error", "detail": "Down"}
@app.post("/download")
async def download(req: DownloadReq):
"""Download from Hugging Face + (optionally) merge + watermark the requested papers."""
if not req.handles:
raise HTTPException(400, "no handles provided")
pdfs: list[bytes] = []
async with httpx.AsyncClient(follow_redirects=True) as client:
for h in req.handles:
safe_filename = f"{h.replace('/', '_')}.pdf"
file_url = f"{DATASET_RAW_URL}/{safe_filename}"
response = await client.get(file_url)
if response.status_code != 200:
print(f"FAILED: {file_url} returned status {response.status_code}")
raise HTTPException(404, f"no PDF for handle {h} in cloud archive")
pdfs.append(response.content)
if req.watermark:
out = wm.brand_papers(pdfs, logo_bytes=LOGO_BYTES, text=BRAND_TEXT)
else:
out = wm.merge_pdfs(pdfs) if len(pdfs) > 1 else pdfs[0]
fname = "ktu_papers.pdf"
return StreamingResponse(io.BytesIO(out), media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="{fname}"'})
@app.post("/watermark-notes")
async def watermark_notes(req: WatermarkNotesReq):
"""
Accepts uploaded PDF study notes or PPT slides (as base64), strips existing
third-party watermarks (like Ktunotes.live / Ktuspecial.in), and stamps them using
keralattlogo.png (LOGO_BYTES) with adjustable logo_opacity & logo_scale.
"""
if not req.pdfs_b64:
raise HTTPException(400, "No PDF files provided")
pdfs: list[bytes] = []
for b64_str in req.pdfs_b64:
if "," in b64_str:
b64_str = b64_str.split(",", 1)[1]
pdfs.append(base64.b64decode(b64_str))
out = wm.brand_papers(
pdfs,
logo_bytes=LOGO_BYTES,
text=BRAND_TEXT,
logo_scale=req.logo_scale,
logo_opacity=req.logo_opacity,
clean_existing=req.clean_existing,
extra_remove_keywords=req.extra_remove_keywords,
)
safe_name = (req.filename or "ktu_notes.pdf").replace('"', '')
return StreamingResponse(
io.BytesIO(out),
media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="{safe_name}"'}
)
@app.post("/telegram-auth")
async def telegram_auth(request: Request):
"""
Returns XOR-masked Telegram credentials from Hugging Face Secrets ONLY to keralatimetable.in.
"""
origin = (request.headers.get("origin") or request.headers.get("referer") or "").lower()
if "keralatimetable.in" not in origin and "localhost" not in origin and "127.0.0.1" not in origin:
raise HTTPException(403, "Forbidden origin")
if not TG_BOT_TOKEN:
raise HTTPException(500, "TG_BOT_TOKEN secret not configured in Space")
return {
"t": [ord(c) ^ 42 for c in TG_BOT_TOKEN],
"c": [ord(c) ^ 42 for c in TG_CHAT_ID]
}
@app.post("/gemini-auth")
async def gemini_auth(request: Request):
"""
Returns XOR-masked Gemini, Groq, OpenRouter & HF API keys from Hugging Face Secrets ONLY to keralatimetable.in.
"""
origin = (request.headers.get("origin") or request.headers.get("referer") or "").lower()
if "keralatimetable.in" not in origin and "localhost" not in origin and "127.0.0.1" not in origin:
raise HTTPException(403, "Forbidden origin")
g_keys = [k.strip() for k in GEMINI_API_KEY.split(",") if k.strip()]
gr_keys = [k.strip() for k in GROQ_API_KEY.split(",") if k.strip()]
or_keys = [k.strip() for k in OPENROUTER_API_KEY.split(",") if k.strip()]
hf_keys = [k.strip() for k in HF_TOKEN.split(",") if k.strip()]
if not g_keys and not gr_keys and not or_keys and not hf_keys:
raise HTTPException(500, "No AI API keys configured in Space Secrets")
c_gem = random.choice(g_keys) if g_keys else ""
c_grq = random.choice(gr_keys) if gr_keys else ""
c_ort = random.choice(or_keys) if or_keys else ""
c_hft = random.choice(hf_keys) if hf_keys else ""
return {
"k": [ord(c) ^ 42 for c in c_gem],
"g": [ord(c) ^ 42 for c in c_grq],
"o": [ord(c) ^ 42 for c in c_ort],
"h": [ord(c) ^ 42 for c in c_hft]
}
@app.post("/vision-audit")
async def vision_audit(request: Request):
"""
Authenticated Server Relay to KeralaTimetable/ktu-vision-brain (Qwen3-VL-8B on ZeroGPU).
"""
payload = await request.json()
payload_str = json.dumps(payload)
hf_keys = [k.strip() for k in HF_TOKEN.split(",") if k.strip()]
random.shuffle(hf_keys)
token_candidates = hf_keys if hf_keys else [""]
brain_urls = [u.strip() for u in VISION_BRAIN_URLS_RAW.split(",") if u.strip()]
random.shuffle(brain_urls)
client_ip = request.headers.get("x-forwarded-for") or (request.client.host if request.client else "")
last_error = "Vision Brain relay failed"
async with httpx.AsyncClient(timeout=90.0, follow_redirects=True) as client:
for api_url in brain_urls:
for token in token_candidates:
headers = {"Content-Type": "application/json"}
if token:
headers["Authorization"] = f"Bearer {token}"
elif client_ip:
headers["X-Forwarded-For"] = client_ip
try:
init_res = await client.post(
api_url,
headers=headers,
json={"data": [payload_str]}
)
if init_res.status_code != 200:
last_error = f"Queue HTTP {init_res.status_code}: {init_res.text}"
continue
event_id = init_res.json().get("event_id")
if not event_id:
last_error = "Missing event_id from Vision Brain Space"
continue
stream_url = f"{api_url}/{event_id}"
sse_res = await client.get(stream_url, headers=headers)
if sse_res.status_code != 200:
last_error = f"Stream HTTP {sse_res.status_code}: {sse_res.text}"
continue
if "event: error" in sse_res.text:
last_error = f"ZeroGPU error/quota: {sse_res.text}"
continue
last_data_line = None
for line in sse_res.text.splitlines():
if line.startswith("data:"):
last_data_line = line[5:].strip()
if not last_data_line or last_data_line == "null":
last_error = "Empty stream from Vision Brain Space"
continue
parsed_arr = json.loads(last_data_line)
if isinstance(parsed_arr, list) and len(parsed_arr) > 0:
inner = parsed_arr[0]
if isinstance(inner, str):
return json.loads(inner)
return inner
return {"status": "ok", "results": parsed_arr}
except Exception as e:
last_error = str(e)
continue
raise HTTPException(503, f"Vision Brain busy or quota reached: {last_error}")
@app.post("/admin/harvest")
def admin_harvest(key: str):
"""Re-harvest the repository index. Protect with ADMIN_KEY env var."""
if key != ADMIN_KEY:
raise HTTPException(403, "forbidden")
n = repo.harvest()
return {"stored": n}
|