File size: 1,932 Bytes
8a4966f
 
 
 
 
 
 
 
 
 
7ca91fb
 
 
 
5a8af87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4966f
d13438a
 
d0b522a
 
 
 
 
 
 
4efe46c
d0b522a
b0c7e9d
8a4966f
 
f31f0f6
d13438a
 
8a4966f
f39f1d3
8a4966f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()