bareethul commited on
Commit
1189722
·
verified ·
1 Parent(s): c699ba7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -1,31 +1,38 @@
1
- import gradio as gr
2
- import pandas as pd
3
- from autogluon.tabular import TabularPredictor
4
- from huggingface_hub import snapshot_download
5
- import os, zipfile
6
- import torch
 
7
 
 
8
  REPO_ID = "FaiyazAzam/24679-tabular-autolguon-predictor"
 
 
9
  repo_dir = snapshot_download(repo_id=REPO_ID)
10
 
11
- # unzip the dir that holds the predictor
12
  zip_path = os.path.join(repo_dir, "autogluon_predictor_dir.zip")
13
  extract_to = "/tmp/predictor_dir"
14
  if not os.path.exists(extract_to):
15
  with zipfile.ZipFile(zip_path, "r") as zf:
16
  zf.extractall(extract_to)
17
 
 
18
  predictor = TabularPredictor.load(
19
  extract_to,
20
  require_py_version_match=False)
21
 
 
22
  def predict_tabular(height, width, depth, page_count):
23
- # Validation
24
  if height <= 0 or width <= 0 or depth <= 0:
25
  raise ValueError("All dimensions must be positive numbers.")
26
  if page_count <= 0:
27
  raise ValueError("Page count must be a positive integer.")
28
 
 
29
  row = {
30
  "Height": height,
31
  "Width": width,
@@ -33,13 +40,18 @@ def predict_tabular(height, width, depth, page_count):
33
  "Page Count": page_count,
34
  }
35
  df = pd.DataFrame([row])
 
 
36
  pred = int(predictor.predict(df)[0])
37
 
 
38
  return f"Predicted Genre Code: {pred}"
39
 
 
40
  with gr.Blocks(title="Book Genre Predictor") as demo:
41
  gr.Markdown("## Predict the Genre of a Book (Numeric Labels)")
42
 
 
43
  with gr.Row():
44
  with gr.Column():
45
  height = gr.Number(label="Height (cm)", info="Book height in cm")
@@ -47,15 +59,20 @@ with gr.Blocks(title="Book Genre Predictor") as demo:
47
  depth = gr.Number(label="Depth (cm)", info="Book spine thickness in cm")
48
  page_count = gr.Number(label="Page Count", info="Number of pages (positive integer)")
49
 
 
50
  out = gr.Textbox(label="Result")
 
 
51
  run_btn = gr.Button("Predict")
52
 
 
53
  run_btn.click(
54
  predict_tabular,
55
  inputs=[height, width, depth, page_count],
56
  outputs=out
57
  )
58
 
 
59
  gr.Examples(
60
  examples=[
61
  [20.1, 13.5, 1.8, 250],
@@ -66,4 +83,5 @@ with gr.Blocks(title="Book Genre Predictor") as demo:
66
  outputs=out
67
  )
68
 
 
69
  demo.launch(share=True)
 
1
+ # Import required libraries
2
+ import gradio as gr # for building the user interface
3
+ import pandas as pd # for handling tabular data
4
+ from autogluon.tabular import TabularPredictor # AutoGluon class to load tabular models
5
+ from huggingface_hub import snapshot_download # to download model files from Hugging Face Hub
6
+ import os, zipfile # for file path operations and unzipping
7
+ import torch # model/device handling
8
 
9
+ # Define the Hugging Face repo ID where the trained predictor is stored
10
  REPO_ID = "FaiyazAzam/24679-tabular-autolguon-predictor"
11
+
12
+ # Download the repo snapshot locally
13
  repo_dir = snapshot_download(repo_id=REPO_ID)
14
 
15
+ # Unzip the model directory into /tmp (safe location on Spaces)
16
  zip_path = os.path.join(repo_dir, "autogluon_predictor_dir.zip")
17
  extract_to = "/tmp/predictor_dir"
18
  if not os.path.exists(extract_to):
19
  with zipfile.ZipFile(zip_path, "r") as zf:
20
  zf.extractall(extract_to)
21
 
22
+ # Load the AutoGluon Tabular predictor
23
  predictor = TabularPredictor.load(
24
  extract_to,
25
  require_py_version_match=False)
26
 
27
+ # Function that takes book dimensions + page count and returns genre prediction
28
  def predict_tabular(height, width, depth, page_count):
29
+ # Validate inputs (must be positive)
30
  if height <= 0 or width <= 0 or depth <= 0:
31
  raise ValueError("All dimensions must be positive numbers.")
32
  if page_count <= 0:
33
  raise ValueError("Page count must be a positive integer.")
34
 
35
+ # Build a single row DataFrame with the provided inputs
36
  row = {
37
  "Height": height,
38
  "Width": width,
 
40
  "Page Count": page_count,
41
  }
42
  df = pd.DataFrame([row])
43
+
44
+ # Run prediction with the loaded AutoGluon model
45
  pred = int(predictor.predict(df)[0])
46
 
47
+ # Return the prediction as a string
48
  return f"Predicted Genre Code: {pred}"
49
 
50
+ # Build the Gradio UI
51
  with gr.Blocks(title="Book Genre Predictor") as demo:
52
  gr.Markdown("## Predict the Genre of a Book (Numeric Labels)")
53
 
54
+ # Input fields arranged in a row and column layout
55
  with gr.Row():
56
  with gr.Column():
57
  height = gr.Number(label="Height (cm)", info="Book height in cm")
 
59
  depth = gr.Number(label="Depth (cm)", info="Book spine thickness in cm")
60
  page_count = gr.Number(label="Page Count", info="Number of pages (positive integer)")
61
 
62
+ # Output text box to display results
63
  out = gr.Textbox(label="Result")
64
+
65
+ # Prediction button
66
  run_btn = gr.Button("Predict")
67
 
68
+ # Connect button click to prediction function
69
  run_btn.click(
70
  predict_tabular,
71
  inputs=[height, width, depth, page_count],
72
  outputs=out
73
  )
74
 
75
+ # Pre loaded example inputs for quick testing
76
  gr.Examples(
77
  examples=[
78
  [20.1, 13.5, 1.8, 250],
 
83
  outputs=out
84
  )
85
 
86
+ # Launch the Gradio app
87
  demo.launch(share=True)