Commit Β·
6d901f5
1
Parent(s): e07d0dc
Move @spaces.GPU functions before Gradio Blocks for HF detection
Browse files
app.py
CHANGED
|
@@ -150,7 +150,43 @@ def api_download_requirements():
|
|
| 150 |
|
| 151 |
|
| 152 |
# ---------------------------------------------------------------------------
|
| 153 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
# ---------------------------------------------------------------------------
|
| 155 |
|
| 156 |
@spaces.GPU
|
|
@@ -166,7 +202,6 @@ def run_gradio_detection(
|
|
| 166 |
if input_image is None:
|
| 167 |
return None, "Please upload or capture an image to perform detection."
|
| 168 |
|
| 169 |
-
# Convert PIL Image or NumPy to OpenCV BGR array
|
| 170 |
if isinstance(input_image, Image.Image):
|
| 171 |
img_np = np.array(input_image)
|
| 172 |
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
|
@@ -186,8 +221,7 @@ def run_gradio_detection(
|
|
| 186 |
)
|
| 187 |
|
| 188 |
annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
|
| 189 |
-
|
| 190 |
-
# Log detection summary to DB
|
| 191 |
for det in detections:
|
| 192 |
db.log_detection(
|
| 193 |
object_name=det["label"],
|
|
@@ -208,7 +242,6 @@ def teach_new_object(name, category, description, reference_images):
|
|
| 208 |
return "β Error: Please upload at least one reference photo.", get_objects_table()
|
| 209 |
|
| 210 |
try:
|
| 211 |
-
# Check if exists or create
|
| 212 |
existing = db.get_object_by_name(name.strip())
|
| 213 |
if existing:
|
| 214 |
obj = existing
|
|
@@ -236,38 +269,6 @@ def teach_new_object(name, category, description, reference_images):
|
|
| 236 |
return f"β Error teaching object: {str(e)}", get_objects_table()
|
| 237 |
|
| 238 |
|
| 239 |
-
def get_objects_table():
|
| 240 |
-
objs = db.get_all_objects()
|
| 241 |
-
data = []
|
| 242 |
-
for o in objs:
|
| 243 |
-
data.append([o.id, o.name, o.category, o.image_count, o.embedding_count, o.created_at])
|
| 244 |
-
return data
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
def delete_selected_object(object_id):
|
| 248 |
-
if not object_id or not object_id.strip():
|
| 249 |
-
return "β Please enter a valid Object ID to delete.", get_objects_table()
|
| 250 |
-
success = db.delete_object(object_id.strip())
|
| 251 |
-
if success:
|
| 252 |
-
return f"β
Deleted object '{object_id}'.", get_objects_table()
|
| 253 |
-
return f"β Object '{object_id}' not found.", get_objects_table()
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
def load_doc_file(doc_name):
|
| 257 |
-
doc_path = os.path.join("docs", f"{doc_name}.md")
|
| 258 |
-
if os.path.exists(doc_path):
|
| 259 |
-
with open(doc_path, "r", encoding="utf-8") as f:
|
| 260 |
-
return f.read()
|
| 261 |
-
return f"Documentation file '{doc_name}' not found."
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
def export_db_handler():
|
| 265 |
-
temp_dir = tempfile.mkdtemp()
|
| 266 |
-
export_path = os.path.join(temp_dir, "object_library_export.zip")
|
| 267 |
-
db.export_database_zip(export_path)
|
| 268 |
-
return export_path
|
| 269 |
-
|
| 270 |
-
|
| 271 |
# ---------------------------------------------------------------------------
|
| 272 |
# Building Modern Glassmorphic Gradio UI
|
| 273 |
# ---------------------------------------------------------------------------
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
# ---------------------------------------------------------------------------
|
| 153 |
+
# Helper functions (non-GPU)
|
| 154 |
+
# ---------------------------------------------------------------------------
|
| 155 |
+
|
| 156 |
+
def get_objects_table():
|
| 157 |
+
objs = db.get_all_objects()
|
| 158 |
+
data = []
|
| 159 |
+
for o in objs:
|
| 160 |
+
data.append([o.id, o.name, o.category, o.image_count, o.embedding_count, o.created_at])
|
| 161 |
+
return data
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def delete_selected_object(object_id):
|
| 165 |
+
if not object_id or not object_id.strip():
|
| 166 |
+
return "β Please enter a valid Object ID to delete.", get_objects_table()
|
| 167 |
+
success = db.delete_object(object_id.strip())
|
| 168 |
+
if success:
|
| 169 |
+
return f"β
Deleted object '{object_id}'.", get_objects_table()
|
| 170 |
+
return f"β Object '{object_id}' not found.", get_objects_table()
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def load_doc_file(doc_name):
|
| 174 |
+
doc_path = os.path.join("docs", f"{doc_name}.md")
|
| 175 |
+
if os.path.exists(doc_path):
|
| 176 |
+
with open(doc_path, "r", encoding="utf-8") as f:
|
| 177 |
+
return f.read()
|
| 178 |
+
return f"Documentation file '{doc_name}' not found."
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def export_db_handler():
|
| 182 |
+
temp_dir = tempfile.mkdtemp()
|
| 183 |
+
export_path = os.path.join(temp_dir, "object_library_export.zip")
|
| 184 |
+
db.export_database_zip(export_path)
|
| 185 |
+
return export_path
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# ---------------------------------------------------------------------------
|
| 189 |
+
# GPU Functions β defined BEFORE Gradio Blocks for HF ZeroGPU detection
|
| 190 |
# ---------------------------------------------------------------------------
|
| 191 |
|
| 192 |
@spaces.GPU
|
|
|
|
| 202 |
if input_image is None:
|
| 203 |
return None, "Please upload or capture an image to perform detection."
|
| 204 |
|
|
|
|
| 205 |
if isinstance(input_image, Image.Image):
|
| 206 |
img_np = np.array(input_image)
|
| 207 |
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
|
| 224 |
+
|
|
|
|
| 225 |
for det in detections:
|
| 226 |
db.log_detection(
|
| 227 |
object_name=det["label"],
|
|
|
|
| 242 |
return "β Error: Please upload at least one reference photo.", get_objects_table()
|
| 243 |
|
| 244 |
try:
|
|
|
|
| 245 |
existing = db.get_object_by_name(name.strip())
|
| 246 |
if existing:
|
| 247 |
obj = existing
|
|
|
|
| 269 |
return f"β Error teaching object: {str(e)}", get_objects_table()
|
| 270 |
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
# ---------------------------------------------------------------------------
|
| 273 |
# Building Modern Glassmorphic Gradio UI
|
| 274 |
# ---------------------------------------------------------------------------
|