samder03 commited on
Commit
3cebcb7
·
verified ·
1 Parent(s): 1c43bd3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os # For reading environment variables
2
+ import shutil # For directory cleanup
3
+ import zipfile # For extracting model archives
4
+ import pathlib # For path manipulations
5
+ import tempfile # For creating temporary files/directories
6
+
7
+ import gradio # For interactive UI
8
+ import pandas # For tabular data handling
9
+ import PIL.Image # For image I/O
10
+
11
+ import huggingface_hub # For downloading model assets
12
+ import autogluon.multimodal # For loading AutoGluon image classifier
13
+
14
+ # Hardcoded Hub model (native zip)
15
+ MODEL_REPO_ID = "SebastianAndreu/2025-24679-HW1-Part2-image-autogluon-predictor"
16
+ ZIP_FILENAME = "autogluon_image_predictor_dir.zip"
17
+ HF_TOKEN = os.getenv("HF_TOKEN", None)
18
+
19
+ # Local cache/extract dirs
20
+ CACHE_DIR = pathlib.Path("hf_assets")
21
+ EXTRACT_DIR = CACHE_DIR / "predictor_native"
22
+
23
+ # Download & load the native predictor
24
+ def _prepare_predictor_dir() -> str:
25
+ CACHE_DIR.mkdir(parents=True, exist_ok=True)
26
+ local_zip = huggingface_hub.hf_hub_download(
27
+ repo_id=MODEL_REPO_ID,
28
+ filename=ZIP_FILENAME,
29
+ repo_type="model",
30
+ token=HF_TOKEN,
31
+ local_dir=str(CACHE_DIR),
32
+ local_dir_use_symlinks=False,
33
+ )
34
+ if EXTRACT_DIR.exists():
35
+ shutil.rmtree(EXTRACT_DIR)
36
+ EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
37
+ with zipfile.ZipFile(local_zip, "r") as zf:
38
+ zf.extractall(str(EXTRACT_DIR))
39
+ contents = list(EXTRACT_DIR.iterdir())
40
+ predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
41
+ return str(predictor_root)
42
+
43
+ PREDICTOR_DIR = _prepare_predictor_dir()
44
+ PREDICTOR = autogluon.multimodal.MultiModalPredictor.load(PREDICTOR_DIR)
45
+
46
+ # Explicit class labels (edit copy as desired)
47
+ class_labels = {0: "No Duo", 1: "Duo"}
48
+
49
+ # Helper to map model class -> human label
50
+ def _human_label(c):
51
+ try:
52
+ ci = int(c)
53
+ return class_labels.get(ci, str(c))
54
+ except Exception:
55
+ return class_labels.get(c, str(c))
56
+
57
+ # Do the prediction!
58
+ def do_predict(pil_img: PIL.Image.Image):
59
+ # Make sure there's actually an image to work with
60
+ if pil_img is None:
61
+ return "No image provided.", {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"])
62
+
63
+ # IF we have something to work with, save it and prepare the input
64
+ tmpdir = pathlib.Path(tempfile.mkdtemp())
65
+ img_path = tmpdir / "input.png"
66
+ pil_img.save(img_path)
67
+
68
+ df = pandas.DataFrame({"image": [str(img_path)]}) # For AutoGluon expected input format
69
+
70
+ # For class probabilities
71
+ proba_df = PREDICTOR.predict_proba(df)
72
+
73
+ # For user-friendly column names
74
+ proba_df = proba_df.rename(columns={0: "No Duo", 1: "Duo"})
75
+ row = proba_df.iloc[0]
76
+
77
+ # For pretty ranked dict expected by gr.Label
78
+ pretty_dict = {
79
+ "No Duo": float(row.get("No Duo", 0.0)),
80
+ "Duo": float(row.get("Duo", 0.0)),
81
+ }
82
+
83
+ return pretty_dict
84
+
85
+ # Representative example images! These can be local or links.
86
+ EXAMPLES = [
87
+ ["https://i5.walmartimages.com/seo/Green-Duolingo-Owl-Plush-Toy-Duo-Plushie-Of-Duo-The-Owl-Cartoon-Anime-Owl-Doll_400e5bf6-8527-4fa5-b8d2-30d84105d412.20f81142253890c3c961b6aec37feb0d.jpeg"],
88
+ ["https://i.ytimg.com/vi/WFkYQrOQBBU/maxresdefault.jpg"],
89
+ ["https://greggvanourek.com/wp-content/uploads/2023/08/Nature-path-by-water-trees-and-mountains-AdobeStock_291242770-scaled.jpeg"]
90
+ ]
91
+
92
+ # Gradio UI
93
+ with gradio.Blocks() as demo:
94
+
95
+ # Provide an introduction
96
+ gradio.Markdown("# Duo or no Duo?")
97
+ gradio.Markdown("""
98
+ This is an app that detects whether or not Duo is in an image or not. To use,
99
+ just upload a photo. The result should be generated automatically.
100
+ """)
101
+
102
+ # Interface for the incoming image
103
+ image_in = gradio.Image(type="pil", label="Input image", sources=["upload", "webcam"])
104
+
105
+ # Interface elements to show htte result and probabilities
106
+ proba_pretty = gradio.Label(num_top_classes=2, label="Class probabilities")
107
+
108
+ # Whenever a new image is uploaded, update the result
109
+ image_in.change(fn=do_predict, inputs=[image_in], outputs=[proba_pretty])
110
+
111
+ # For clickable example images
112
+ gradio.Examples(
113
+ examples=EXAMPLES,
114
+ inputs=[image_in],
115
+ label="Representative examples",
116
+ examples_per_page=8,
117
+ cache_examples=False,
118
+ )
119
+
120
+ if __name__ == "__main__":
121
+ demo.launch()