Spaces:
Runtime error
Runtime error
Create results_store.py
Browse files- results_store.py +26 -0
results_store.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
RESULTS_DIR = "results"
|
| 7 |
+
|
| 8 |
+
def save_result(data: dict) -> str:
|
| 9 |
+
if not os.path.exists(RESULTS_DIR):
|
| 10 |
+
os.makedirs(RESULTS_DIR)
|
| 11 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 12 |
+
filename = f"{unique_id}.json"
|
| 13 |
+
path = os.path.join(RESULTS_DIR, filename)
|
| 14 |
+
with open(path, "w") as f:
|
| 15 |
+
json.dump({
|
| 16 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 17 |
+
"data": data
|
| 18 |
+
}, f, indent=2)
|
| 19 |
+
return filename
|
| 20 |
+
|
| 21 |
+
def load_result(filename: str) -> dict:
|
| 22 |
+
path = os.path.join(RESULTS_DIR, filename)
|
| 23 |
+
if not os.path.exists(path):
|
| 24 |
+
return {"error": "Result not found."}
|
| 25 |
+
with open(path, "r") as f:
|
| 26 |
+
return json.load(f)
|