huohuobeixiaosile commited on
Commit
fe1329b
·
verified ·
1 Parent(s): ff2fc7f

Update W3-assignment-streamlit.py

Browse files
Files changed (1) hide show
  1. W3-assignment-streamlit.py +21 -13
W3-assignment-streamlit.py CHANGED
@@ -6,6 +6,7 @@ import pandas as pd
6
  import numpy as np
7
  import matplotlib.pyplot as plt
8
 
 
9
  from sklearn.impute import SimpleImputer
10
  from sklearn.preprocessing import StandardScaler
11
  from sklearn.decomposition import PCA
@@ -21,21 +22,28 @@ st.sidebar.title("Controls")
21
  import os
22
  from pathlib import Path
23
 
24
- candidates = [
25
- "youtube_shorts_tiktok_trends_2025.csv",
26
- "data/youtube_shorts_tiktok_trends_2025.csv"
27
- ]
28
-
29
- CSV_PATH = None
30
- for p in candidates:
31
- if Path(p).exists():
32
- CSV_PATH = p
33
- break
 
 
 
 
34
 
35
- assert CSV_PATH is not None, f"CSV not found in: {candidates}"
 
 
 
36
 
37
- # Load dataset
38
- df_raw = pd.read_csv(CSV_PATH, low_memory=False)
39
 
40
  # Choose number of clusters (K)
41
  k = st.sidebar.slider("Number of clusters (K)", min_value=2, max_value=8, value=4, step=1)
 
6
  import numpy as np
7
  import matplotlib.pyplot as plt
8
 
9
+
10
  from sklearn.impute import SimpleImputer
11
  from sklearn.preprocessing import StandardScaler
12
  from sklearn.decomposition import PCA
 
22
  import os
23
  from pathlib import Path
24
 
25
+ @st.cache_data(show_spinner=False)
26
+ def load_data() -> pd.DataFrame:
27
+ """
28
+ Try both locations:
29
+ 1) data/youtube_shorts_tiktok_trends_2025.csv
30
+ 2) ./youtube_shorts_tiktok_trends_2025.csv
31
+ """
32
+ candidates = [
33
+ Path("data/youtube_shorts_tiktok_trends_2025.csv"),
34
+ Path("youtube_shorts_tiktok_trends_2025.csv"),
35
+ ]
36
+ for p in candidates:
37
+ if p.exists():
38
+ return pd.read_csv(p, low_memory=False)
39
 
40
+ raise FileNotFoundError(
41
+ f"CSV not found in any of: {[str(p) for p in candidates]}. "
42
+ "Make sure the file is committed to your Space."
43
+ )
44
 
45
+ # load data
46
+ df_raw = load_data()
47
 
48
  # Choose number of clusters (K)
49
  k = st.sidebar.slider("Number of clusters (K)", min_value=2, max_value=8, value=4, step=1)