File size: 1,827 Bytes
b6868b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5ff2eb
 
b6868b1
 
 
1c7b675
 
 
b6868b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from autogluon.tabular import TabularPredictor
from huggingface_hub import hf_hub_download
import os, zipfile

REPO_ID = "Iris314/classical-automl-model"
ZIP_FILE = "lego_predictor_dir.zip"

local_zip = hf_hub_download(repo_id=REPO_ID, filename=ZIP_FILE)

extract_dir = "lego_predictor_dir"
os.makedirs(extract_dir, exist_ok=True)

with zipfile.ZipFile(local_zip, 'r') as zip_ref:
    zip_ref.extractall(extract_dir)

predictor = TabularPredictor.load(extract_dir, require_py_version_match=False)


def predict_brick(length, height, width, studs):
    record = pd.DataFrame([{
        "Max Length (cm)": length,
        "Max Height (cm)": height,
        "Width (cm)": width,
        "Studs": studs
    }])
    pred = predictor.predict(record)[0]
    proba = predictor.predict_proba(record).iloc[0].to_dict()
    return f"Prediction: {pred}", proba

with gr.Blocks(title="LEGO Brick Classifier") as demo:
    gr.Markdown("## LEGO Brick Classification\nPredict Standard / Flat / Sloped")
    with gr.Row():
        with gr.Column():
            length = gr.Slider(1, 10, step=0.5, value=4, label="Length")
            height = gr.Slider(0.5, 5, step=0.1, value=1.2, label="Height")
            width = gr.Slider(1, 10, step=0.5, value=2, label="Width")
            studs = gr.Slider(0, 12, step=1, value=4, label="Studs")
            btn = gr.Button("Predict")
        with gr.Column():
            out_label = gr.Textbox(label="Prediction")
            out_probs = gr.Label(label="Class Probabilities")

    btn.click(predict_brick, [length, height, width, studs], [out_label, out_probs])

    gr.Examples(
        examples=[
            [4, 1.2, 2, 4],
            [6, 0.5, 2, 6],
            [3, 2.0, 2, 2]
        ],
        inputs=[length, height, width, studs]
    )

demo.launch()