Spaces:
Sleeping
Sleeping
Switch API to load Hugging Face dataset
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
-
import json
|
| 2 |
import logging
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
|
| 7 |
import numpy as np
|
| 8 |
import os
|
| 9 |
|
|
@@ -19,7 +18,7 @@ logging.basicConfig(
|
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
# ---------------------------------------------------------
|
| 22 |
-
# CORS
|
| 23 |
# ---------------------------------------------------------
|
| 24 |
app.add_middleware(
|
| 25 |
CORSMiddleware,
|
|
@@ -29,28 +28,26 @@ app.add_middleware(
|
|
| 29 |
)
|
| 30 |
|
| 31 |
# ---------------------------------------------------------
|
| 32 |
-
#
|
| 33 |
# ---------------------------------------------------------
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
global
|
| 39 |
-
if
|
| 40 |
-
logger.info("Loading dataset
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
logger.info(f"Dataset loaded with {len(df_cache)} rows.")
|
| 45 |
-
return df_cache
|
| 46 |
|
| 47 |
# ---------------------------------------------------------
|
| 48 |
# JSON-safe conversion
|
| 49 |
# ---------------------------------------------------------
|
| 50 |
def to_json_safe(obj):
|
| 51 |
-
if isinstance(obj, (np.integer,
|
| 52 |
return int(obj)
|
| 53 |
-
if isinstance(obj, (np.floating,
|
| 54 |
return float(obj)
|
| 55 |
if isinstance(obj, (np.ndarray, list)):
|
| 56 |
return [to_json_safe(x) for x in obj]
|
|
@@ -59,7 +56,7 @@ def to_json_safe(obj):
|
|
| 59 |
return obj
|
| 60 |
|
| 61 |
# ---------------------------------------------------------
|
| 62 |
-
# Serve index.html
|
| 63 |
# ---------------------------------------------------------
|
| 64 |
@app.get("/", response_class=HTMLResponse)
|
| 65 |
def serve_index():
|
|
@@ -73,8 +70,8 @@ def serve_index():
|
|
| 73 |
# ---------------------------------------------------------
|
| 74 |
@app.get("/tickers")
|
| 75 |
def get_tickers():
|
| 76 |
-
|
| 77 |
-
tickers = sorted(
|
| 78 |
return {"tickers": tickers}
|
| 79 |
|
| 80 |
# ---------------------------------------------------------
|
|
@@ -82,19 +79,17 @@ def get_tickers():
|
|
| 82 |
# ---------------------------------------------------------
|
| 83 |
@app.get("/transcript/{symbol}")
|
| 84 |
def get_transcript(symbol: str):
|
| 85 |
-
|
| 86 |
-
|
| 87 |
symbol = symbol.upper()
|
|
|
|
| 88 |
logger.info(f"Fetching transcript for: {symbol}")
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
-
if
|
| 93 |
raise HTTPException(status_code=404, detail=f"No transcript found for {symbol}")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
records = subset.to_dict(orient="records")
|
| 97 |
-
safe_records = [to_json_safe(r) for r in records]
|
| 98 |
|
| 99 |
-
return {"symbol": symbol, "records":
|
| 100 |
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
# ---------------------------------------------------------
|
| 21 |
+
# CORS
|
| 22 |
# ---------------------------------------------------------
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
# ---------------------------------------------------------
|
| 31 |
+
# Load Hugging Face dataset (cached)
|
| 32 |
# ---------------------------------------------------------
|
| 33 |
+
DATASET_NAME = "kurry/sp500_earnings_transcripts"
|
| 34 |
+
dataset_cache = None
|
| 35 |
|
| 36 |
+
def load_hf_dataset():
|
| 37 |
+
global dataset_cache
|
| 38 |
+
if dataset_cache is None:
|
| 39 |
+
logger.info(f"Loading HF dataset: {DATASET_NAME}")
|
| 40 |
+
dataset_cache = load_dataset(DATASET_NAME, split="train")
|
| 41 |
+
logger.info(f"Loaded {len(dataset_cache)} rows from HF dataset")
|
| 42 |
+
return dataset_cache
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# ---------------------------------------------------------
|
| 45 |
# JSON-safe conversion
|
| 46 |
# ---------------------------------------------------------
|
| 47 |
def to_json_safe(obj):
|
| 48 |
+
if isinstance(obj, (np.integer,)):
|
| 49 |
return int(obj)
|
| 50 |
+
if isinstance(obj, (np.floating,)):
|
| 51 |
return float(obj)
|
| 52 |
if isinstance(obj, (np.ndarray, list)):
|
| 53 |
return [to_json_safe(x) for x in obj]
|
|
|
|
| 56 |
return obj
|
| 57 |
|
| 58 |
# ---------------------------------------------------------
|
| 59 |
+
# Serve index.html
|
| 60 |
# ---------------------------------------------------------
|
| 61 |
@app.get("/", response_class=HTMLResponse)
|
| 62 |
def serve_index():
|
|
|
|
| 70 |
# ---------------------------------------------------------
|
| 71 |
@app.get("/tickers")
|
| 72 |
def get_tickers():
|
| 73 |
+
ds = load_hf_dataset()
|
| 74 |
+
tickers = sorted(set(ds["symbol"]))
|
| 75 |
return {"tickers": tickers}
|
| 76 |
|
| 77 |
# ---------------------------------------------------------
|
|
|
|
| 79 |
# ---------------------------------------------------------
|
| 80 |
@app.get("/transcript/{symbol}")
|
| 81 |
def get_transcript(symbol: str):
|
| 82 |
+
ds = load_hf_dataset()
|
|
|
|
| 83 |
symbol = symbol.upper()
|
| 84 |
+
|
| 85 |
logger.info(f"Fetching transcript for: {symbol}")
|
| 86 |
|
| 87 |
+
rows = [r for r in ds if r["symbol"].upper() == symbol]
|
| 88 |
|
| 89 |
+
if not rows:
|
| 90 |
raise HTTPException(status_code=404, detail=f"No transcript found for {symbol}")
|
| 91 |
|
| 92 |
+
safe_rows = [to_json_safe(r) for r in rows]
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
return {"symbol": symbol, "records": safe_rows}
|
| 95 |
|