Spaces:
Runtime error
Runtime error
| import os # For reading environment variables | |
| import shutil # For directory cleanup | |
| import zipfile # For extracting model archives | |
| import pathlib # For path manipulations | |
| import tempfile # For creating temporary files/directories | |
| import gradio # For interactive UI | |
| import pandas # For tabular data handling | |
| import PIL.Image # For image I/O | |
| import huggingface_hub # For downloading model assets | |
| import autogluon.multimodal # For loading AutoGluon image classifier | |
| # Hardcoded Hub model (native zip) | |
| MODEL_REPO_ID = "yusenthebot/sign-identification-autogluon" # Updated model ID | |
| ZIP_FILENAME = "autogluon_sign_predictor_dir.zip" | |
| HF_TOKEN = os.getenv("HF_TOKEN", None) | |
| # Local cache/extract dirs | |
| CACHE_DIR = pathlib.Path("hf_assets") | |
| EXTRACT_DIR = CACHE_DIR / "predictor_native" | |
| # Download & load the native predictor | |
| def _prepare_predictor_dir() -> str: | |
| CACHE_DIR.mkdir(parents=True, exist_ok=True) | |
| local_zip = huggingface_hub.hf_hub_download( | |
| repo_id=MODEL_REPO_ID, | |
| filename=ZIP_FILENAME, | |
| repo_type="model", | |
| token=HF_TOKEN, | |
| local_dir=str(CACHE_DIR), | |
| local_dir_use_symlinks=False, | |
| ) | |
| if EXTRACT_DIR.exists(): | |
| shutil.rmtree(EXTRACT_DIR) | |
| EXTRACT_DIR.mkdir(parents=True, exist_ok=True) | |
| with zipfile.ZipFile(local_zip, "r") as zf: | |
| zf.extractall(str(EXTRACT_DIR)) | |
| contents = list(EXTRACT_DIR.iterdir()) | |
| predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR | |
| return str(predictor_root) | |
| PREDICTOR_DIR = _prepare_predictor_dir() | |
| PREDICTOR = autogluon.multimodal.MultiModalPredictor.load(PREDICTOR_DIR) | |
| # Explicit class labels (edit copy as desired) | |
| CLASS_LABELS = {0: "β No Stop Sign", 1: "π Stop Sign"} # Updated class labels | |
| # Helper to map model class -> human label | |
| def _human_label(c): | |
| try: | |
| ci = int(c) | |
| return CLASS_LABELS.get(ci, str(c)) | |
| except Exception: | |
| return CLASS_LABELS.get(c, str(c)) | |
| # Do the prediction! | |
| def do_predict(pil_img: PIL.Image.Image): | |
| # Make sure there's actually an image to work with | |
| if pil_img is None: | |
| return "No image provided.", {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"]) | |
| # IF we have something to work with, save it and prepare the input | |
| tmpdir = pathlib.Path(tempfile.mkdtemp()) | |
| img_path = tmpdir / "input.png" | |
| pil_img.save(img_path) | |
| df = pandas.DataFrame({"image": [str(img_path)]}) # For AutoGluon expected input format | |
| # For class probabilities | |
| proba_df = PREDICTOR.predict_proba(df) | |
| # For user-friendly column names | |
| proba_df = proba_df.rename(columns={0: "β No Stop Sign (0)", 1: "π Stop Sign (1)"}) # Updated column names | |
| row = proba_df.iloc[0] | |
| # For pretty ranked dict expected by gr.Label | |
| pretty_dict = { | |
| "β No Stop Sign": float(row.get("β No Stop Sign (0)", 0.0)), # Updated dictionary keys | |
| "π Stop Sign": float(row.get("π Stop Sign (1)", 0.0)), # Updated dictionary keys | |
| } | |
| return pretty_dict | |
| # Representative example images! These can be local or links. | |
| EXAMPLES = [ | |
| ["https://datasets-server.huggingface.co/assets/ecopus/sign_identification/--/a1506696eb48233ed9cd1afa1bea8e7002a7ad85/--/default/original/4/image/image.jpg?Expires=1760832682&Signature=m1wwlKKZ5Lkn2kN2Ty~zRgpJdXWwdXypqcrztCO001yw-2hYtU5ZfFHfmcp0miuoUGZDVdjEF4~3OAdBiACGotB5K5wjMQH~37EcHy~NYYsoFek2Zwg8rY7Syv4t9PnCpaScC4yN8tnRKQmHyIyq4Zj6Zr5JHNjMD5T6mgL9nH6Ul0PUojo~vEYXwc4FkCU1MPfuNWA3j4lIPiOwW2Daqfb-kmbeZcTFJ2tCk5CP8AbyWO5wncs~6JSiDCrj-1SQDgnlsarA95XLt6egDxbo0RBe2XWkV3jHHeQcNMd9aosD5LGPuAwa7LJnzcztwIcpx5iqsD0b5uHUcuh779qETw__&Key-Pair-Id=K3EI6M078Z3AC3"], # Example from the new dataset | |
| ["https://datasets-server.huggingface.co/assets/ecopus/sign_identification/--/a1506696eb48233ed9cd1afa1bea8e7002a7ad85/--/default/original/5/image/image.jpg?Expires=1760832682&Signature=ZZrth953~hKHf30qk9IfV-IW3xoZKmhYHdu-bZfWe0BbFHoRgMFNHkZG5zfb4npHxK6iPD5RXoFpdIipzpGZNRWCFRYy7rYXbm60fXFLFozwGBYhuGgV5-RIL~A-fvziB0-5KVL8BsC9cpy9YNeOv6PRKX36wEW6z8B7p8TTFV3A6u6KpfAuiyxl7GHISzLePXJlJtmmY6JeKAq4TUd99FxWitELwD4qtCcntP05FdldDycuQaqan5pDKcj6phCfa-p2vICEWyRyr8BPv3HnOcgdzcX1pTARLzf9lWiVz~TD5e4bJKPmhD2N~4k544OzQzZu9BvlGnE1YU1VIC2iGg__&Key-Pair-Id=K3EI6M078Z3AC3"], | |
| ["https://datasets-server.huggingface.co/assets/ecopus/sign_identification/--/a1506696eb48233ed9cd1afa1bea8e7002a7ad85/--/default/original/10/image/image.jpg?Expires=1760832682&Signature=Od6vvEE2Oh3UeagI5aWd6U60AXj9PymrbJhIQ2z6iS0y5DAw4xh2lPH3409TfJ5e9L~2FcxP1sl3wbkyV8GMEY~q8RYfxwpVkwMswcV3dK1ddvnWYFvJwtWnNExZxsDtgPO8h~KOsUcm5GLYnG7Wd21ibQ8r22-kVuRQb5jRpJLLBFAA0l9XMn-vLjHJAEwUrUUy15VKUL1G2oxFxhZQMxDXwS0QEaeNDzu9iMyy1YOvhN-jF4EFAf7pwPZggRfNIGuRYhemoB2tor-YU~KQAURiAD0mZ80ojbeihMcbFq6TB6~watBFegrYqCfp~~kTmLA0exqAkXmsHReqe26XPA__&Key-Pair-Id=K3EI6M078Z3AC3"],# Example from the new dataset | |
| ["https://datasets-server.huggingface.co/assets/ecopus/sign_identification/--/a1506696eb48233ed9cd1afa1bea8e7002a7ad85/--/default/original/11/image/image.jpg?Expires=1760832682&Signature=ObjUmUy3TxAdYQWbjo7d5n9LkDRc0ue-wt75eteR~tiQkCNn9yLqTRMUdefIvzKOoeUHTCzMQi7TZeHo7ik4LDNjdPaFdNmGx2TJLkGYogKLMIZNLKa1uSNYBifOq8w9WlJyaA8qc03C7m0hogbbrLtGsXyPk4ET9XmCsR3yU5w1DBXvsV4FPohiAuRqcMAFYOKDwqNZyeuRkNUIEXCsma68jESnDDJP0rXQruOZkd~Tm1GFnh8uB3zmwmby1m2bBjmNrAPPhnVDELD1OkpQUOrnAMuhHbcsg4S~Wo0sWPdnWgfONLw7j5xkp~GdJ3Nw~a57X-A~~Bo15ZhTgIHPhg__&Key-Pair-Id=K3EI6M078Z3AC3"] # Example from the new dataset | |
| ] | |
| # Gradio UI | |
| with gradio.Blocks() as demo: | |
| # Provide an introduction | |
| gradio.Markdown("# Stop Sign Detection?") # Updated title | |
| gradio.Markdown(""" | |
| This is a simple app that demonstrates how to use an autogluon multimodal | |
| predictor in a gradio space to predict if a picture contains a stop sign. To use, | |
| just upload a photo. The result will be generated automatically if there is or is not a stop sign. | |
| """) # Updated description | |
| # Interface for the incoming image | |
| image_in = gradio.Image(type="pil", label="Input image", sources=["upload", "webcam"]) | |
| # Interface elements to show htte result and probabilities | |
| proba_pretty = gradio.Label(num_top_classes=2, label="Class probabilities") | |
| # Whenever a new image is uploaded, update the result | |
| image_in.change(fn=do_predict, inputs=[image_in], outputs=[proba_pretty]) | |
| # For clickable example images | |
| gradio.Examples( | |
| examples=EXAMPLES, | |
| inputs=[image_in], | |
| label="Representative examples", | |
| examples_per_page=8, | |
| cache_examples=False, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |