Spaces:
Build error
Build error
Push.
Browse files
app.py
CHANGED
|
@@ -1,4 +1,119 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import hopsworks
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import seaborn as sns
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
|
| 10 |
+
# Constants
|
| 11 |
+
DATA_DIR = "data"
|
| 12 |
+
TIMESTAMP_FILE = "last_download_time.txt"
|
| 13 |
+
|
| 14 |
+
# Initialize Hopsworks connection
|
| 15 |
+
def connect_to_hopsworks():
|
| 16 |
+
st.write("Connecting to Hopsworks...")
|
| 17 |
+
project_name = "id2223AirQuality"
|
| 18 |
+
api_key = os.getenv("HOPSWORKS_API_KEY")
|
| 19 |
+
conn = hopsworks.connection(api_key_value=api_key)
|
| 20 |
+
project = conn.get_project(project_name)
|
| 21 |
+
return project
|
| 22 |
+
|
| 23 |
+
# Fetch data from Hopsworks feature group
|
| 24 |
+
def fetch_data_from_feature_group(project, feature_group_name, version):
|
| 25 |
+
feature_store = project.get_feature_store()
|
| 26 |
+
feature_group = feature_store.get_feature_group(name=feature_group_name, version=version)
|
| 27 |
+
data = feature_group.read()
|
| 28 |
+
return data
|
| 29 |
+
|
| 30 |
+
# Save data locally
|
| 31 |
+
def save_data_locally(data, filename):
|
| 32 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 33 |
+
filepath = os.path.join(DATA_DIR, filename)
|
| 34 |
+
data.to_csv(filepath, index=False)
|
| 35 |
+
|
| 36 |
+
# Save timestamp
|
| 37 |
+
timestamp_path = os.path.join(DATA_DIR, TIMESTAMP_FILE)
|
| 38 |
+
with open(timestamp_path, "w") as f:
|
| 39 |
+
f.write(str(datetime.now()))
|
| 40 |
+
return filepath
|
| 41 |
+
|
| 42 |
+
# Load local data
|
| 43 |
+
def load_local_data(filename):
|
| 44 |
+
filepath = os.path.join(DATA_DIR, filename)
|
| 45 |
+
if os.path.exists(filepath):
|
| 46 |
+
return pd.read_csv(filepath)
|
| 47 |
+
else:
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
# Check if local data is valid
|
| 51 |
+
def is_local_data_valid():
|
| 52 |
+
timestamp_path = os.path.join(DATA_DIR, TIMESTAMP_FILE)
|
| 53 |
+
if not os.path.exists(timestamp_path):
|
| 54 |
+
return False
|
| 55 |
+
try:
|
| 56 |
+
with open(timestamp_path, "r") as f:
|
| 57 |
+
last_download_time = datetime.fromisoformat(f.read().strip())
|
| 58 |
+
# Check if the data is more than a day old
|
| 59 |
+
if datetime.now() - last_download_time > timedelta(days=1):
|
| 60 |
+
return False
|
| 61 |
+
return True
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.warning(f"Error reading timestamp: {e}")
|
| 64 |
+
return False
|
| 65 |
+
|
| 66 |
+
# Plot graphs
|
| 67 |
+
def plot_graphs(data):
|
| 68 |
+
st.write("### Data Preview")
|
| 69 |
+
st.dataframe(data.head())
|
| 70 |
+
|
| 71 |
+
#st.write("### Histogram")
|
| 72 |
+
#column = st.selectbox("Select column for histogram", data.columns)
|
| 73 |
+
#fig, ax = plt.subplots()
|
| 74 |
+
#sns.histplot(data[column], kde=True, ax=ax)
|
| 75 |
+
#st.pyplot(fig)
|
| 76 |
+
|
| 77 |
+
#st.write("### Correlation Matrix")
|
| 78 |
+
#fig, ax = plt.subplots()
|
| 79 |
+
#sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)
|
| 80 |
+
#st.pyplot(fig)
|
| 81 |
+
|
| 82 |
+
# Streamlit UI
|
| 83 |
+
def main():
|
| 84 |
+
st.title("Hopsworks Feature Group Explorer")
|
| 85 |
+
|
| 86 |
+
# Initialize session state
|
| 87 |
+
if "hopsworks_project" not in st.session_state:
|
| 88 |
+
st.session_state.hopsworks_project = None
|
| 89 |
+
if "data" not in st.session_state:
|
| 90 |
+
st.session_state.data = None
|
| 91 |
+
|
| 92 |
+
# User inputs for feature group and version
|
| 93 |
+
st.sidebar.title("Data Settings")
|
| 94 |
+
feature_group_name = st.sidebar.text_input("Feature Group Name", value="predictions")
|
| 95 |
+
version = st.sidebar.number_input("Feature Group Version", value=1, min_value=1)
|
| 96 |
+
filename = st.sidebar.text_input("Local Filename", value="data.csv")
|
| 97 |
+
|
| 98 |
+
# Check for valid local data
|
| 99 |
+
if is_local_data_valid():
|
| 100 |
+
st.write("Using cached local data.")
|
| 101 |
+
st.session_state.data = load_local_data(filename)
|
| 102 |
+
else:
|
| 103 |
+
# Fetch data if local data is invalid
|
| 104 |
+
if st.session_state.hopsworks_project is None:
|
| 105 |
+
st.write("Initializing Hopsworks connection...")
|
| 106 |
+
st.session_state.hopsworks_project = connect_to_hopsworks()
|
| 107 |
+
st.success("Connected to Hopsworks!")
|
| 108 |
+
|
| 109 |
+
project = st.session_state.hopsworks_project
|
| 110 |
+
data = fetch_data_from_feature_group(project, feature_group_name, version)
|
| 111 |
+
filepath = save_data_locally(data, filename)
|
| 112 |
+
st.session_state.data = data
|
| 113 |
+
st.success(f"Data fetched and saved locally at {filepath}")
|
| 114 |
+
|
| 115 |
+
# Display data and graphs
|
| 116 |
+
if st.session_state.data is not None:
|
| 117 |
+
plot_graphs(st.session_state.data)
|
| 118 |
+
|
| 119 |
+
main()
|