air3 / app.py
howlbz's picture
Update app.py
5a8af87
raw
history blame
1.93 kB
import gradio as gr
import numpy as np
from PIL import Image
import requests
import hopsworks
import joblib
project = hopsworks.login(api_key_value="0rdWXlLgEd3mkGOg.iRZ7TtAkWGPlJHNQcAEph6Qbokoaq7QTBRI9ckwWUki8tIYGyBvrKhJvtLoUOGQ4")
fs = project.get_feature_store()
# mr = project.get_model_registry()
# model = mr.get_model("xgboost_model", version=1)
# model_dir = model.download()
# model = joblib.load("/model.pkl")
def get_model(project, model_name):
"""Retrieve desired model or download it from the Hopsworks Model Registry.
In second case, it will be physically downloaded to this directory"""
TARGET_FILE = "model.pkl"
list_of_files = [os.path.join(dirpath,filename) for dirpath, _, filenames \
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
if list_of_files:
model_path = list_of_files[0]
model = joblib.load(model_path)
else:
if not os.path.exists(TARGET_FILE):
mr = project.get_model_registry()
model = mr.get_model("xgboost_model", version=1)
model_dir = model.download()
model = joblib.load("/model.pkl")
return model
model = get_model(project,"xgboost_model")
def forecast():
x = [ 0. , 24 , -0.68645433, -0.06804887, -0.31264014,
-0.13749569, -0.32063957, -0.2942814 , -0.18460245, -0.41253886,
0.06395449, 0.71276574, -0.36466156, -1.03879548, -0.65985627,
0 , 0 , 0.12254366, 0.39172671, 0.34205118,
0.21383452, -1.0216134 , 0.40277851, -0.34577169, -0.36832646,
-0.7210296 , 0 ]
res = model.predict(np.asarray(x).reshape(-1, 1))
return model_dir
demo = gr.Interface(
fn=forecast,
title="Air Quality Prediction",
description="Get aqi value",
allow_flagging="never",
inputs=[],
outputs=gr.Textbox(label="Result: "))
demo.launch()