Spaces:
Running
Running
Update src/feedback.py
Browse files- src/feedback.py +19 -16
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 |
|