Spaces:
Running
Running
Update src/app.py
Browse files- src/app.py +73 -68
src/app.py
CHANGED
|
@@ -235,8 +235,16 @@ if 'last_detection' not in st.session_state:
|
|
| 235 |
st.session_state.last_detection = None
|
| 236 |
if 'feedback_given' not in st.session_state:
|
| 237 |
st.session_state.feedback_given = False
|
|
|
|
|
|
|
| 238 |
# ========================================
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
# ----- Visit Counter -----
|
| 241 |
# session_state resets on F5 / new tab, so this runs exactly once per browser session
|
| 242 |
if 'visit_counted' not in st.session_state:
|
|
@@ -386,74 +394,6 @@ if detect_clicked:
|
|
| 386 |
"""
|
| 387 |
)
|
| 388 |
|
| 389 |
-
# ========== Feedback UI ==========
|
| 390 |
-
_fb_key = f'feedback_given_{hash(text_input[:50])}'
|
| 391 |
-
if _fb_key not in st.session_state:
|
| 392 |
-
st.session_state[_fb_key] = False
|
| 393 |
-
|
| 394 |
-
st.markdown(
|
| 395 |
-
"""
|
| 396 |
-
<style>
|
| 397 |
-
.fb-header { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.3rem; }
|
| 398 |
-
.privacy-tip {
|
| 399 |
-
position: relative; display: inline-block;
|
| 400 |
-
cursor: help; color: #9ca3af; font-size: 0.9rem;
|
| 401 |
-
}
|
| 402 |
-
.privacy-tip .tip-text {
|
| 403 |
-
visibility: hidden; opacity: 0;
|
| 404 |
-
width: 240px; background-color: #374151; color: #f9fafb;
|
| 405 |
-
text-align: left; border-radius: 6px;
|
| 406 |
-
padding: 0.5rem 0.7rem; font-size: 0.78rem; line-height: 1.4;
|
| 407 |
-
position: absolute; z-index: 100;
|
| 408 |
-
bottom: 130%; left: 50%; transform: translateX(-50%);
|
| 409 |
-
transition: opacity 0.25s ease; pointer-events: none;
|
| 410 |
-
}
|
| 411 |
-
.privacy-tip:hover .tip-text { visibility: visible; opacity: 1; }
|
| 412 |
-
</style>
|
| 413 |
-
<div class="fb-header">
|
| 414 |
-
<strong>📝 Result Feedback</strong>: Does this detection result meet your expectations?
|
| 415 |
-
<span class="privacy-tip">🔒
|
| 416 |
-
<span class="tip-text">🔒 Your feedback is stored privately and will never be shared with third parties. It is used solely to improve detection accuracy.</span>
|
| 417 |
-
</span>
|
| 418 |
-
</div>
|
| 419 |
-
""",
|
| 420 |
-
unsafe_allow_html=True
|
| 421 |
-
)
|
| 422 |
-
|
| 423 |
-
if not st.session_state[_fb_key]:
|
| 424 |
-
feedback_col1, feedback_col2 = st.columns(2)
|
| 425 |
-
with feedback_col1:
|
| 426 |
-
if st.button("✅ Expected", use_container_width=True, type="secondary",
|
| 427 |
-
key=f"expected_btn_{hash(text_input[:50])}"):
|
| 428 |
-
try:
|
| 429 |
-
fb_success, fb_message = feedback_manager.save_feedback(
|
| 430 |
-
text_input, selected_domain, crit, p_value, 'expected'
|
| 431 |
-
)
|
| 432 |
-
if fb_success:
|
| 433 |
-
st.session_state[_fb_key] = True
|
| 434 |
-
st.toast("Thank you for your feedback!", icon="✅")
|
| 435 |
-
else:
|
| 436 |
-
st.error(f"Failed to save feedback: {fb_message}")
|
| 437 |
-
except Exception as e:
|
| 438 |
-
st.error(f"Failed to save feedback: {str(e)}")
|
| 439 |
-
|
| 440 |
-
with feedback_col2:
|
| 441 |
-
if st.button("❌ Unexpected", use_container_width=True, type="secondary",
|
| 442 |
-
key=f"unexpected_btn_{hash(text_input[:50])}"):
|
| 443 |
-
try:
|
| 444 |
-
fb_success, fb_message = feedback_manager.save_feedback(
|
| 445 |
-
text_input, selected_domain, crit, p_value, 'unexpected'
|
| 446 |
-
)
|
| 447 |
-
if fb_success:
|
| 448 |
-
st.session_state[_fb_key] = True
|
| 449 |
-
st.toast("Feedback recorded! This will help us improve.", icon="📝")
|
| 450 |
-
else:
|
| 451 |
-
st.error(f"Failed to save feedback: {fb_message}")
|
| 452 |
-
except Exception as e:
|
| 453 |
-
st.error(f"Failed to save feedback: {str(e)}")
|
| 454 |
-
else:
|
| 455 |
-
st.caption("✅ Feedback submitted — thank you!")
|
| 456 |
-
# =============================================
|
| 457 |
|
| 458 |
# Show detailed results
|
| 459 |
with result_placeholder:
|
|
@@ -464,6 +404,71 @@ if detect_clicked:
|
|
| 464 |
st.error(f"❌ Error during detection: {str(e)}")
|
| 465 |
st.exception(e)
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
# with st.expander("📋 Citation"):
|
| 468 |
# st.markdown(
|
| 469 |
# """
|
|
|
|
| 235 |
st.session_state.last_detection = None
|
| 236 |
if 'feedback_given' not in st.session_state:
|
| 237 |
st.session_state.feedback_given = False
|
| 238 |
+
if 'pending_toast' not in st.session_state:
|
| 239 |
+
st.session_state.pending_toast = None
|
| 240 |
# ========================================
|
| 241 |
|
| 242 |
+
# Show any pending toast (set by feedback buttons before st.rerun())
|
| 243 |
+
if st.session_state.pending_toast:
|
| 244 |
+
_msg, _icon = st.session_state.pending_toast
|
| 245 |
+
st.toast(_msg, icon=_icon)
|
| 246 |
+
st.session_state.pending_toast = None
|
| 247 |
+
|
| 248 |
# ----- Visit Counter -----
|
| 249 |
# session_state resets on F5 / new tab, so this runs exactly once per browser session
|
| 250 |
if 'visit_counted' not in st.session_state:
|
|
|
|
| 394 |
"""
|
| 395 |
)
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
# Show detailed results
|
| 399 |
with result_placeholder:
|
|
|
|
| 404 |
st.error(f"❌ Error during detection: {str(e)}")
|
| 405 |
st.exception(e)
|
| 406 |
|
| 407 |
+
# -----------------
|
| 408 |
+
# Feedback UI (outside if detect_clicked — persists across all reruns via session_state)
|
| 409 |
+
# -----------------
|
| 410 |
+
if st.session_state.last_detection is not None and not st.session_state.feedback_given:
|
| 411 |
+
_ld = st.session_state.last_detection
|
| 412 |
+
st.markdown(
|
| 413 |
+
"""
|
| 414 |
+
<style>
|
| 415 |
+
.fb-header { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.3rem; }
|
| 416 |
+
.privacy-tip {
|
| 417 |
+
position: relative; display: inline-block;
|
| 418 |
+
cursor: help; color: #9ca3af; font-size: 0.9rem;
|
| 419 |
+
}
|
| 420 |
+
.privacy-tip .tip-text {
|
| 421 |
+
visibility: hidden; opacity: 0;
|
| 422 |
+
width: 240px; background-color: #374151; color: #f9fafb;
|
| 423 |
+
text-align: left; border-radius: 6px;
|
| 424 |
+
padding: 0.5rem 0.7rem; font-size: 0.78rem; line-height: 1.4;
|
| 425 |
+
position: absolute; z-index: 100;
|
| 426 |
+
bottom: 130%; left: 50%; transform: translateX(-50%);
|
| 427 |
+
transition: opacity 0.25s ease; pointer-events: none;
|
| 428 |
+
}
|
| 429 |
+
.privacy-tip:hover .tip-text { visibility: visible; opacity: 1; }
|
| 430 |
+
</style>
|
| 431 |
+
<div class="fb-header">
|
| 432 |
+
<strong>📝 Result Feedback</strong>: Does this detection result meet your expectations?
|
| 433 |
+
<span class="privacy-tip">🔒
|
| 434 |
+
<span class="tip-text">🔒 Your feedback is stored privately and will never be shared with third parties. It is used solely to improve detection accuracy.</span>
|
| 435 |
+
</span>
|
| 436 |
+
</div>
|
| 437 |
+
""",
|
| 438 |
+
unsafe_allow_html=True
|
| 439 |
+
)
|
| 440 |
+
feedback_col1, feedback_col2 = st.columns(2)
|
| 441 |
+
with feedback_col1:
|
| 442 |
+
if st.button("✅ Expected", use_container_width=True, type="secondary",
|
| 443 |
+
key="expected_btn"):
|
| 444 |
+
try:
|
| 445 |
+
fb_success, fb_message = feedback_manager.save_feedback(
|
| 446 |
+
_ld['text'], _ld['domain'], _ld['statistics'], _ld['p_value'], 'expected'
|
| 447 |
+
)
|
| 448 |
+
if fb_success:
|
| 449 |
+
st.session_state.feedback_given = True
|
| 450 |
+
st.session_state.pending_toast = ("Thank you for your feedback!", "✅")
|
| 451 |
+
st.rerun()
|
| 452 |
+
else:
|
| 453 |
+
st.error(f"Failed to save feedback: {fb_message}")
|
| 454 |
+
except Exception as e:
|
| 455 |
+
st.error(f"Failed to save feedback: {str(e)}")
|
| 456 |
+
with feedback_col2:
|
| 457 |
+
if st.button("❌ Unexpected", use_container_width=True, type="secondary",
|
| 458 |
+
key="unexpected_btn"):
|
| 459 |
+
try:
|
| 460 |
+
fb_success, fb_message = feedback_manager.save_feedback(
|
| 461 |
+
_ld['text'], _ld['domain'], _ld['statistics'], _ld['p_value'], 'unexpected'
|
| 462 |
+
)
|
| 463 |
+
if fb_success:
|
| 464 |
+
st.session_state.feedback_given = True
|
| 465 |
+
st.session_state.pending_toast = ("Feedback recorded! This will help us improve.", "📝")
|
| 466 |
+
st.rerun()
|
| 467 |
+
else:
|
| 468 |
+
st.error(f"Failed to save feedback: {fb_message}")
|
| 469 |
+
except Exception as e:
|
| 470 |
+
st.error(f"Failed to save feedback: {str(e)}")
|
| 471 |
+
|
| 472 |
# with st.expander("📋 Citation"):
|
| 473 |
# st.markdown(
|
| 474 |
# """
|