Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image | |
import hopsworks | |
import threading | |
import time | |
# IDE Help | |
from hopsworks.core.dataset_api import DatasetApi | |
# Monitor | |
dir_wine_saves = "latest_wine" | |
file_dataframe_save = "df_recent.png" | |
# Images | |
hopsworks_images_location = f"Resources/images/{dir_wine_saves}" | |
hopsworks_images = { | |
"df_recent": | |
{ | |
"name": file_dataframe_save, | |
"local_path": "" | |
} | |
} | |
last_refresh_time = None | |
print("Logging in to Hopsworks...") | |
project = hopsworks.login() | |
print("Getting feature store...") | |
fs = project.get_feature_store() | |
print("Get database handler from Hopsworks...") | |
dataset_api: DatasetApi = project.get_dataset_api() | |
def download_and_refresh(): | |
global last_refresh_time, dataset_api | |
refreshed_all = False | |
error = [] | |
while not refreshed_all: | |
error_during_exec = False | |
for image in hopsworks_images: | |
# Only execute when no error has currently occurred (so we don't end up in an infinite loop) | |
if not error_during_exec and len(error) > 0 and error['name'] != hopsworks_images[image]['name']: | |
print(f"Skipping {hopsworks_images[image]['name']} to only retry failed images.") | |
continue | |
elif not error_during_exec: | |
# Pop current image | |
print(f"Retrying to get {image}...") | |
error = [cur_error for cur_error in error if cur_error['name'] != image['name']] | |
try: | |
print(f"Downloading {hopsworks_images[image]['name']} from Hopsworks...") | |
hopsworks_images[image]['local_path'] = dataset_api.download( | |
f"{hopsworks_images_location}/{hopsworks_images[image]['name']}", | |
overwrite=True) | |
print(f"Saved in: {hopsworks_images[image]['local_path']}") | |
last_refresh_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | |
print(f"Image refreshed at {last_refresh_time}") | |
except Exception as e: | |
print(f"An error occurred when trying to refresh {hopsworks_images[image]['name']}: {e}") | |
error_during_exec = True | |
# Make sure images are not added twice if re-added when retrying to download | |
error = [cur_error for cur_error in error if cur_error['name'] != hopsworks_images[image]['name']] | |
error.append(image) | |
if error_during_exec: | |
print(f"Will retry fetching failed image(s) in 1 minute...") | |
time.sleep(60) | |
else: | |
print(f"Done getting all images.") | |
refreshed_all = True | |
def download_and_refresh_scheduled(): | |
while True: | |
print(f"Refreshing in 12 hours...") | |
# Sleep for 12 hours (12 hours * 60 minutes * 60 seconds) | |
time.sleep(43200) | |
download_and_refresh() | |
def get_label_text(): | |
if last_refresh_time: | |
return f"Recent Prediction History (Last Refreshed: {last_refresh_time})" | |
else: | |
return "Recent Prediction History" | |
# Get all once | |
print("Getting all images...") | |
download_and_refresh() | |
thread = threading.Thread(target=download_and_refresh_scheduled) | |
thread.daemon = True | |
thread.start() | |
print("Configuring gradio...") | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
label1 = gr.Label(get_label_text()) | |
image1 = gr.Image(hopsworks_images['df_recent']['local_path'], | |
elem_id="recent-predictions", type="pil") | |
print("Launching gradio...") | |
demo.launch(debug=True) | |