Spaces:
Sleeping
Create app.py
Browse filesThis Python code creates an interactive image classification application using AutoGluon MultiModal and Gradio to determine if a picture contains a tomato.
Key Functionality and Components
The application performs the following main steps:
Model Setup: It uses the huggingface_hub library to download and extract a pre-trained AutoGluon MultiModal image predictor from a specified Hugging Face model repository (apsora/autoML_images_data). This model is loaded locally.
Prediction Logic: The do_predict function takes an uploaded image, saves it temporarily, and then uses the loaded MultiModalPredictor to classify the image into one of two classes: "π
Tomato" or "π« Not a tomato." It returns the class probabilities for display.
Interactive User Interface (Gradio):
It creates a user-friendly web interface using Gradio. Users can upload an image or capture one using a webcam.When a new image is provided, the do_predict function runs automatically and the result is displayed in a Gradio Label component, showing the predicted class and the confidence score (probability) for both "Tomato" and "Not a tomato." Example images are provided to demonstrate the application's capabilities.
In essence, this is a deployable minimal example demonstrating how to serve a machine learning model, specifically an AutoGluon image classifier, within a Gradio interface.
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install autogluon.multimodal --quiet
|
| 2 |
+
|
| 3 |
+
import os # For reading environment variables
|
| 4 |
+
import shutil # For directory cleanup
|
| 5 |
+
import zipfile # For extracting model archives
|
| 6 |
+
import pathlib # For path manipulations
|
| 7 |
+
import tempfile # For creating temporary files/directories
|
| 8 |
+
|
| 9 |
+
import gradio # For interactive UI
|
| 10 |
+
import pandas # For tabular data handling
|
| 11 |
+
import PIL.Image # For image I/O
|
| 12 |
+
|
| 13 |
+
import huggingface_hub # For downloading model assets
|
| 14 |
+
import autogluon.multimodal # For loading AutoGluon image classifier
|
| 15 |
+
|
| 16 |
+
# Hardcoded Hub model (native zip)
|
| 17 |
+
MODEL_REPO_ID = "apsora/autoML_images_data"
|
| 18 |
+
ZIP_FILENAME = "autogluon_image_predictor_dir.zip"
|
| 19 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 20 |
+
|
| 21 |
+
# Local cache/extract dirs
|
| 22 |
+
CACHE_DIR = pathlib.Path("hf_assets")
|
| 23 |
+
EXTRACT_DIR = CACHE_DIR / "predictor_native"
|
| 24 |
+
|
| 25 |
+
# Download & load the native predictor
|
| 26 |
+
def _prepare_predictor_dir() -> str:
|
| 27 |
+
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
local_zip = huggingface_hub.hf_hub_download(
|
| 29 |
+
repo_id=MODEL_REPO_ID,
|
| 30 |
+
filename=ZIP_FILENAME,
|
| 31 |
+
repo_type="model",
|
| 32 |
+
token=HF_TOKEN,
|
| 33 |
+
local_dir=str(CACHE_DIR),
|
| 34 |
+
local_dir_use_symlinks=False,
|
| 35 |
+
)
|
| 36 |
+
if EXTRACT_DIR.exists():
|
| 37 |
+
shutil.rmtree(EXTRACT_DIR)
|
| 38 |
+
EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
|
| 39 |
+
with zipfile.ZipFile(local_zip, "r") as zf:
|
| 40 |
+
zf.extractall(str(EXTRACT_DIR))
|
| 41 |
+
contents = list(EXTRACT_DIR.iterdir())
|
| 42 |
+
predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
|
| 43 |
+
return str(predictor_root)
|
| 44 |
+
|
| 45 |
+
PREDICTOR_DIR = _prepare_predictor_dir()
|
| 46 |
+
PREDICTOR = autogluon.multimodal.MultiModalPredictor.load(PREDICTOR_DIR)
|
| 47 |
+
|
| 48 |
+
# Explicit class labels (edit copy as desired)
|
| 49 |
+
CLASS_LABELS = {0: "π« Not a tomato", 1: "π
Tomato"}
|
| 50 |
+
|
| 51 |
+
# Helper to map model class -> human label
|
| 52 |
+
def _human_label(c):
|
| 53 |
+
try:
|
| 54 |
+
ci = int(c)
|
| 55 |
+
return CLASS_LABELS.get(ci, str(c))
|
| 56 |
+
except Exception:
|
| 57 |
+
return CLASS_LABELS.get(c, str(c))
|
| 58 |
+
|
| 59 |
+
# Do the prediction!
|
| 60 |
+
def do_predict(pil_img: PIL.Image.Image):
|
| 61 |
+
# Make sure there's actually an image to work with
|
| 62 |
+
if pil_img is None:
|
| 63 |
+
return "No image provided.", {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"])
|
| 64 |
+
|
| 65 |
+
# IF we have something to work with, save it and prepare the input
|
| 66 |
+
tmpdir = pathlib.Path(tempfile.mkdtemp())
|
| 67 |
+
img_path = tmpdir / "input.png"
|
| 68 |
+
pil_img.save(img_path)
|
| 69 |
+
|
| 70 |
+
df = pandas.DataFrame({"image": [str(img_path)]}) # For AutoGluon expected input format
|
| 71 |
+
|
| 72 |
+
# For class probabilities
|
| 73 |
+
proba_df = PREDICTOR.predict_proba(df)
|
| 74 |
+
|
| 75 |
+
# For user-friendly column names
|
| 76 |
+
proba_df = proba_df.rename(columns={0: "π« Not a tomato (0)", 1: "π
Tomato (1)"})
|
| 77 |
+
row = proba_df.iloc[0]
|
| 78 |
+
|
| 79 |
+
# For pretty ranked dict expected by gr.Label
|
| 80 |
+
pretty_dict = {
|
| 81 |
+
"π« Not a tomato": float(row.get("π« Not a tomato (0)", 0.0)),
|
| 82 |
+
"π
Tomato": float(row.get("π
Tomato (1)", 0.0)),
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
return pretty_dict
|
| 86 |
+
|
| 87 |
+
# Representative example images! These can be local or links.
|
| 88 |
+
EXAMPLES = [
|
| 89 |
+
["https://dengarden.com/.image/w_1920,q_auto:good,c_limit/MTk3NDQ3MTk3NDE4MDcxMDQ2/how-to-get-the-highest-yield-and-best-flavor-from-tomatoes.jpg"],
|
| 90 |
+
["https://www.greenlanedelivery.com/cdn/shop/products/Grapes_White_SL_1200x1200.jpg?v=1671549475"],
|
| 91 |
+
["https://agrinigeriaprodsa.blob.core.windows.net/agrifarmer/a8738a87-3e02-4d1c-8ba7-e028205ee6bb.jpg"]
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
# Gradio UI
|
| 95 |
+
with gradio.Blocks() as demo:
|
| 96 |
+
|
| 97 |
+
# Provide an introduction
|
| 98 |
+
gradio.Markdown("# Tomato or No Tomato?")
|
| 99 |
+
gradio.Markdown("""
|
| 100 |
+
This is a simple app that demonstrates how to use an autogluon multimodal
|
| 101 |
+
predictor in a gradio space to predict the contents of a picture. To use,
|
| 102 |
+
just upload a photo. The result should be generated automatically.
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
# Interface for the incoming image
|
| 106 |
+
image_in = gradio.Image(type="pil", label="Input image", sources=["upload", "webcam"])
|
| 107 |
+
|
| 108 |
+
# Interface elements to show htte result and probabilities
|
| 109 |
+
proba_pretty = gradio.Label(num_top_classes=2, label="Class probabilities")
|
| 110 |
+
|
| 111 |
+
# Whenever a new image is uploaded, update the result
|
| 112 |
+
image_in.change(fn=do_predict, inputs=[image_in], outputs=[proba_pretty])
|
| 113 |
+
|
| 114 |
+
# For clickable example images
|
| 115 |
+
gradio.Examples(
|
| 116 |
+
examples=EXAMPLES,
|
| 117 |
+
inputs=[image_in],
|
| 118 |
+
label="Representative examples",
|
| 119 |
+
examples_per_page=8,
|
| 120 |
+
cache_examples=False,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|