phoebehxf commited on
Commit
c2c0e92
·
1 Parent(s): ad05714

add feedback storage

Browse files
Files changed (1) hide show
  1. app.py +81 -4
app.py CHANGED
@@ -15,12 +15,17 @@ from skimage import measure
15
  from matplotlib import cm
16
  from glob import glob
17
  from natsort import natsorted
 
18
 
19
  # ===== 导入三个推理模块 =====
20
  from inference_seg import load_model as load_seg_model, run as run_seg
21
  from inference_count import load_model as load_count_model, run as run_count
22
  from inference_track import load_model as load_track_model, run as run_track
23
 
 
 
 
 
24
  # ===== 清理缓存目录 =====
25
  print("===== clearing cache =====")
26
  cache_path = os.path.expanduser("~/.cache/")
@@ -74,6 +79,54 @@ load_all_models()
74
  DATASET_DIR = Path("solver_cache")
75
  DATASET_DIR.mkdir(parents=True, exist_ok=True)
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def save_feedback(query_id, feedback_type, feedback_text=None, img_path=None, bboxes=None):
78
  """保存用户反馈到JSON文件"""
79
  feedback_data = {
@@ -999,7 +1052,15 @@ with gr.Blocks(
999
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1000
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1001
 
1002
- save_feedback(
 
 
 
 
 
 
 
 
1003
  query_id=query_id,
1004
  feedback_type=f"score_{int(score)}",
1005
  feedback_text=comment,
@@ -1173,7 +1234,15 @@ with gr.Blocks(
1173
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1174
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1175
 
1176
- save_feedback(
 
 
 
 
 
 
 
 
1177
  query_id=query_id,
1178
  feedback_type=f"score_{int(score)}",
1179
  feedback_text=comment,
@@ -1487,7 +1556,15 @@ with gr.Blocks(
1487
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1488
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1489
 
1490
- save_feedback(
 
 
 
 
 
 
 
 
1491
  query_id=query_id,
1492
  feedback_type=f"score_{int(score)}",
1493
  feedback_text=comment,
@@ -1516,7 +1593,7 @@ with gr.Blocks(
1516
  if __name__ == "__main__":
1517
  demo.queue().launch(
1518
  server_name="0.0.0.0",
1519
- server_port=7862,
1520
  share=False,
1521
  ssr_mode=False,
1522
  show_error=True,
 
15
  from matplotlib import cm
16
  from glob import glob
17
  from natsort import natsorted
18
+ from huggingface_hub import HfApi, upload_file
19
 
20
  # ===== 导入三个推理模块 =====
21
  from inference_seg import load_model as load_seg_model, run as run_seg
22
  from inference_count import load_model as load_count_model, run as run_count
23
  from inference_track import load_model as load_track_model, run as run_track
24
 
25
+ HF_TOKEN = os.getenv("HF_TOKEN")
26
+ DATASET_REPO = "phoebe777777/celltool_feedback"
27
+
28
+
29
  # ===== 清理缓存目录 =====
30
  print("===== clearing cache =====")
31
  cache_path = os.path.expanduser("~/.cache/")
 
79
  DATASET_DIR = Path("solver_cache")
80
  DATASET_DIR.mkdir(parents=True, exist_ok=True)
81
 
82
+ def save_feedback_to_hf(query_id, feedback_type, feedback_text=None, img_path=None, bboxes=None):
83
+ """保存反馈到 Hugging Face Dataset"""
84
+
85
+ # 如果没有 token,回退到本地存储
86
+ if not HF_TOKEN:
87
+ print("⚠️ No HF_TOKEN found, using local storage")
88
+ save_feedback(query_id, feedback_type, feedback_text, img_path, bboxes)
89
+ return
90
+
91
+ feedback_data = {
92
+ "query_id": query_id,
93
+ "feedback_type": feedback_type,
94
+ "feedback_text": feedback_text,
95
+ "image_path": img_path,
96
+ "bboxes": str(bboxes), # 转为字符串
97
+ "datetime": time.strftime("%Y-%m-%d %H:%M:%S"),
98
+ "timestamp": time.time()
99
+ }
100
+
101
+ try:
102
+ api = HfApi()
103
+
104
+ # 创建临时文件
105
+ filename = f"feedback_{query_id}_{int(time.time())}.json"
106
+
107
+ with open(filename, 'w', encoding='utf-8') as f:
108
+ json.dump(feedback_data, f, indent=2, ensure_ascii=False)
109
+
110
+ # 上传到 dataset
111
+ api.upload_file(
112
+ path_or_fileobj=filename,
113
+ path_in_repo=f"data/{filename}",
114
+ repo_id=DATASET_REPO,
115
+ repo_type="dataset",
116
+ token=HF_TOKEN
117
+ )
118
+
119
+ # 清理本地文件
120
+ os.remove(filename)
121
+
122
+ print(f"✅ Feedback saved to HF Dataset: {DATASET_REPO}")
123
+
124
+ except Exception as e:
125
+ print(f"⚠️ Failed to save to HF Dataset: {e}")
126
+ # 回退到本地存储
127
+ save_feedback(query_id, feedback_type, feedback_text, img_path, bboxes)
128
+
129
+
130
  def save_feedback(query_id, feedback_type, feedback_text=None, img_path=None, bboxes=None):
131
  """保存用户反馈到JSON文件"""
132
  feedback_data = {
 
1052
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1053
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1054
 
1055
+ # save_feedback(
1056
+ # query_id=query_id,
1057
+ # feedback_type=f"score_{int(score)}",
1058
+ # feedback_text=comment,
1059
+ # img_path=img_path,
1060
+ # bboxes=bboxes
1061
+ # )
1062
+ # 使用 HF 存储
1063
+ save_feedback_to_hf(
1064
  query_id=query_id,
1065
  feedback_type=f"score_{int(score)}",
1066
  feedback_text=comment,
 
1234
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1235
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1236
 
1237
+ # save_feedback(
1238
+ # query_id=query_id,
1239
+ # feedback_type=f"score_{int(score)}",
1240
+ # feedback_text=comment,
1241
+ # img_path=img_path,
1242
+ # bboxes=bboxes
1243
+ # )
1244
+ # 使用 HF 存储
1245
+ save_feedback_to_hf(
1246
  query_id=query_id,
1247
  feedback_type=f"score_{int(score)}",
1248
  feedback_text=comment,
 
1556
  img_path = annot_val[0] if annot_val and len(annot_val) > 0 else None
1557
  bboxes = annot_val[1] if annot_val and len(annot_val) > 1 else []
1558
 
1559
+ # save_feedback(
1560
+ # query_id=query_id,
1561
+ # feedback_type=f"score_{int(score)}",
1562
+ # feedback_text=comment,
1563
+ # img_path=img_path,
1564
+ # bboxes=bboxes
1565
+ # )
1566
+ # 使用 HF 存储
1567
+ save_feedback_to_hf(
1568
  query_id=query_id,
1569
  feedback_type=f"score_{int(score)}",
1570
  feedback_text=comment,
 
1593
  if __name__ == "__main__":
1594
  demo.queue().launch(
1595
  server_name="0.0.0.0",
1596
+ server_port=7860,
1597
  share=False,
1598
  ssr_mode=False,
1599
  show_error=True,