import logging from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse from fastapi.middleware.cors import CORSMiddleware from datasets import load_dataset import numpy as np import os app = FastAPI() # --------------------------------------------------------- # Logging # --------------------------------------------------------- logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s" ) logger = logging.getLogger(__name__) # --------------------------------------------------------- # CORS # --------------------------------------------------------- app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # --------------------------------------------------------- # Load Hugging Face dataset (cached) # --------------------------------------------------------- DATASET_NAME = "kurry/sp500_earnings_transcripts" dataset_cache = None def load_hf_dataset(): global dataset_cache if dataset_cache is None: logger.info(f"Loading HF dataset: {DATASET_NAME}") dataset_cache = load_dataset(DATASET_NAME, split="train") logger.info(f"Loaded {len(dataset_cache)} rows from HF dataset") return dataset_cache # --------------------------------------------------------- # JSON-safe conversion # --------------------------------------------------------- def to_json_safe(obj): if isinstance(obj, (np.integer,)): return int(obj) if isinstance(obj, (np.floating,)): return float(obj) if isinstance(obj, (np.ndarray, list)): return [to_json_safe(x) for x in obj] if isinstance(obj, dict): return {k: to_json_safe(v) for k, v in obj.items()} return obj # --------------------------------------------------------- # Serve index.html # --------------------------------------------------------- @app.get("/", response_class=HTMLResponse) def serve_index(): if not os.path.exists("index.html"): return "