Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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-
|
| 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 |
-
# 打包数据
|
| 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 |
-
#
|
| 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 |
-
|
| 319 |
-
|
| 320 |
-
# 上传 JSON 文件
|
| 321 |
api = HfApi()
|
|
|
|
|
|
|
| 322 |
api.upload_file(
|
| 323 |
path_or_fileobj=bytes(json_string, "utf-8"),
|
| 324 |
-
path_in_repo=
|
| 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}
|
| 331 |
|
| 332 |
except Exception as e:
|
| 333 |
-
print(f"
|
| 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):
|