Spaces:
Sleeping

shyameati commited on
Commit
9ccf981
·
1 Parent(s): 9da13e6

Use HF dataset + fix ticker column + serve index.html

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -2,7 +2,7 @@ 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
 
@@ -28,18 +28,43 @@ app.add_middleware(
28
  )
29
 
30
  # ---------------------------------------------------------
31
- # Load Hugging Face dataset (lazy load)
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
@@ -55,6 +80,7 @@ def to_json_safe(obj):
55
  return {k: to_json_safe(v) for k, v in obj.items()}
56
  return obj
57
 
 
58
  # ---------------------------------------------------------
59
  # Serve index.html
60
  # ---------------------------------------------------------
@@ -65,6 +91,7 @@ def serve_index():
65
  with open("index.html", "r") as f:
66
  return f.read()
67
 
 
68
  # ---------------------------------------------------------
69
  # List all tickers
70
  # ---------------------------------------------------------
@@ -74,6 +101,7 @@ def get_tickers():
74
  tickers = sorted(set([t.upper() for t in ds["ticker"]]))
75
  return {"tickers": tickers}
76
 
 
77
  # ---------------------------------------------------------
78
  # Get transcript for a symbol
79
  # ---------------------------------------------------------
 
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, load_from_disk
6
  import numpy as np
7
  import os
8
 
 
28
  )
29
 
30
  # ---------------------------------------------------------
31
+ # Dataset caching configuration
32
  # ---------------------------------------------------------
33
  DATASET_NAME = "kurry/sp500_earnings_transcripts"
34
+ CACHE_PATH = "/data/hf_dataset" # persistent bucket mount
35
  dataset_cache = None
36
 
37
+
38
  def load_hf_dataset():
39
+ """
40
+ Loads the HF dataset with persistent caching.
41
+ - If /data/hf_dataset exists → load from disk (fast, offline)
42
+ - Else → download once, save to disk, then load
43
+ """
44
  global dataset_cache
45
+
46
+ # Already loaded in memory
47
+ if dataset_cache is not None:
48
+ return dataset_cache
49
+
50
+ # Load from persistent cache
51
+ if os.path.exists(CACHE_PATH):
52
+ logger.info(f"Loading dataset from cache at {CACHE_PATH}")
53
+ dataset_cache = load_from_disk(CACHE_PATH)
54
+ logger.info(f"Loaded {len(dataset_cache)} rows from cached dataset")
55
+ return dataset_cache
56
+
57
+ # Download once, then save
58
+ logger.info(f"Downloading HF dataset: {DATASET_NAME}")
59
+ ds = load_dataset(DATASET_NAME, split="train")
60
+
61
+ logger.info(f"Saving dataset to cache at {CACHE_PATH}")
62
+ ds.save_to_disk(CACHE_PATH)
63
+
64
+ dataset_cache = ds
65
+ logger.info(f"Dataset cached and loaded ({len(ds)} rows)")
66
+ return ds
67
+
68
 
69
  # ---------------------------------------------------------
70
  # JSON-safe conversion
 
80
  return {k: to_json_safe(v) for k, v in obj.items()}
81
  return obj
82
 
83
+
84
  # ---------------------------------------------------------
85
  # Serve index.html
86
  # ---------------------------------------------------------
 
91
  with open("index.html", "r") as f:
92
  return f.read()
93
 
94
+
95
  # ---------------------------------------------------------
96
  # List all tickers
97
  # ---------------------------------------------------------
 
101
  tickers = sorted(set([t.upper() for t in ds["ticker"]]))
102
  return {"tickers": tickers}
103
 
104
+
105
  # ---------------------------------------------------------
106
  # Get transcript for a symbol
107
  # ---------------------------------------------------------