Spaces:
Build error
Build error
Push.
Browse files1234import streamlit as stx = st.slider('Select a value')st.write(x, 'squared is', x * x)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import streamlit as stimport hopsworksimport pandas as pdimport osimport timeimport matplotlib.pyplot as pltimport seaborn as snsfrom datetime import datetime, timedelta# ConstantsDATA_DIR = "data"TIMESTAMP_FILE = "last_download_time.txt"# Initialize Hopsworks connectiondef connect_to_hopsworks():st.write("Connecting to Hopsworks...")project_name = "id2223AirQuality"api_key = os.getenv("HOPSWORKS_API_KEY")conn = hopsworks.connection(api_key_value=api_key)project = conn.get_project(project_name)return project# Fetch data from Hopsworks feature groupdef fetch_data_from_feature_group(project, feature_group_name, version):feature_store = project.get_feature_store()feature_group = feature_store.get_feature_group(name=feature_group_name, version=version)data = feature_group.read()return data# Save data locallydef save_data_locally(data, filename):os.makedirs(DATA_DIR, exist_ok=True)filepath = os.path.join(DATA_DIR, filename)data.to_csv(filepath, index=False)# Save timestamptimestamp_path = os.path.join(DATA_DIR, TIMESTAMP_FILE)with open(timestamp_path, "w") as f:f.write(str(datetime.now()))return filepath# Load local datadef load_local_data(filename):filepath = os.path.join(DATA_DIR, filename)if os.path.exists(filepath):return pd.read_csv(filepath)else:return None# Check if local data is validdef is_local_data_valid():timestamp_path = os.path.join(DATA_DIR, TIMESTAMP_FILE)if not os.path.exists(timestamp_path):return Falsetry:with open(timestamp_path, "r") as f:last_download_time = datetime.fromisoformat(f.read().strip())# Check if the data is more than a day oldif datetime.now() - last_download_time > timedelta(days=1):return Falsereturn Trueexcept Exception as e:st.warning(f"Error reading timestamp: {e}")return False# Plot graphsdef plot_graphs(data):st.write("### Data Preview")st.dataframe(data.head())#st.write("### Histogram")#column = st.selectbox("Select column for histogram", data.columns)#fig, ax = plt.subplots()#sns.histplot(data[column], kde=True, ax=ax)#st.pyplot(fig)#st.write("### Correlation Matrix")#fig, ax = plt.subplots()#sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)#st.pyplot(fig)# Streamlit UIdef main():st.title("Hopsworks Feature Group Explorer")# Initialize session stateif "hopsworks_project" not in st.session_state:st.session_state.hopsworks_project = Noneif "data" not in st.session_state:st.session_state.data = None# User inputs for feature group and versionst.sidebar.title("Data Settings")feature_group_name = st.sidebar.text_input("Feature Group Name", value="predictions")version = st.sidebar.number_input("Feature Group Version", value=1, min_value=1)filename = st.sidebar.text_input("Local Filename", value="data.csv")# Check for valid local dataif is_local_data_valid():st.write("Using cached local data.")st.session_state.data = load_local_data(filename)else:# Fetch data if local data is invalidif st.session_state.hopsworks_project is None:st.write("Initializing Hopsworks connection...")st.session_state.hopsworks_project = connect_to_hopsworks()st.success("Connected to Hopsworks!")project = st.session_state.hopsworks_projectdata = fetch_data_from_feature_group(project, feature_group_name, version)filepath = save_data_locally(data, filename)st.session_state.data = datast.success(f"Data fetched and saved locally at {filepath}")# Display data and graphsif st.session_state.data is not None:plot_graphs(st.session_state.data)main()
app.py+117-2