Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,15 @@ import os
|
|
| 2 |
import importlib.util
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import gradio as gr
|
| 5 |
-
import torch
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import torchvision.transforms as transforms
|
| 8 |
|
| 9 |
# --- CONFIG ---
|
| 10 |
PRIVATE_DATASET_ID = "abdulrafay9/containeralign-private"
|
| 11 |
-
TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
|
| 13 |
if not TOKEN:
|
| 14 |
raise RuntimeError("HF_TOKEN is not set. Add it in Settings → Variables and secrets → Secrets.")
|
| 15 |
|
| 16 |
-
# --- DOWNLOAD
|
| 17 |
core_path = hf_hub_download(
|
| 18 |
repo_id=PRIVATE_DATASET_ID,
|
| 19 |
repo_type="dataset",
|
|
@@ -27,7 +24,7 @@ weights_path = hf_hub_download(
|
|
| 27 |
token=TOKEN,
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# --- LOAD
|
| 31 |
spec = importlib.util.spec_from_file_location("app_core", core_path)
|
| 32 |
app_core = importlib.util.module_from_spec(spec)
|
| 33 |
spec.loader.exec_module(app_core)
|
|
@@ -35,28 +32,19 @@ spec.loader.exec_module(app_core)
|
|
| 35 |
# --- LOAD MODEL ---
|
| 36 |
model = app_core.load_model(weights_path)
|
| 37 |
|
| 38 |
-
# ---
|
| 39 |
-
transform = transforms.Compose([
|
| 40 |
-
transforms.Resize((128, 128)),
|
| 41 |
-
transforms.ToTensor(),
|
| 42 |
-
transforms.Normalize((0.5,), (0.5,))
|
| 43 |
-
])
|
| 44 |
-
|
| 45 |
-
# --- PREDICT FUNCTION ---
|
| 46 |
def predict(image):
|
| 47 |
-
|
| 48 |
-
tensor = transform(img).unsqueeze(0)
|
| 49 |
-
result = app_core.predict_alignment(model, tensor)
|
| 50 |
-
return f"Prediction: {result}"
|
| 51 |
|
| 52 |
-
# --- GRADIO
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=predict,
|
| 55 |
-
inputs=gr.Image(type="
|
| 56 |
-
outputs="
|
| 57 |
title="Container Alignment Detection",
|
| 58 |
-
description="Upload an image to check whether containers are
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|
|
|
|
| 2 |
import importlib.util
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# --- CONFIG ---
|
| 7 |
PRIVATE_DATASET_ID = "abdulrafay9/containeralign-private"
|
| 8 |
+
TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
if not TOKEN:
|
| 11 |
raise RuntimeError("HF_TOKEN is not set. Add it in Settings → Variables and secrets → Secrets.")
|
| 12 |
|
| 13 |
+
# --- DOWNLOAD FILES ---
|
| 14 |
core_path = hf_hub_download(
|
| 15 |
repo_id=PRIVATE_DATASET_ID,
|
| 16 |
repo_type="dataset",
|
|
|
|
| 24 |
token=TOKEN,
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# --- LOAD MODULE ---
|
| 28 |
spec = importlib.util.spec_from_file_location("app_core", core_path)
|
| 29 |
app_core = importlib.util.module_from_spec(spec)
|
| 30 |
spec.loader.exec_module(app_core)
|
|
|
|
| 32 |
# --- LOAD MODEL ---
|
| 33 |
model = app_core.load_model(weights_path)
|
| 34 |
|
| 35 |
+
# --- DEFINE PREDICTION FUNCTION ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def predict(image):
|
| 37 |
+
return app_core.predict_alignment(model, image)
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# --- GRADIO INTERFACE ---
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=predict,
|
| 42 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 43 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 44 |
title="Container Alignment Detection",
|
| 45 |
+
description="Upload an image to check whether containers are Aligned or Not Aligned."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# --- RUN APP ---
|
| 49 |
if __name__ == "__main__":
|
| 50 |
demo.launch()
|