Spaces:
Sleeping
Sleeping
Tolani Akinola
commited on
Commit
Β·
25f61db
1
Parent(s):
7c8a784
Persist AI detection session state
Browse files- streamlit_app.py +115 -29
streamlit_app.py
CHANGED
|
@@ -339,11 +339,39 @@ def main():
|
|
| 339 |
|
| 340 |
st.markdown(ModernUIComponents.create_status_cards(status_data), unsafe_allow_html=True)
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
# Modern sign-out button
|
| 343 |
col1, col2, col3 = st.columns([1, 1, 1])
|
| 344 |
with col2:
|
| 345 |
if st.button("π Sign Out", use_container_width=True, type="secondary"):
|
| 346 |
end_shift(user_email, "manual")
|
|
|
|
| 347 |
st.session_state.clear()
|
| 348 |
st.success("β
Signed out successfully. See you next time!")
|
| 349 |
st.rerun()
|
|
@@ -410,11 +438,11 @@ def main():
|
|
| 410 |
), unsafe_allow_html=True)
|
| 411 |
|
| 412 |
col1, col2 = st.columns(2)
|
| 413 |
-
|
| 414 |
with col1:
|
| 415 |
st.subheader("π· Camera Capture")
|
| 416 |
cam = st.camera_input("Take a photo of the item", help="Position the item clearly in the frame")
|
| 417 |
-
|
| 418 |
with col2:
|
| 419 |
st.subheader("π File Upload")
|
| 420 |
up = st.file_uploader(
|
|
@@ -426,38 +454,96 @@ def main():
|
|
| 426 |
img_file = cam or up
|
| 427 |
if img_file:
|
| 428 |
try:
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
st.image(img, use_container_width=True, caption="Captured Image")
|
| 431 |
-
|
| 432 |
if st.button("π Identify Item with AI", use_container_width=True, type="primary"):
|
| 433 |
-
|
| 434 |
-
t0 = time.time()
|
| 435 |
-
try:
|
| 436 |
-
pre = preprocess_for_label(img)
|
| 437 |
-
raw = gemma_item_name(_to_png_bytes(pre))
|
| 438 |
-
processing_time = time.time() - t0
|
| 439 |
-
|
| 440 |
-
norm = normalize_item_name(raw)
|
| 441 |
-
|
| 442 |
-
if raw:
|
| 443 |
-
st.success(f"π€ **AI Detection:** {raw}")
|
| 444 |
-
st.info(f"β¨ **Normalized:** {norm or '(unknown)'} Β· β±οΈ {processing_time:.2f}s")
|
| 445 |
-
|
| 446 |
-
st.session_state["scanned_item_name"] = norm or raw or ""
|
| 447 |
-
st.session_state["last_activity_at"] = local_now()
|
| 448 |
-
|
| 449 |
-
log_event("item_identified", user_email, {
|
| 450 |
-
"raw_name": raw,
|
| 451 |
-
"normalized_name": norm,
|
| 452 |
-
"processing_time": processing_time
|
| 453 |
-
})
|
| 454 |
-
|
| 455 |
-
except Exception as e:
|
| 456 |
-
st.error(f"β AI identification failed: {e}")
|
| 457 |
-
log_event("ai_identification_failed", user_email, {"error": str(e)}, "error")
|
| 458 |
except Exception as e:
|
| 459 |
st.error(f"β Failed to process image: {e}")
|
|
|
|
| 460 |
log_event("image_processing_failed", user_email, {"error": str(e)}, "error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 463 |
|
|
|
|
| 339 |
|
| 340 |
st.markdown(ModernUIComponents.create_status_cards(status_data), unsafe_allow_html=True)
|
| 341 |
|
| 342 |
+
ai_state_defaults = {
|
| 343 |
+
"ai_image_bytes": None,
|
| 344 |
+
"ai_detection_pending": False,
|
| 345 |
+
"ai_detection_processing": False,
|
| 346 |
+
"ai_detection_result": None,
|
| 347 |
+
"ai_detection_error": None,
|
| 348 |
+
"ai_detection_duration": None,
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
def ensure_ai_detection_defaults() -> None:
|
| 352 |
+
for key, default in ai_state_defaults.items():
|
| 353 |
+
if key not in st.session_state:
|
| 354 |
+
st.session_state[key] = default
|
| 355 |
+
|
| 356 |
+
def clear_ai_detection_results() -> None:
|
| 357 |
+
st.session_state["ai_detection_pending"] = False
|
| 358 |
+
st.session_state["ai_detection_processing"] = False
|
| 359 |
+
st.session_state["ai_detection_result"] = None
|
| 360 |
+
st.session_state["ai_detection_error"] = None
|
| 361 |
+
st.session_state["ai_detection_duration"] = None
|
| 362 |
+
|
| 363 |
+
def reset_ai_detection_state() -> None:
|
| 364 |
+
st.session_state["ai_image_bytes"] = None
|
| 365 |
+
clear_ai_detection_results()
|
| 366 |
+
|
| 367 |
+
ensure_ai_detection_defaults()
|
| 368 |
+
|
| 369 |
# Modern sign-out button
|
| 370 |
col1, col2, col3 = st.columns([1, 1, 1])
|
| 371 |
with col2:
|
| 372 |
if st.button("π Sign Out", use_container_width=True, type="secondary"):
|
| 373 |
end_shift(user_email, "manual")
|
| 374 |
+
reset_ai_detection_state()
|
| 375 |
st.session_state.clear()
|
| 376 |
st.success("β
Signed out successfully. See you next time!")
|
| 377 |
st.rerun()
|
|
|
|
| 438 |
), unsafe_allow_html=True)
|
| 439 |
|
| 440 |
col1, col2 = st.columns(2)
|
| 441 |
+
|
| 442 |
with col1:
|
| 443 |
st.subheader("π· Camera Capture")
|
| 444 |
cam = st.camera_input("Take a photo of the item", help="Position the item clearly in the frame")
|
| 445 |
+
|
| 446 |
with col2:
|
| 447 |
st.subheader("π File Upload")
|
| 448 |
up = st.file_uploader(
|
|
|
|
| 454 |
img_file = cam or up
|
| 455 |
if img_file:
|
| 456 |
try:
|
| 457 |
+
img_bytes = img_file.getvalue()
|
| 458 |
+
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 459 |
+
png_bytes = _to_png_bytes(img)
|
| 460 |
+
|
| 461 |
+
if st.session_state["ai_image_bytes"] != png_bytes:
|
| 462 |
+
st.session_state["ai_image_bytes"] = png_bytes
|
| 463 |
+
clear_ai_detection_results()
|
| 464 |
+
|
| 465 |
st.image(img, use_container_width=True, caption="Captured Image")
|
| 466 |
+
|
| 467 |
if st.button("π Identify Item with AI", use_container_width=True, type="primary"):
|
| 468 |
+
st.session_state["ai_detection_pending"] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
except Exception as e:
|
| 470 |
st.error(f"β Failed to process image: {e}")
|
| 471 |
+
reset_ai_detection_state()
|
| 472 |
log_event("image_processing_failed", user_email, {"error": str(e)}, "error")
|
| 473 |
+
elif st.session_state.get("ai_image_bytes") is not None:
|
| 474 |
+
reset_ai_detection_state()
|
| 475 |
+
|
| 476 |
+
status_placeholder = st.empty()
|
| 477 |
+
if st.session_state.get("ai_detection_processing"):
|
| 478 |
+
with status_placeholder.spinner("Analyzing image with AI..."):
|
| 479 |
+
if (
|
| 480 |
+
st.session_state.get("ai_detection_result") is None
|
| 481 |
+
and st.session_state.get("ai_detection_error") is None
|
| 482 |
+
):
|
| 483 |
+
try:
|
| 484 |
+
image_bytes = st.session_state.get("ai_image_bytes")
|
| 485 |
+
if not image_bytes:
|
| 486 |
+
raise RuntimeError("No image data available for AI detection.")
|
| 487 |
+
|
| 488 |
+
t0 = time.time()
|
| 489 |
+
source_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 490 |
+
pre = preprocess_for_label(source_img)
|
| 491 |
+
raw = gemma_item_name(_to_png_bytes(pre))
|
| 492 |
+
processing_time = time.time() - t0
|
| 493 |
+
|
| 494 |
+
norm = normalize_item_name(raw)
|
| 495 |
+
|
| 496 |
+
st.session_state["ai_detection_result"] = {"raw": raw, "normalized": norm}
|
| 497 |
+
st.session_state["ai_detection_duration"] = processing_time
|
| 498 |
+
|
| 499 |
+
st.session_state["scanned_item_name"] = norm or raw or ""
|
| 500 |
+
st.session_state["last_activity_at"] = local_now()
|
| 501 |
+
|
| 502 |
+
log_event("item_identified", user_email, {
|
| 503 |
+
"raw_name": raw,
|
| 504 |
+
"normalized_name": norm,
|
| 505 |
+
"processing_time": processing_time
|
| 506 |
+
})
|
| 507 |
+
except Exception as e:
|
| 508 |
+
st.session_state["ai_detection_error"] = str(e)
|
| 509 |
+
st.session_state["ai_detection_duration"] = None
|
| 510 |
+
log_event("ai_identification_failed", user_email, {"error": str(e)}, "error")
|
| 511 |
+
finally:
|
| 512 |
+
st.session_state["ai_detection_processing"] = False
|
| 513 |
+
st.rerun()
|
| 514 |
+
else:
|
| 515 |
+
time.sleep(0.1)
|
| 516 |
+
else:
|
| 517 |
+
error_msg = st.session_state.get("ai_detection_error")
|
| 518 |
+
result = st.session_state.get("ai_detection_result") or {}
|
| 519 |
+
duration = st.session_state.get("ai_detection_duration")
|
| 520 |
+
|
| 521 |
+
if error_msg:
|
| 522 |
+
st.error(f"β AI identification failed: {error_msg}")
|
| 523 |
+
elif result:
|
| 524 |
+
raw_name = result.get("raw")
|
| 525 |
+
norm_name = result.get("normalized")
|
| 526 |
+
|
| 527 |
+
if raw_name:
|
| 528 |
+
st.success(f"π€ **AI Detection:** {raw_name}")
|
| 529 |
+
|
| 530 |
+
info_parts = []
|
| 531 |
+
info_parts.append(f"β¨ **Normalized:** {norm_name or '(unknown)'}")
|
| 532 |
+
if isinstance(duration, (int, float)):
|
| 533 |
+
info_parts.append(f"β±οΈ {duration:.2f}s")
|
| 534 |
+
st.info(" Β· ".join(info_parts))
|
| 535 |
+
|
| 536 |
+
if st.session_state.get("ai_detection_pending"):
|
| 537 |
+
if not st.session_state.get("ai_image_bytes"):
|
| 538 |
+
st.session_state["ai_detection_error"] = "Please capture or upload an image before running AI detection."
|
| 539 |
+
st.session_state["ai_detection_pending"] = False
|
| 540 |
+
elif not st.session_state.get("ai_detection_processing"):
|
| 541 |
+
st.session_state["ai_detection_processing"] = True
|
| 542 |
+
st.session_state["ai_detection_error"] = None
|
| 543 |
+
st.session_state["ai_detection_result"] = None
|
| 544 |
+
st.session_state["ai_detection_duration"] = None
|
| 545 |
+
st.session_state["ai_detection_pending"] = False
|
| 546 |
+
st.rerun()
|
| 547 |
|
| 548 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 549 |
|