File size: 3,956 Bytes
594eb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7727aa
594eb60
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import uuid
import joblib
import json
import gradio as gr
import pandas as pd

from huggingface_hub import CommitScheduler
from pathlib import Path

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from warnings import filterwarnings
filterwarnings('ignore')


log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
log_folder = log_file.parent

scheduler = CommitScheduler(
    repo_id="machine-failure-logs",
    repo_type="dataset",
    folder_path=log_folder,
    path_in_repo="data",
    every=2
)

health_status_predictor = joblib.load('model.joblib')

Latitude = gr.Number(label='Latitude')
Longitude = gr.Number(label='Longitude')
DBH = gr.Number(label='DBH')
Tree_Height = gr.Number(label='Tree_Height')
Crown_Width_North_South = gr.Number(label='Crown_Width_North_South')
Crown_Width_East_West = gr.Number(label='Crown_Width_East_West')
Slope = gr.Number(label='Slope')
Elevation = gr.Number(label='Elevation')
Temperature = gr.Number(label='Temperature')
Humidity = gr.Number(label='Humidity')
Soil_TN = gr.Number(label='Soil_TN')
Soil_TP = gr.Number(label='Soil_TP')
Soil_AP = gr.Number(label='Soil_AP')
Soil_AN = gr.Number(label='Soil_AN')
Menhinick_Index = gr.Number(label='Menhinick_Index')
Gleason_Index = gr.Number(label='Gleason_Index')
Fire_Risk_Index = gr.Number(label='Fire_Risk_Index')

model_output = gr.Label(label="Health Status")

def predict_health_status(Latitude, Longitude, DBH, Tree_Height, Crown_Width_North_South, Crown_Width_East_West, Slope, Elevation, Temperature, Humidity, Soil_TN, Soil_TP, Soil_AP, Soil_AN, Menhinick_Index, Gleason_Index, Fire_Risk_Index):
    sample = {
        'Latitude': Latitude,
        'Longitude': Longitude,
        'DBH': DBH,
        'Tree_Height': Tree_Height,
        'Crown_Width_North_South': Crown_Width_North_South,
        'Crown_Width_East_West': Crown_Width_East_West,
        'Slope': Slope,
        'Elevation': Elevation,
        'Temperature': Temperature,
        'Humidity': Humidity,
        'Soil_TN': Soil_TN,
        'Soil_TP': Soil_TP,
        'Soil_AP': Soil_AP,
        'Soil_AN': Soil_AN,
        'Menhinick_Index': Menhinick_Index,
        'Gleason_Index': Gleason_Index.
        'Fire_Risk_Index': Fire_Risk_Index
        
    }
    data_point = pd.DataFrame([sample])
    prediction = health_status_predictor.predict(data_point).tolist()

    with scheduler.lock:
        with log_file.open("a") as f:
            f.write(json.dumps(
                {
                    'Latitude': Latitude,
                    'Longitude': Longitude,
                    'DBH': DBH,
                    'Tree_Height': Tree_Height,
                    'Crown_Width_North_South': Crown_Width_North_South,
                    'Crown_Width_East_West': Crown_Width_East_West,
                    'Slope': Slope,
                    'Elevation': Elevation,
                    'Temperature': Temperature,
                    'Humidity': Humidity,
                    'Soil_TN': Soil_TN,
                    'Soil_TP': Soil_TP,
                    'Soil_AP': Soil_AP,
                    'Soil_AN': Soil_AN,
                    'Menhinick_Index': Menhinick_Index,
                    'Gleason_Index': Gleason_Index,
                    'Fire_Risk_Index': Fire_Risk_Index,
                    'prediction': prediction[0]
                }
            ))
            f.write("\n")
            
    return prediction[0]

demo = gr.Interface(
    fn=predict_health_status,
    inputs=[Latitude, Longitude, DBH, Tree_Height, Crown_Width_North_South, Crown_Width_East_West, Slope, Elevation, Temperature, Humidity, Soil_TN, Soil_TP, Soil_AP, Soil_AN, Menhinick_Index, Gleason_Index, Fire_Risk_Index],
    outputs=model_output,
    title="Health Status Predictor",
    description="This API allows you to predict the health status of a Tree",
    allow_flagging="auto",
    concurrency_limit=8
)


demo.queue()
demo.launch(share=False)