Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,24 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import PIL.Image
|
| 4 |
-
import numpy as np
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import pathlib
|
| 7 |
-
import tempfile
|
| 8 |
import os
|
| 9 |
import shutil
|
| 10 |
import zipfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
import huggingface_hub as h
|
| 12 |
-
from huggingface_hub import HfApi, Repository
|
| 13 |
import autogluon.multimodal
|
| 14 |
|
| 15 |
model_repo_id = "nadakandrew/sign-identification-autogluon"
|
| 16 |
-
zip_filename
|
| 17 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 18 |
-
cache_dir
|
| 19 |
extract_dir = cache_dir / "predictor_native"
|
| 20 |
|
| 21 |
-
def prepare_predictor_dir()
|
| 22 |
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 23 |
local_zip = h.hf_hub_download(
|
| 24 |
repo_id=model_repo_id,
|
|
@@ -40,7 +38,7 @@ def prepare_predictor_dir() -> str:
|
|
| 40 |
predictor_dir = prepare_predictor_dir()
|
| 41 |
predictor = autogluon.multimodal.MultiModalPredictor.load(predictor_dir)
|
| 42 |
|
| 43 |
-
def do_predict(pil_img
|
| 44 |
if pil_img is None:
|
| 45 |
return "No image provided.", None, None
|
| 46 |
|
|
@@ -58,11 +56,8 @@ def do_predict(pil_img: PIL.Image.Image, preprocess: bool = True):
|
|
| 58 |
img_path = tmpdir / "input.png"
|
| 59 |
pil_img.save(img_path)
|
| 60 |
|
| 61 |
-
|
| 62 |
df = pd.DataFrame({"image": [str(img_path)]})
|
| 63 |
-
|
| 64 |
proba_df = predictor.predict_proba(df)
|
| 65 |
-
|
| 66 |
proba_df = proba_df.rename(columns={0: "class_0", 1: "class_1"})
|
| 67 |
row = proba_df.iloc[0]
|
| 68 |
|
|
@@ -73,15 +68,8 @@ def do_predict(pil_img: PIL.Image.Image, preprocess: bool = True):
|
|
| 73 |
|
| 74 |
return pretty_dict, original_img, preprocessed_img
|
| 75 |
|
| 76 |
-
|
| 77 |
-
EXAMPLES = [
|
| 78 |
-
["https://universalsigns.com/wp-content/uploads/2022/08/StopSign-3.jpg"],
|
| 79 |
-
["https://images.roadtrafficsigns.com/img/pla/K/student-drop-off-area-sign-k-2459_pl.png"],
|
| 80 |
-
["https://hansonsign.com/wp-content/uploads/2024/05/donatos-662x646.jpg"]
|
| 81 |
-
]
|
| 82 |
-
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
-
|
| 85 |
gr.Markdown("# Is this a STOP sign or not?")
|
| 86 |
gr.Markdown("Upload a photo to see results.")
|
| 87 |
|
|
@@ -101,15 +89,7 @@ with gr.Blocks() as demo:
|
|
| 101 |
outputs=[proba_pretty, original_img_out, preprocessed_img_out]
|
| 102 |
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
examples=EXAMPLES,
|
| 106 |
-
inputs=[image_in],
|
| 107 |
-
label="Choose any one",
|
| 108 |
-
examples_per_page=8,
|
| 109 |
-
cache_examples=False,
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 68 |
|
| 69 |
return pretty_dict, original_img, preprocessed_img
|
| 70 |
|
| 71 |
+
# Remove external URLs - use local examples or none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Blocks() as demo:
|
|
|
|
| 73 |
gr.Markdown("# Is this a STOP sign or not?")
|
| 74 |
gr.Markdown("Upload a photo to see results.")
|
| 75 |
|
|
|
|
| 89 |
outputs=[proba_pretty, original_img_out, preprocessed_img_out]
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# No examples with external URLs to avoid connection errors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
demo.launch()
|
|
|