mohitk24 commited on
Commit
8efa7a7
·
verified ·
1 Parent(s): 793a092

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import zipfile
4
+ import pathlib
5
+ import tempfile
6
+ import gradio as gr
7
+ import pandas as pd
8
+ import numpy as np
9
+ import PIL.Image
10
+ import huggingface_hub as h
11
+ import autogluon.multimodal
12
+
13
+ model_repo_id = "nadakandrew/sign-identification-autogluon"
14
+ zip_filename = "autogluon_image_predictor_dir.zip"
15
+ HF_TOKEN = os.getenv("HF_TOKEN", None)
16
+ cache_dir = pathlib.Path("hf_assets")
17
+ extract_dir = cache_dir / "predictor_native"
18
+
19
+ def prepare_predictor_dir():
20
+ cache_dir.mkdir(parents=True, exist_ok=True)
21
+ local_zip = h.hf_hub_download(
22
+ repo_id=model_repo_id,
23
+ filename=zip_filename,
24
+ repo_type="model",
25
+ token=HF_TOKEN,
26
+ local_dir=str(cache_dir),
27
+ local_dir_use_symlinks=False,
28
+ )
29
+ if extract_dir.exists():
30
+ shutil.rmtree(extract_dir)
31
+ extract_dir.mkdir(parents=True, exist_ok=True)
32
+ with zipfile.ZipFile(local_zip, "r") as zf:
33
+ zf.extractall(str(extract_dir))
34
+ contents = list(extract_dir.iterdir())
35
+ predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else extract_dir
36
+ return str(predictor_root)
37
+
38
+ predictor_dir = prepare_predictor_dir()
39
+ predictor = autogluon.multimodal.MultiModalPredictor.load(predictor_dir)
40
+
41
+ def do_predict(pil_img, preprocess=True):
42
+ if pil_img is None:
43
+ return "No image provided.", None, None
44
+
45
+ original_img = pil_img.copy()
46
+ preprocessed_img = None
47
+
48
+ if preprocess:
49
+ target_size = (224, 224)
50
+ preprocessed_img = pil_img.resize(target_size).convert("RGB")
51
+ tmpdir = pathlib.Path(tempfile.mkdtemp())
52
+ img_path = tmpdir / "input.png"
53
+ preprocessed_img.save(img_path)
54
+ else:
55
+ tmpdir = pathlib.Path(tempfile.mkdtemp())
56
+ img_path = tmpdir / "input.png"
57
+ pil_img.save(img_path)
58
+
59
+ df = pd.DataFrame({"image": [str(img_path)]})
60
+ proba_df = predictor.predict_proba(df)
61
+ proba_df = proba_df.rename(columns={0: "class_0", 1: "class_1"})
62
+ row = proba_df.iloc[0]
63
+
64
+ pretty_dict = {
65
+ "Not a STOP sign": float(row.get("class_0", 0.0)),
66
+ "STOP sign": float(row.get("class_1", 0.0)),
67
+ }
68
+
69
+ return pretty_dict, original_img, preprocessed_img
70
+
71
+ EXAMPLES = [
72
+ ["https://universalsigns.com/wp-content/uploads/2022/08/StopSign-3.jpg"],
73
+ ["https://images.roadtrafficsigns.com/img/pla/K/student-drop-off-area-sign-k-2459_pl.png"],
74
+ ["https://hansonsign.com/wp-content/uploads/2024/05/donatos-662x646.jpg"]
75
+ ]
76
+
77
+
78
+ with gr.Blocks() as demo:
79
+ gr.Markdown("# Is this a STOP sign or not?")
80
+ gr.Markdown("Upload a photo to see results.")
81
+
82
+ with gr.Row():
83
+ image_in = gr.Image(type="pil", label="Input image", sources=["upload", "webcam"])
84
+ original_img_out = gr.Image(type="pil", label="Original image")
85
+ preprocessed_img_out = gr.Image(type="pil", label="Preprocessed image")
86
+
87
+ with gr.Row():
88
+ preprocess_checkbox = gr.Checkbox(label="Apply Preprocessing", value=True)
89
+
90
+ proba_pretty = gr.Label(num_top_classes=2, label="Class probabilities")
91
+
92
+ image_in.change(
93
+ fn=do_predict,
94
+ inputs=[image_in, preprocess_checkbox],
95
+ outputs=[proba_pretty, original_img_out, preprocessed_img_out]
96
+ )
97
+
98
+ gr.Examples(
99
+ examples=EXAMPLES,
100
+ inputs=[image_in],
101
+ label="Choose any one",
102
+ examples_per_page=8,
103
+ cache_examples=False,
104
+ )
105
+
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()