-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.py
More file actions
60 lines (52 loc) · 1.72 KB
/
Copy pathhistory.py
File metadata and controls
60 lines (52 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import os
import csv
from datetime import datetime
# Path to the JSON history file
HISTORY_FILE = "history.json"
# ─── Log Prediction to JSON ───
def log_prediction(label, confidence):
entry = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"prediction": label,
"confidence": round(confidence * 100, 2) # store as percentage
}
# Load existing history or start fresh
history = []
if os.path.exists(HISTORY_FILE):
try:
with open(HISTORY_FILE, "r") as f:
data = json.load(f)
if isinstance(data, list):
history = data
except json.JSONDecodeError:
history = []
# Append new entry and save
history.append(entry)
with open(HISTORY_FILE, "w") as f:
json.dump(history, f, indent=4)
# ─── Export History to CSV ───
def export_to_csv(json_path=HISTORY_FILE, csv_path="history.csv"):
"""
Reads the JSON history and writes it to a CSV file.
Returns the CSV path if successful, or None if no data.
"""
# Load history list
if not os.path.exists(json_path):
return None
try:
with open(json_path, "r") as f:
history = json.load(f)
if not isinstance(history, list) or not history:
return None
except (json.JSONDecodeError, ValueError):
return None
# Write to CSV
try:
with open(csv_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["timestamp", "prediction", "confidence"])
writer.writeheader()
writer.writerows(history)
except Exception:
return None
return csv_path