Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,44 @@ log_dir = "./logs"
|
|
21 |
os.makedirs(log_dir, exist_ok=True) # ensure folder exists
|
22 |
log_file_path = os.path.join(log_dir, "Second_Opinion_Logs.log")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def log_action(action, request=None):
|
25 |
"""
|
26 |
Logs major actions with IP address and UTC timestamp.
|
|
|
21 |
os.makedirs(log_dir, exist_ok=True) # ensure folder exists
|
22 |
log_file_path = os.path.join(log_dir, "Second_Opinion_Logs.log")
|
23 |
|
24 |
+
########################################
|
25 |
+
# AUTO-UPLOAD LOGS TO HUGGING FACE
|
26 |
+
########################################
|
27 |
+
import threading
|
28 |
+
import time
|
29 |
+
from huggingface_hub import HfApi, HfFolder
|
30 |
+
|
31 |
+
def upload_logs_to_hf():
|
32 |
+
"""Upload the local log file to Hugging Face repo."""
|
33 |
+
try:
|
34 |
+
api = HfApi()
|
35 |
+
token = HfFolder.get_token() or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
36 |
+
if not token:
|
37 |
+
print(" No HF token found — skipping upload.")
|
38 |
+
return
|
39 |
+
api.upload_file(
|
40 |
+
path_or_fileobj=log_file_path,
|
41 |
+
path_in_repo="logs/Second_Opinion_Logs.log",
|
42 |
+
repo_id="singhn9/privateSOWN",
|
43 |
+
repo_type="space",
|
44 |
+
token=token,
|
45 |
+
)
|
46 |
+
print("Logs uploaded to Hugging Face repo.")
|
47 |
+
except Exception as e:
|
48 |
+
print(f" Log upload failed: {e}")
|
49 |
+
|
50 |
+
def background_log_uploader(interval=300):
|
51 |
+
"""Run log upload in background every `interval` seconds."""
|
52 |
+
while True:
|
53 |
+
time.sleep(interval)
|
54 |
+
if os.path.exists(log_file_path):
|
55 |
+
upload_logs_to_hf()
|
56 |
+
|
57 |
+
# start the background thread (daemon=True so it won't block shutdown)
|
58 |
+
threading.Thread(target=background_log_uploader, daemon=True).start()
|
59 |
+
print(" Background log uploader started.")
|
60 |
+
|
61 |
+
|
62 |
def log_action(action, request=None):
|
63 |
"""
|
64 |
Logs major actions with IP address and UTC timestamp.
|