Spaces:
Running
Running
Jin Zhu commited on
Commit ·
b6bd417
1
Parent(s): 97d8375
update code
Browse files- src/app.py +118 -52
- src/feedback.py +19 -16
- src/stats.py +107 -0
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 |
-
# ==========
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 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 |
# -----------------
|
src/feedback.py
CHANGED
|
@@ -170,18 +170,21 @@ Each entry contains:
|
|
| 170 |
success = False
|
| 171 |
messages = []
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
if self.local_backup
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
messages.append(f"💾 Local backup saved")
|
| 179 |
success = True
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# 上传到 HF 数据集
|
| 184 |
-
if self.api and self.dataset_repo_id:
|
| 185 |
try:
|
| 186 |
# 保存为 JSON 文件
|
| 187 |
upload_file(
|
|
@@ -192,12 +195,12 @@ Each entry contains:
|
|
| 192 |
token=self.hf_token,
|
| 193 |
commit_message=f"Add feedback: {feedback_type} at {feedback_entry['timestamp']}"
|
| 194 |
)
|
| 195 |
-
|
| 196 |
# 同时创建/更新 CSV 版本(方便查看)
|
| 197 |
df = pd.DataFrame(feedback_data)
|
| 198 |
csv_file = self.local_dir / 'feedback_data.csv'
|
| 199 |
df.to_csv(csv_file, index=False)
|
| 200 |
-
|
| 201 |
upload_file(
|
| 202 |
path_or_fileobj=str(csv_file),
|
| 203 |
path_in_repo="feedback_data.csv",
|
|
@@ -206,14 +209,14 @@ Each entry contains:
|
|
| 206 |
token=self.hf_token,
|
| 207 |
commit_message=f"Update CSV: {len(feedback_data)} total entries"
|
| 208 |
)
|
| 209 |
-
|
| 210 |
messages.append(f"☁️ Uploaded to HF dataset: {self.dataset_repo_id}")
|
| 211 |
success = True
|
| 212 |
-
|
| 213 |
except Exception as e:
|
| 214 |
messages.append(f"⚠️ HF upload failed: {e}")
|
| 215 |
-
# 如果 HF 上传失败但本地
|
| 216 |
-
success = success or
|
| 217 |
|
| 218 |
return success, " | ".join(messages)
|
| 219 |
|
|
|
|
| 170 |
success = False
|
| 171 |
messages = []
|
| 172 |
|
| 173 |
+
# Always write local file first — required for HF upload regardless of local_backup setting.
|
| 174 |
+
# (Bug fix: previously only written inside `if self.local_backup`, causing empty uploads to HF)
|
| 175 |
+
local_write_ok = False
|
| 176 |
+
try:
|
| 177 |
+
with open(self.local_file, 'w', encoding='utf-8') as f:
|
| 178 |
+
json.dump(feedback_data, f, ensure_ascii=False, indent=2)
|
| 179 |
+
local_write_ok = True
|
| 180 |
+
if self.local_backup:
|
| 181 |
messages.append(f"💾 Local backup saved")
|
| 182 |
success = True
|
| 183 |
+
except Exception as e:
|
| 184 |
+
messages.append(f"❌ Local save failed: {e}")
|
| 185 |
+
|
| 186 |
+
# 上传到 HF 数据集(仅当本地写入成功时)
|
| 187 |
+
if self.api and self.dataset_repo_id and local_write_ok:
|
| 188 |
try:
|
| 189 |
# 保存为 JSON 文件
|
| 190 |
upload_file(
|
|
|
|
| 195 |
token=self.hf_token,
|
| 196 |
commit_message=f"Add feedback: {feedback_type} at {feedback_entry['timestamp']}"
|
| 197 |
)
|
| 198 |
+
|
| 199 |
# 同时创建/更新 CSV 版本(方便查看)
|
| 200 |
df = pd.DataFrame(feedback_data)
|
| 201 |
csv_file = self.local_dir / 'feedback_data.csv'
|
| 202 |
df.to_csv(csv_file, index=False)
|
| 203 |
+
|
| 204 |
upload_file(
|
| 205 |
path_or_fileobj=str(csv_file),
|
| 206 |
path_in_repo="feedback_data.csv",
|
|
|
|
| 209 |
token=self.hf_token,
|
| 210 |
commit_message=f"Update CSV: {len(feedback_data)} total entries"
|
| 211 |
)
|
| 212 |
+
|
| 213 |
messages.append(f"☁️ Uploaded to HF dataset: {self.dataset_repo_id}")
|
| 214 |
success = True
|
| 215 |
+
|
| 216 |
except Exception as e:
|
| 217 |
messages.append(f"⚠️ HF upload failed: {e}")
|
| 218 |
+
# 如果 HF 上传失败但本地写入成功,仍然返回成功
|
| 219 |
+
success = success or local_write_ok
|
| 220 |
|
| 221 |
return success, " | ".join(messages)
|
| 222 |
|
src/stats.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import threading
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from huggingface_hub import HfApi, upload_file, hf_hub_download
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class StatsManager:
|
| 11 |
+
STATS_FILENAME = "stats.json"
|
| 12 |
+
WRITE_INTERVAL_SECONDS = 60
|
| 13 |
+
|
| 14 |
+
def __init__(self, dataset_repo_id: str, hf_token: Optional[str] = None, local_backup: bool = True):
|
| 15 |
+
self.dataset_repo_id = dataset_repo_id
|
| 16 |
+
self.hf_token = hf_token or os.environ.get("HF_TOKEN")
|
| 17 |
+
self.local_backup = local_backup
|
| 18 |
+
self._lock = threading.Lock()
|
| 19 |
+
|
| 20 |
+
self.api = HfApi(token=self.hf_token) if (self.dataset_repo_id and self.hf_token) else None
|
| 21 |
+
|
| 22 |
+
if os.environ.get("SPACE_ID"):
|
| 23 |
+
self.local_dir = Path("/tmp/feedback_data")
|
| 24 |
+
else:
|
| 25 |
+
self.local_dir = Path(__file__).parent / "feedback_data"
|
| 26 |
+
self.local_dir.mkdir(exist_ok=True, parents=True)
|
| 27 |
+
self.local_file = self.local_dir / self.STATS_FILENAME
|
| 28 |
+
|
| 29 |
+
self._stats = self._load_stats()
|
| 30 |
+
self._last_hf_write = datetime.min
|
| 31 |
+
|
| 32 |
+
def _load_stats(self) -> dict:
|
| 33 |
+
if self.api and self.dataset_repo_id:
|
| 34 |
+
try:
|
| 35 |
+
local_path = hf_hub_download(
|
| 36 |
+
repo_id=self.dataset_repo_id,
|
| 37 |
+
filename=self.STATS_FILENAME,
|
| 38 |
+
repo_type="dataset",
|
| 39 |
+
token=self.hf_token,
|
| 40 |
+
force_download=True,
|
| 41 |
+
cache_dir=str(self.local_dir)
|
| 42 |
+
)
|
| 43 |
+
with open(local_path, "r", encoding="utf-8") as f:
|
| 44 |
+
return json.load(f)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
if "404" not in str(e):
|
| 47 |
+
print(f"[StatsManager] HF load warning: {e}")
|
| 48 |
+
|
| 49 |
+
if self.local_file.exists():
|
| 50 |
+
try:
|
| 51 |
+
with open(self.local_file, "r", encoding="utf-8") as f:
|
| 52 |
+
return json.load(f)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"[StatsManager] Local load error: {e}")
|
| 55 |
+
|
| 56 |
+
now = datetime.now().isoformat()
|
| 57 |
+
return {"visit_count": 0, "detection_count": 0, "last_updated": now, "created_at": now}
|
| 58 |
+
|
| 59 |
+
def _write_to_local(self):
|
| 60 |
+
try:
|
| 61 |
+
with open(self.local_file, "w", encoding="utf-8") as f:
|
| 62 |
+
json.dump(self._stats, f, indent=2)
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"[StatsManager] Local write error: {e}")
|
| 65 |
+
|
| 66 |
+
def _upload_to_hf(self):
|
| 67 |
+
if not (self.api and self.dataset_repo_id):
|
| 68 |
+
return
|
| 69 |
+
try:
|
| 70 |
+
upload_file(
|
| 71 |
+
path_or_fileobj=str(self.local_file),
|
| 72 |
+
path_in_repo=self.STATS_FILENAME,
|
| 73 |
+
repo_id=self.dataset_repo_id,
|
| 74 |
+
repo_type="dataset",
|
| 75 |
+
token=self.hf_token,
|
| 76 |
+
commit_message="stats: visits={} detections={}".format(
|
| 77 |
+
self._stats["visit_count"], self._stats["detection_count"]
|
| 78 |
+
)
|
| 79 |
+
)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"[StatsManager] HF upload error (non-fatal): {e}")
|
| 82 |
+
|
| 83 |
+
def _maybe_flush(self):
|
| 84 |
+
self._stats["last_updated"] = datetime.now().isoformat()
|
| 85 |
+
self._write_to_local()
|
| 86 |
+
elapsed = (datetime.now() - self._last_hf_write).total_seconds()
|
| 87 |
+
if elapsed >= self.WRITE_INTERVAL_SECONDS:
|
| 88 |
+
self._upload_to_hf()
|
| 89 |
+
self._last_hf_write = datetime.now()
|
| 90 |
+
|
| 91 |
+
def increment_visit(self):
|
| 92 |
+
with self._lock:
|
| 93 |
+
self._stats["visit_count"] += 1
|
| 94 |
+
self._maybe_flush()
|
| 95 |
+
|
| 96 |
+
def increment_detection(self):
|
| 97 |
+
with self._lock:
|
| 98 |
+
self._stats["detection_count"] += 1
|
| 99 |
+
self._maybe_flush()
|
| 100 |
+
|
| 101 |
+
@property
|
| 102 |
+
def visit_count(self) -> int:
|
| 103 |
+
return self._stats.get("visit_count", 0)
|
| 104 |
+
|
| 105 |
+
@property
|
| 106 |
+
def detection_count(self) -> int:
|
| 107 |
+
return self._stats.get("detection_count", 0)
|