Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -292,46 +292,46 @@ def save_all_results_to_file(all_results, user_data):
|
|
| 292 |
"""
|
| 293 |
Packages results and uploads them as a single JSON file to a Hugging Face Dataset.
|
| 294 |
"""
|
| 295 |
-
#
|
| 296 |
-
repo_id = "
|
| 297 |
|
| 298 |
-
#
|
| 299 |
username = user_data.get("age", "user")
|
| 300 |
filename = f"submission_{username}_{pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 301 |
-
|
| 302 |
-
|
|
|
|
| 303 |
final_data_package = {
|
| 304 |
"user_info": user_data,
|
| 305 |
"results": all_results
|
| 306 |
}
|
| 307 |
-
|
| 308 |
-
# Convert the dictionary to a JSON string in memory
|
| 309 |
json_string = json.dumps(final_data_package, ensure_ascii=False, indent=4)
|
| 310 |
-
|
| 311 |
-
#
|
| 312 |
hf_token = os.getenv("HF_TOKEN")
|
| 313 |
|
| 314 |
if not hf_token:
|
| 315 |
-
print("HF_TOKEN not found. Cannot upload to the Hub.
|
| 316 |
return
|
| 317 |
|
| 318 |
try:
|
| 319 |
-
|
| 320 |
-
api = HfApi()
|
| 321 |
|
| 322 |
-
#
|
|
|
|
| 323 |
api.upload_file(
|
| 324 |
path_or_fileobj=bytes(json_string, "utf-8"),
|
| 325 |
-
path_in_repo=
|
| 326 |
repo_id=repo_id,
|
| 327 |
repo_type="dataset",
|
| 328 |
token=hf_token,
|
| 329 |
commit_message=f"Add new submission from {username}"
|
| 330 |
)
|
| 331 |
-
print(f"Successfully uploaded
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
-
print(f"
|
|
|
|
| 335 |
|
| 336 |
def toggle_reference_view(current):
|
| 337 |
if current == "参考": return gr.update(visible=False), gr.update(visible=True), gr.update(value="返回")
|
|
|
|
| 292 |
"""
|
| 293 |
Packages results and uploads them as a single JSON file to a Hugging Face Dataset.
|
| 294 |
"""
|
| 295 |
+
# 设置新的目标仓库
|
| 296 |
+
repo_id = "intersteller2887/Turing-test-web"
|
| 297 |
|
| 298 |
+
# 生成文件名(可用年龄或其他信息标记)
|
| 299 |
username = user_data.get("age", "user")
|
| 300 |
filename = f"submission_{username}_{pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 301 |
+
remote_path = f"Submissions/{filename}" # 上传到 Submissions 文件夹中
|
| 302 |
+
|
| 303 |
+
# 打包数据
|
| 304 |
final_data_package = {
|
| 305 |
"user_info": user_data,
|
| 306 |
"results": all_results
|
| 307 |
}
|
|
|
|
|
|
|
| 308 |
json_string = json.dumps(final_data_package, ensure_ascii=False, indent=4)
|
| 309 |
+
|
| 310 |
+
# 获取 Hugging Face token(确保你已在 Space Secrets 中设置)
|
| 311 |
hf_token = os.getenv("HF_TOKEN")
|
| 312 |
|
| 313 |
if not hf_token:
|
| 314 |
+
print("HF_TOKEN not found. Cannot upload to the Hub.")
|
| 315 |
return
|
| 316 |
|
| 317 |
try:
|
| 318 |
+
from huggingface_hub import HfApi
|
|
|
|
| 319 |
|
| 320 |
+
# 上传 JSON 文件
|
| 321 |
+
api = HfApi()
|
| 322 |
api.upload_file(
|
| 323 |
path_or_fileobj=bytes(json_string, "utf-8"),
|
| 324 |
+
path_in_repo=remote_path,
|
| 325 |
repo_id=repo_id,
|
| 326 |
repo_type="dataset",
|
| 327 |
token=hf_token,
|
| 328 |
commit_message=f"Add new submission from {username}"
|
| 329 |
)
|
| 330 |
+
print(f"Successfully uploaded to: {repo_id}/{remote_path}")
|
| 331 |
|
| 332 |
except Exception as e:
|
| 333 |
+
print(f"Upload failed: {e}")
|
| 334 |
+
|
| 335 |
|
| 336 |
def toggle_reference_view(current):
|
| 337 |
if current == "参考": return gr.update(visible=False), gr.update(visible=True), gr.update(value="返回")
|