|
|
import os |
|
|
import shutil |
|
|
import zipfile |
|
|
import pathlib |
|
|
import tempfile |
|
|
|
|
|
import gradio |
|
|
import pandas |
|
|
import PIL.Image |
|
|
|
|
|
import huggingface_hub |
|
|
import autogluon.multimodal |
|
|
|
|
|
|
|
|
MODEL_REPO_ID = "yl0628/autogluon-image-predictor" |
|
|
ZIP_FILENAME = "autogluon_image_predictor_dir.zip" |
|
|
|
|
|
|
|
|
CACHE_DIR = pathlib.Path("hf_assets") |
|
|
EXTRACT_DIR = CACHE_DIR / "predictor_native" |
|
|
|
|
|
|
|
|
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", |
|
|
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) |
|
|
|
|
|
|
|
|
CLASS_LABELS = {0: "no_tomato", 1: "tomato"} |
|
|
|
|
|
|
|
|
def _human_label(c): |
|
|
try: |
|
|
ci = int(c) |
|
|
return CLASS_LABELS.get(ci, str(c)) |
|
|
except Exception: |
|
|
return CLASS_LABELS.get(c, str(c)) |
|
|
|
|
|
|
|
|
def do_predict(pil_img: PIL.Image.Image): |
|
|
|
|
|
if pil_img is None: |
|
|
return "No image provided.", {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"]) |
|
|
|
|
|
|
|
|
tmpdir = pathlib.Path(tempfile.mkdtemp()) |
|
|
img_path = tmpdir / "input.png" |
|
|
pil_img.save(img_path) |
|
|
|
|
|
df = pandas.DataFrame({"image": [str(img_path)]}) |
|
|
|
|
|
|
|
|
proba_df = PREDICTOR.predict_proba(df) |
|
|
|
|
|
|
|
|
proba_df = proba_df.rename(columns={0: "no_tomato", 1: "tomato"}) |
|
|
row = proba_df.iloc[0] |
|
|
|
|
|
|
|
|
pretty_dict = { |
|
|
"no_tomato": float(row.get("no_tomato", 0.0)), |
|
|
"tomato": float(row.get("tomato", 0.0)), |
|
|
} |
|
|
|
|
|
return pretty_dict |
|
|
|
|
|
|
|
|
EXAMPLES = [ |
|
|
["Tomato1.jpg"], |
|
|
["Tomato2.jpg"], |
|
|
["Carrots.jpg"] |
|
|
] |
|
|
|
|
|
|
|
|
with gradio.Blocks() as demo: |
|
|
|
|
|
|
|
|
gradio.Markdown("# Tomato or Not?") |
|
|
gradio.Markdown(""" |
|
|
This is a simple app that uses the model at kaitongg/best_tomato_model to classify whether an image has a tomato or not, |
|
|
utilizing data found at Iris314/Food_tomatoes_dataset. To use the interface, upload an image in the area shown below. |
|
|
""") |
|
|
|
|
|
|
|
|
image_in = gradio.Image(type="pil", label="Input image", sources=["upload", "webcam"]) |
|
|
|
|
|
|
|
|
proba_pretty = gradio.Label(num_top_classes=2, label="Class probabilities") |
|
|
|
|
|
|
|
|
image_in.change(fn=do_predict, inputs=[image_in], outputs=[proba_pretty]) |
|
|
|
|
|
|
|
|
gradio.Examples( |
|
|
examples=EXAMPLES, |
|
|
inputs=[image_in], |
|
|
label="Representative examples", |
|
|
examples_per_page=8, |
|
|
cache_examples=False, |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |