intersteller2887 commited on
Commit
28535b3
·
verified ·
1 Parent(s): 6853053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -292,45 +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 = "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):
 
292
  """
293
  Packages results and uploads them as a single JSON file to a Hugging Face Dataset.
294
  """
295
+ # IMPORTANT: Change this to your Hugging Face username and dataset repo name
296
+ repo_id = "intersteller2887/Turing-test-dataset"
297
 
298
+ # Create a unique filename for the submission
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
+ # Package all data into a single dictionary
 
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
+ # Get the Hugging Face token from the environment secrets
312
  hf_token = os.getenv("HF_TOKEN")
313
 
314
  if not hf_token:
315
+ print("HF_TOKEN not found. Cannot upload to the Hub. Please set it in Space secrets.")
316
  return
317
 
318
  try:
319
+ # Instantiate the HfApi client
 
 
320
  api = HfApi()
321
+
322
+ # Upload the JSON string as a file to the specified dataset repository
323
  api.upload_file(
324
  path_or_fileobj=bytes(json_string, "utf-8"),
325
+ path_in_repo=f"submissions/{filename}", # We recommend saving to a subfolder
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 results to dataset: {repo_id}")
332
 
333
  except Exception as e:
334
+ print(f"Error uploading to Hugging Face Hub: {e}")
335
 
336
 
337
  def toggle_reference_view(current):