"""Dedicated Hugging Face Gradio Space for Levelian YouTube Study Mode Classifier.""" from __future__ import annotations import os import time from pathlib import Path import gradio as gr from fastapi import Request from fastapi.responses import JSONResponse from fastapi.routing import APIRoute try: import spaces # type: ignore @spaces.GPU def _zerogpu_probe() -> str: return "ok" except Exception: pass from study_classifier import classify_youtube_study, init_study_cache CACHE_DB = Path("/tmp/levelian_study_cache.db") init_study_cache(CACHE_DB) def _read_env(key: str) -> str: return os.environ.get(key, "").strip() async def health(): return JSONResponse( { "ok": True, "service": "levelian-study", "youtubeApiConfigured": bool(_read_env("YOUTUBE_API_KEY")), "groqConfigured": bool(_read_env("GROQ_API_KEY")), "geminiConfigured": bool(_read_env("GEMINI_API_KEY")), } ) async def study_classify(request: Request): try: body = await request.json() except Exception: return JSONResponse(status_code=400, content={"ok": False, "error": "INVALID_JSON"}) res = classify_youtube_study( video_id=str(body.get("videoId") or "").strip(), title=str(body.get("title") or "").strip(), channel=str(body.get("channel") or "").strip(), db_path=CACHE_DB, env_reader=_read_env, ) return JSONResponse({"ok": True, **res}) with gr.Blocks(title="Levelian Study AI") as demo: gr.Markdown( "# Levelian YouTube Study Classifier\n" "Status: **Live** ยท Endpoints: `GET /gradio_api/health`, `POST /gradio_api/api/study/classify`" ) if __name__ == "__main__": demo.launch(prevent_thread_lock=True) custom_routes = [ APIRoute("/gradio_api/health", health, methods=["GET"]), APIRoute("/gradio_api/api/study/classify", study_classify, methods=["POST"]), ] for r in reversed(custom_routes): demo.app.router.routes.insert(0, r) while True: time.sleep(3600)