Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio
|
| 5 |
+
import pathlib
|
| 6 |
+
import os # For filesystem operations
|
| 7 |
+
import shutil # For directory cleanup
|
| 8 |
+
import zipfile # For extracting model archives
|
| 9 |
+
import huggingface_hub
|
| 10 |
+
# import huggingface
|
| 11 |
+
from huggingface_hub import login, create_repo, HfApi
|
| 12 |
+
import autogluon
|
| 13 |
+
from autogluon import tabular
|
| 14 |
+
|
| 15 |
+
# Model config
|
| 16 |
+
MODEL_REPO_ID = "cassieli226/sign-identification-automl"
|
| 17 |
+
ZIP_FILENAME = "autogluon_predictor_dir.zip"
|
| 18 |
+
CACHE_DIR = pathlib.Path("hf_assets")
|
| 19 |
+
EXTRACT_DIR = CACHE_DIR / "predictor_native"
|
| 20 |
+
|
| 21 |
+
# Download & load the native predictor
|
| 22 |
+
def _prepare_predictor_dir() -> str:
|
| 23 |
+
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
| 24 |
+
local_zip = huggingface_hub.hf_hub_download(
|
| 25 |
+
repo_id=MODEL_REPO_ID,
|
| 26 |
+
filename=ZIP_FILENAME,
|
| 27 |
+
repo_type="model",
|
| 28 |
+
local_dir=str(CACHE_DIR),
|
| 29 |
+
local_dir_use_symlinks=False,
|
| 30 |
+
)
|
| 31 |
+
if EXTRACT_DIR.exists():
|
| 32 |
+
shutil.rmtree(EXTRACT_DIR)
|
| 33 |
+
EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
|
| 34 |
+
with zipfile.ZipFile(local_zip, "r") as zf:
|
| 35 |
+
zf.extractall(str(EXTRACT_DIR))
|
| 36 |
+
contents = list(EXTRACT_DIR.iterdir())
|
| 37 |
+
predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
|
| 38 |
+
return str(predictor_root)
|
| 39 |
+
|
| 40 |
+
PREDICTOR_DIR = _prepare_predictor_dir()
|
| 41 |
+
PREDICTOR = autogluon.tabular.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False)
|
| 42 |
+
|
| 43 |
+
clf = PREDICTOR # autogluon.tabular predictor
|
| 44 |
+
|
| 45 |
+
def predict_image(image, confidence_threshold=0.5, return_probabilities=False):
|
| 46 |
+
if image is None:
|
| 47 |
+
return "Please upload an image", ""
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Make prediction
|
| 51 |
+
prediction = clf.predict(image)
|
| 52 |
+
|
| 53 |
+
detailed = f"**Predicted Class:** {prediction}"
|
| 54 |
+
|
| 55 |
+
if return_probabilities:
|
| 56 |
+
try:
|
| 57 |
+
# Get prediction probabilities
|
| 58 |
+
probs = clf.predict_proba(image)
|
| 59 |
+
if hasattr(probs, 'iloc'):
|
| 60 |
+
prob_dict = probs.iloc[0].to_dict()
|
| 61 |
+
else:
|
| 62 |
+
prob_dict = dict(probs)
|
| 63 |
+
|
| 64 |
+
sorted_probs = sorted(prob_dict.items(), key=lambda x: x[1], reverse=True)
|
| 65 |
+
detailed += "\n\n**Prediction Probabilities:**\n"
|
| 66 |
+
|
| 67 |
+
for class_name, prob in sorted_probs[:5]:
|
| 68 |
+
if prob >= confidence_threshold:
|
| 69 |
+
detailed += f"- {class_name}: {prob:.3f}\n"
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
detailed += f"\n\nNote: Could not retrieve probabilities ({str(e)})"
|
| 73 |
+
|
| 74 |
+
return prediction, detailed
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error: {str(e)}", f"**Error:** {str(e)}"
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(title="Image Classifier", theme=gr.themes.Soft()) as demo:
|
| 80 |
+
gr.Markdown("""
|
| 81 |
+
# 🖼️ AI Image Classifier
|
| 82 |
+
|
| 83 |
+
## Upload an image to get AI-powered classification results
|
| 84 |
+
|
| 85 |
+
This model uses AutoGluon to classify images into different categories.
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column():
|
| 90 |
+
image_input = gr.Image(
|
| 91 |
+
type="pil",
|
| 92 |
+
label="Upload Image",
|
| 93 |
+
height=300
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
gr.Markdown("### 🔧 Inference Parameters")
|
| 97 |
+
confidence_threshold = gr.Slider(
|
| 98 |
+
minimum=0.0,
|
| 99 |
+
maximum=1.0,
|
| 100 |
+
value=0.1,
|
| 101 |
+
step=0.05,
|
| 102 |
+
label="Confidence Threshold"
|
| 103 |
+
)
|
| 104 |
+
return_probabilities = gr.Checkbox(
|
| 105 |
+
label="Show Prediction Probabilities",
|
| 106 |
+
value=True
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
predict_btn = gr.Button("🎯 Classify Image", variant="primary", size="lg")
|
| 110 |
+
|
| 111 |
+
with gr.Column():
|
| 112 |
+
prediction_output = gr.Textbox(
|
| 113 |
+
label="Prediction Result",
|
| 114 |
+
interactive=False,
|
| 115 |
+
lines=1
|
| 116 |
+
)
|
| 117 |
+
detailed_output = gr.Markdown(
|
| 118 |
+
label="Detailed Analysis",
|
| 119 |
+
value="Results will appear here..."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
predict_btn.click(
|
| 123 |
+
fn=predict_image,
|
| 124 |
+
inputs=[image_input, confidence_threshold, return_probabilities],
|
| 125 |
+
outputs=[prediction_output, detailed_output]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
gr.Markdown("""
|
| 129 |
+
---
|
| 130 |
+
*Built with AutoGluon and Gradio*
|
| 131 |
+
|
| 132 |
+
**How to use:**
|
| 133 |
+
1. Upload an image (JPG, PNG, etc.)
|
| 134 |
+
2. Adjust confidence threshold if needed
|
| 135 |
+
3. Click 'Classify Image' to get results
|
| 136 |
+
""")
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
demo.launch()
|