mamba413 commited on
Commit
28c4ec8
·
verified ·
1 Parent(s): 97d8375

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +118 -52
src/app.py CHANGED
@@ -173,6 +173,7 @@ def load_model(from_pretrained, base_model, cache_dir, device):
173
  # Result Feedback Module Import
174
  # -----------------
175
  from feedback import FeedbackManager
 
176
 
177
  # Initialize Feedback Manager with HF dataset
178
  # make sure HF_TOKEN is set to visit private repository
@@ -183,6 +184,16 @@ feedback_manager = FeedbackManager(
183
  local_backup=False if os.environ.get('SPACE_ID') else True # 保留本地备份
184
  )
185
 
 
 
 
 
 
 
 
 
 
 
186
  # -----------------
187
  # Configuration
188
  # -----------------
@@ -226,6 +237,13 @@ if 'feedback_given' not in st.session_state:
226
  st.session_state.feedback_given = False
227
  # ========================================
228
 
 
 
 
 
 
 
 
229
  # -----------------
230
  # Streamlit Layout
231
  # -----------------
@@ -319,6 +337,12 @@ if detect_clicked:
319
  'elapsed_time': elapsed_time
320
  }
321
 
 
 
 
 
 
 
322
  st.info(
323
  f"""
324
  **Conclusion**:
@@ -362,58 +386,74 @@ if detect_clicked:
362
  """
363
  )
364
 
365
- # ========== 🆕 Feedback buttons (moved here for better UX) ==========
366
- st.markdown("**📝 Result Feedback**: Does this detection result meet your expectations?")
367
-
368
- current_text = text_input
369
- current_domain = selected_domain
370
- current_statistics = crit
371
- current_pvalue = p_value
372
- feedback_col1, feedback_col2 = st.columns(2)
373
-
374
- with feedback_col1:
375
- if st.button("✅ Expected", use_container_width=True, type="secondary", key=f"expected_btn_{hash(text_input[:50])}"):
376
- try:
377
- success, message = feedback_manager.save_feedback(
378
- current_text,
379
- current_domain,
380
- current_statistics,
381
- current_pvalue,
382
- 'expected'
383
- )
384
- if success:
385
- st.success("✅ Thank you for your feedback!")
386
- st.caption(f"💾 {message}")
387
- else:
388
- st.error(f"Failed to save feedback: {message}")
389
- except Exception as e:
390
- st.error(f"Failed to save feedback: {str(e)}")
391
- import traceback
392
- st.code(traceback.format_exc())
393
-
394
- with feedback_col2:
395
- if st.button("❌ Unexpected", use_container_width=True, type="secondary", key=f"unexpected_btn_{hash(text_input[:50])}"):
396
- try:
397
- success, message = feedback_manager.save_feedback(
398
- current_text,
399
- current_domain,
400
- current_statistics,
401
- current_pvalue,
402
- 'unexpected'
403
- )
404
- if success:
405
- st.warning("❌ Feedback recorded! This will help us improve.")
406
- st.caption(f"💾 {message}")
407
- else:
408
- st.error(f"Failed to save feedback: {message}")
409
- except Exception as e:
410
- st.error(f"Failed to save feedback: {str(e)}")
411
- import traceback
412
- st.code(traceback.format_exc())
413
-
414
- if st.session_state.feedback_given:
415
- st.success("✅ Feedback submitted successfully!")
416
- # ============================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
  # Show detailed results
419
  with result_placeholder:
@@ -442,6 +482,32 @@ if detect_clicked:
442
  # language="bibtex"
443
  # )
444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445
  # -----------------
446
  # Footer
447
  # -----------------
 
173
  # Result Feedback Module Import
174
  # -----------------
175
  from feedback import FeedbackManager
176
+ from stats import StatsManager
177
 
178
  # Initialize Feedback Manager with HF dataset
179
  # make sure HF_TOKEN is set to visit private repository
 
184
  local_backup=False if os.environ.get('SPACE_ID') else True # 保留本地备份
185
  )
186
 
187
+ @st.cache_resource
188
+ def get_stats_manager():
189
+ return StatsManager(
190
+ dataset_repo_id=FEEDBACK_DATASET_ID,
191
+ hf_token=os.environ.get('HF_TOKEN'),
192
+ local_backup=False if os.environ.get('SPACE_ID') else True,
193
+ )
194
+
195
+ stats_manager = get_stats_manager()
196
+
197
  # -----------------
198
  # Configuration
199
  # -----------------
 
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:
243
+ st.session_state.visit_counted = True
244
+ stats_manager.increment_visit()
245
+ # -------------------------
246
+
247
  # -----------------
248
  # Streamlit Layout
249
  # -----------------
 
337
  'elapsed_time': elapsed_time
338
  }
339
 
340
+ # Count detection once per unique detect action
341
+ _det_key = f'det_counted_{hash(text_input[:80])}'
342
+ if _det_key not in st.session_state:
343
+ st.session_state[_det_key] = True
344
+ stats_manager.increment_detection()
345
+
346
  st.info(
347
  f"""
348
  **Conclusion**:
 
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:
 
482
  # language="bibtex"
483
  # )
484
 
485
+ # -----------------
486
+ # Statistics Chip (fixed top-right)
487
+ # -----------------
488
+ st.markdown(
489
+ f"""
490
+ <style>
491
+ .stats-chip {{
492
+ position: fixed;
493
+ top: 3.6rem;
494
+ right: 1rem;
495
+ display: flex;
496
+ align-items: center;
497
+ gap: 0.35rem;
498
+ font-size: 0.78rem;
499
+ color: #9ca3af;
500
+ z-index: 999;
501
+ pointer-events: none;
502
+ }}
503
+ </style>
504
+ <div class="stats-chip">
505
+ <span>{stats_manager.visit_count:,} visits</span>
506
+ </div>
507
+ """,
508
+ unsafe_allow_html=True
509
+ )
510
+
511
  # -----------------
512
  # Footer
513
  # -----------------