Spaces:
Sleeping
Sleeping
[feat]: upgrade code deployment
Browse files- .github/workflows/sync_ahf.yaml +5 -0
- deployment/gradio/{main.py → app.py} +4 -2
- requirements.txt +1 -0
- src/infer.py +18 -2
.github/workflows/sync_ahf.yaml
CHANGED
|
@@ -25,6 +25,11 @@ jobs:
|
|
| 25 |
git config --global user.email "$EMAIL_USER"
|
| 26 |
git config --global user.name "tph-kds"
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
- name: Force push to Hugging Face Space
|
| 29 |
run: |
|
| 30 |
git remote add hf https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME || true
|
|
|
|
| 25 |
git config --global user.email "$EMAIL_USER"
|
| 26 |
git config --global user.name "tph-kds"
|
| 27 |
|
| 28 |
+
# Xóa file .pth trước khi push (để tránh bị Hugging Face chặn)
|
| 29 |
+
- name: Remove local model checkpoints
|
| 30 |
+
run: |
|
| 31 |
+
rm -f checkpoints/**/*.pth || true
|
| 32 |
+
|
| 33 |
- name: Force push to Hugging Face Space
|
| 34 |
run: |
|
| 35 |
git remote add hf https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME || true
|
deployment/gradio/{main.py → app.py}
RENAMED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from src.infer import inference_pipeline
|
| 3 |
|
| 4 |
-
model_path = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth"
|
|
|
|
| 5 |
|
| 6 |
def classify_image(
|
| 7 |
image_path: str
|
|
@@ -14,7 +15,8 @@ def classify_image(
|
|
| 14 |
try:
|
| 15 |
prediction = inference_pipeline(
|
| 16 |
image_path=image_path,
|
| 17 |
-
model_path=model_path
|
|
|
|
| 18 |
)
|
| 19 |
return f"Prediction: {prediction.capitalize()}"
|
| 20 |
except Exception as e:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from src.infer import inference_pipeline
|
| 3 |
|
| 4 |
+
# model_path = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth"
|
| 5 |
+
|
| 6 |
|
| 7 |
def classify_image(
|
| 8 |
image_path: str
|
|
|
|
| 15 |
try:
|
| 16 |
prediction = inference_pipeline(
|
| 17 |
image_path=image_path,
|
| 18 |
+
model_path=model_path,
|
| 19 |
+
hf=True
|
| 20 |
)
|
| 21 |
return f"Prediction: {prediction.capitalize()}"
|
| 22 |
except Exception as e:
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
scikit-learn==1.6.1
|
| 2 |
gradio==4.44.1
|
| 3 |
pydantic==2.10.6
|
|
|
|
| 4 |
# torch torchvision --index-url https://download.pytorch.org/whl/cu126
|
|
|
|
| 1 |
scikit-learn==1.6.1
|
| 2 |
gradio==4.44.1
|
| 3 |
pydantic==2.10.6
|
| 4 |
+
huggingface_hub==0.36.0
|
| 5 |
# torch torchvision --index-url https://download.pytorch.org/whl/cu126
|
src/infer.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from src.model import CatDogClassifier
|
| 3 |
from src.config import CatDogClassifierConfigs
|
| 4 |
|
| 5 |
def inference_pipeline(
|
| 6 |
image_path: str = "datasets/single_prediction/cat_or_dog_1.jpg",
|
| 7 |
-
model_path: str = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth"
|
|
|
|
| 8 |
):
|
| 9 |
|
| 10 |
# Initialize model
|
|
@@ -21,7 +23,21 @@ def inference_pipeline(
|
|
| 21 |
)
|
| 22 |
# Load state_dict
|
| 23 |
model = CatDogClassifier(configs=model_configs)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
y_pred = model.predict(
|
| 26 |
model=model,
|
| 27 |
image_path=image_path
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
from src.model import CatDogClassifier
|
| 4 |
from src.config import CatDogClassifierConfigs
|
| 5 |
|
| 6 |
def inference_pipeline(
|
| 7 |
image_path: str = "datasets/single_prediction/cat_or_dog_1.jpg",
|
| 8 |
+
model_path: str = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth",
|
| 9 |
+
hf: bool = False
|
| 10 |
):
|
| 11 |
|
| 12 |
# Initialize model
|
|
|
|
| 23 |
)
|
| 24 |
# Load state_dict
|
| 25 |
model = CatDogClassifier(configs=model_configs)
|
| 26 |
+
# Load weights
|
| 27 |
+
if hf:
|
| 28 |
+
# Download from Hugging Face Model Hub (not from Spaces)
|
| 29 |
+
model_path = hf_hub_download(
|
| 30 |
+
repo_id="vikenkd/catdog-model",
|
| 31 |
+
filename="best_cat_dog_classifier_model_20251019_122336.pth",
|
| 32 |
+
repo_type="model"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Load state_dict (both local & remote)
|
| 36 |
+
state_dict = torch.load(model_path, map_location=model_configs.device)
|
| 37 |
+
model.load_state_dict(state_dict)
|
| 38 |
+
model.eval()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
y_pred = model.predict(
|
| 42 |
model=model,
|
| 43 |
image_path=image_path
|