Initial commit: add Gradio app and requirements
Browse files- app.py +18 -10
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 16 |
-
cache_dir
|
| 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,7 +40,7 @@ def prepare_predictor_dir():
|
|
| 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,8 +58,11 @@ def do_predict(pil_img, preprocess=True):
|
|
| 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,6 +73,7 @@ def do_predict(pil_img, preprocess=True):
|
|
| 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"],
|
|
@@ -75,6 +81,7 @@ EXAMPLES = [
|
|
| 75 |
]
|
| 76 |
|
| 77 |
with gr.Blocks() as demo:
|
|
|
|
| 78 |
gr.Markdown("# Is this a STOP sign or not?")
|
| 79 |
gr.Markdown("Upload a photo to see results.")
|
| 80 |
|
|
@@ -105,3 +112,4 @@ with gr.Blocks() as demo:
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch()
|
|
|
|
|
|
| 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 = "autogluon_image_predictor_dir.zip"
|
| 17 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 18 |
+
cache_dir = pathlib.Path("hf_assets")
|
| 19 |
extract_dir = cache_dir / "predictor_native"
|
| 20 |
|
| 21 |
+
def prepare_predictor_dir() -> str:
|
| 22 |
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 23 |
local_zip = h.hf_hub_download(
|
| 24 |
repo_id=model_repo_id,
|
|
|
|
| 40 |
predictor_dir = prepare_predictor_dir()
|
| 41 |
predictor = autogluon.multimodal.MultiModalPredictor.load(predictor_dir)
|
| 42 |
|
| 43 |
+
def do_predict(pil_img: PIL.Image.Image, preprocess: bool = True):
|
| 44 |
if pil_img is None:
|
| 45 |
return "No image provided.", None, None
|
| 46 |
|
|
|
|
| 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 |
|
| 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"],
|
|
|
|
| 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 |
|
|
|
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
| 115 |
+
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
-
pandas
|
| 3 |
-
numpy
|
| 4 |
pillow
|
| 5 |
-
autogluon.multimodal
|
| 6 |
huggingface_hub
|
|
|
|
| 1 |
+
|
| 2 |
+
autogluon.multimodal
|
| 3 |
gradio
|
|
|
|
|
|
|
| 4 |
pillow
|
|
|
|
| 5 |
huggingface_hub
|