Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from autogluon.tabular import TabularPredictor
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import os, zipfile
|
| 6 |
+
|
| 7 |
+
REPO_ID = "FaiyazAzam/24679-tabular-autolguon-predictor"
|
| 8 |
+
repo_dir = snapshot_download(repo_id=REPO_ID)
|
| 9 |
+
|
| 10 |
+
# unzip the dir that holds the predictor
|
| 11 |
+
zip_path = os.path.join(repo_dir, "autogluon_predictor_dir.zip")
|
| 12 |
+
extract_to = "/content/predictor_dir"
|
| 13 |
+
if not os.path.exists(extract_to):
|
| 14 |
+
with zipfile.ZipFile(zip_path, "r") as zf:
|
| 15 |
+
zf.extractall(extract_to)
|
| 16 |
+
|
| 17 |
+
predictor = TabularPredictor.load(extract_to)
|
| 18 |
+
|
| 19 |
+
def predict_tabular(height, width, depth, page_count):
|
| 20 |
+
# Validation
|
| 21 |
+
if height <= 0 or width <= 0 or depth <= 0:
|
| 22 |
+
raise ValueError("All dimensions must be positive numbers.")
|
| 23 |
+
if page_count <= 0:
|
| 24 |
+
raise ValueError("Page count must be a positive integer.")
|
| 25 |
+
|
| 26 |
+
row = {
|
| 27 |
+
"Height": height,
|
| 28 |
+
"Width": width,
|
| 29 |
+
"Depth": depth,
|
| 30 |
+
"Page Count": page_count,
|
| 31 |
+
}
|
| 32 |
+
df = pd.DataFrame([row])
|
| 33 |
+
pred = int(predictor.predict(df)[0])
|
| 34 |
+
|
| 35 |
+
return {f"Predicted Genre = {pred}"}
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(title="Book Genre Predictor") as demo:
|
| 38 |
+
gr.Markdown("## Predict the Genre of a Book (Numeric Labels)")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
with gr.Column():
|
| 42 |
+
height = gr.Number(label="Height (cm)", info="Book height in cm")
|
| 43 |
+
width = gr.Number(label="Width (cm)", info="Book width in cm")
|
| 44 |
+
depth = gr.Number(label="Depth (cm)", info="Book spine thickness in cm")
|
| 45 |
+
page_count = gr.Number(label="Page Count", info="Number of pages (positive integer)")
|
| 46 |
+
|
| 47 |
+
out = gr.JSON(label="Result")
|
| 48 |
+
run_btn = gr.Button("Predict")
|
| 49 |
+
|
| 50 |
+
run_btn.click(
|
| 51 |
+
predict_tabular,
|
| 52 |
+
inputs=[height, width, depth, page_count, show_proba],
|
| 53 |
+
outputs=out
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
gr.Examples(
|
| 57 |
+
examples=[
|
| 58 |
+
[20.1, 13.5, 1.8, 250],
|
| 59 |
+
[24.0, 15.0, 2.2, 320],
|
| 60 |
+
[18.5, 12.0, 1.5, 180],
|
| 61 |
+
],
|
| 62 |
+
inputs=[height, width, depth, page_count, show_proba],
|
| 63 |
+
outputs=out
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
demo.launch(share=True)
|
| 68 |
+
|