Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +23 -12
- app.py +80 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,12 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Predict Book Genre from Physical Features — AutoGluon + Gradio
|
| 2 |
+
|
| 3 |
+
This Space wraps a class-trained AutoGluon `TabularPredictor` with a Gradio UI.
|
| 4 |
+
|
| 5 |
+
## Dataset
|
| 6 |
+
- Zion's Book tabular dataset (Hugging Face: its-zion-18/Books-tabular-dataset)
|
| 7 |
+
- Features: Height, Width, Depth, Page Count
|
| 8 |
+
- Target: Genre (5 classes)
|
| 9 |
+
|
| 10 |
+
## Training
|
| 11 |
+
- Framework: AutoGluon Tabular
|
| 12 |
+
- Preset: best_quality
|
| 13 |
+
- Time Limit: 300 seconds
|
| 14 |
+
- Best model: WeightedEnsemble_L2
|
| 15 |
+
- Evaluation: Accuracy 1.0, Weighted F1 1.0 (on original split)
|
| 16 |
+
|
| 17 |
+
## Limitations
|
| 18 |
+
- Very small dataset (30 eval samples)
|
| 19 |
+
- Augmented synthetic data may not generalize well
|
| 20 |
+
|
| 21 |
+
## Usage
|
| 22 |
+
Adjust the sliders and number field to input book dimensions and page count.
|
| 23 |
+
The app will output predicted genre probabilities.
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
+
import pandas
|
| 3 |
+
import gradio
|
| 4 |
+
import huggingface_hub
|
| 5 |
+
import autogluon.tabular
|
| 6 |
+
|
| 7 |
+
MODEL_REPO_ID = "FaiyazAzam/24679-tabular-autolguon-predictor"
|
| 8 |
+
ZIP_FILENAME = "autogluon_predictor_dir.zip"
|
| 9 |
+
CACHE_DIR = pathlib.Path("hf_assets")
|
| 10 |
+
EXTRACT_DIR = CACHE_DIR / "predictor_native"
|
| 11 |
+
|
| 12 |
+
FEATURE_COLS = ['Height', 'Width', 'Depth', 'Page Count']
|
| 13 |
+
TARGET_COL = 'Genre'
|
| 14 |
+
|
| 15 |
+
def _prepare_predictor_dir() -> str:
|
| 16 |
+
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
| 17 |
+
local_zip = huggingface_hub.hf_hub_download(
|
| 18 |
+
repo_id=MODEL_REPO_ID,
|
| 19 |
+
filename=ZIP_FILENAME,
|
| 20 |
+
repo_type="model",
|
| 21 |
+
local_dir=str(CACHE_DIR),
|
| 22 |
+
local_dir_use_symlinks=False,
|
| 23 |
+
)
|
| 24 |
+
if EXTRACT_DIR.exists():
|
| 25 |
+
import shutil, zipfile
|
| 26 |
+
shutil.rmtree(EXTRACT_DIR)
|
| 27 |
+
EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
with zipfile.ZipFile(local_zip, "r") as zf:
|
| 29 |
+
zf.extractall(str(EXTRACT_DIR))
|
| 30 |
+
contents = list(EXTRACT_DIR.iterdir())
|
| 31 |
+
predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
|
| 32 |
+
return str(predictor_root)
|
| 33 |
+
|
| 34 |
+
PREDICTOR_DIR = _prepare_predictor_dir()
|
| 35 |
+
PREDICTOR = autogluon.tabular.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False)
|
| 36 |
+
|
| 37 |
+
def do_predict(height, width, depth, page_count):
|
| 38 |
+
row = {
|
| 39 |
+
"Height": float(height),
|
| 40 |
+
"Width": float(width),
|
| 41 |
+
"Depth": float(depth),
|
| 42 |
+
"Page Count": int(page_count),
|
| 43 |
+
}
|
| 44 |
+
X = pandas.DataFrame([row], columns=FEATURE_COLS)
|
| 45 |
+
pred_series = PREDICTOR.predict(X)
|
| 46 |
+
raw_pred = pred_series.iloc[0]
|
| 47 |
+
try:
|
| 48 |
+
proba = PREDICTOR.predict_proba(X)
|
| 49 |
+
if isinstance(proba, pandas.Series):
|
| 50 |
+
proba = proba.to_frame().T
|
| 51 |
+
except Exception:
|
| 52 |
+
proba = None
|
| 53 |
+
proba_dict = None
|
| 54 |
+
if proba is not None:
|
| 55 |
+
row0 = proba.iloc[0]
|
| 56 |
+
tmp = {str(cls): float(val) for cls, val in row0.items()}
|
| 57 |
+
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 58 |
+
return proba_dict
|
| 59 |
+
|
| 60 |
+
EXAMPLES = [
|
| 61 |
+
[20.0, 13.0, 3.0, 350],
|
| 62 |
+
[23.0, 15.0, 5.0, 600],
|
| 63 |
+
[18.0, 11.0, 2.0, 200],
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
with gradio.Blocks() as demo:
|
| 67 |
+
gradio.Markdown("# Predict Book Genre from Physical Features")
|
| 68 |
+
with gradio.Row():
|
| 69 |
+
height = gradio.Slider(10, 30, step=0.5, value=20.0, label="Height (cm)")
|
| 70 |
+
width = gradio.Slider(8, 25, step=0.5, value=13.0, label="Width (cm)")
|
| 71 |
+
with gradio.Row():
|
| 72 |
+
depth = gradio.Slider(1, 10, step=0.1, value=3.0, label="Depth (cm)")
|
| 73 |
+
page_count = gradio.Number(value=350, precision=0, label="Page Count")
|
| 74 |
+
proba_pretty = gradio.Label(num_top_classes=5, label="Predicted Genre Probabilities")
|
| 75 |
+
inputs = [height, width, depth, page_count]
|
| 76 |
+
for comp in inputs:
|
| 77 |
+
comp.change(fn=do_predict, inputs=inputs, outputs=[proba_pretty])
|
| 78 |
+
gradio.Examples(examples=EXAMPLES, inputs=inputs, label="Representative examples", cache_examples=False)
|
| 79 |
+
|
| 80 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
huggingface_hub
|
| 3 |
+
pandas
|
| 4 |
+
autogluon.tabular
|