Spaces:
Runtime error
Runtime error
Update config.py
Browse files
config.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
import os
|
| 2 |
import logging
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from txagent.txagent import TxAgent
|
| 5 |
from db.mongo import get_mongo_client
|
| 6 |
import asyncio
|
| 7 |
from pyfcm import FCMNotification
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Logging
|
| 10 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
@@ -38,10 +39,17 @@ patients_collection = db["patients"]
|
|
| 38 |
analysis_collection = db["patient_analysis_results"]
|
| 39 |
alerts_collection = db["clinical_alerts"]
|
| 40 |
notifications_collection = db["notifications"]
|
| 41 |
-
logger.info("📡 Connected to MongoDB synchronously")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# FCM settings
|
| 44 |
-
FCM_API_KEY = os.getenv("FCM_API_KEY", "your-fcm-server-key")
|
| 45 |
push_service = FCMNotification(api_key=FCM_API_KEY)
|
| 46 |
|
| 47 |
async def send_push_notification(recipient_email, message):
|
|
@@ -50,7 +58,7 @@ async def send_push_notification(recipient_email, message):
|
|
| 50 |
user = await users_collection.find_one({"email": recipient_email})
|
| 51 |
device_token = user.get("device_token") if user and "device_token" in user else None
|
| 52 |
if not device_token:
|
| 53 |
-
logger.warning(f"No device token found for {recipient_email}")
|
| 54 |
return
|
| 55 |
|
| 56 |
# Send push notification
|
|
@@ -58,14 +66,14 @@ async def send_push_notification(recipient_email, message):
|
|
| 58 |
registration_id=device_token,
|
| 59 |
message_title="Risk Alert",
|
| 60 |
message_body=message,
|
| 61 |
-
sound="default" #
|
|
|
|
| 62 |
)
|
| 63 |
-
logger.info(f"Push notification sent to {recipient_email}: {result}")
|
| 64 |
except Exception as e:
|
| 65 |
-
logger.error(f"Failed to send push notification to {recipient_email}: {str(e)}")
|
| 66 |
|
| 67 |
# JWT settings
|
| 68 |
-
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
| 69 |
ALGORITHM = "HS256"
|
| 70 |
|
| 71 |
def setup_app(app: FastAPI):
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from txagent.txagent import TxAgent
|
| 4 |
from db.mongo import get_mongo_client
|
| 5 |
import asyncio
|
| 6 |
from pyfcm import FCMNotification
|
| 7 |
+
from huggingface_hub import HfApi, get_secret
|
| 8 |
+
from datetime import datetime
|
| 9 |
|
| 10 |
# Logging
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
| 39 |
analysis_collection = db["patient_analysis_results"]
|
| 40 |
alerts_collection = db["clinical_alerts"]
|
| 41 |
notifications_collection = db["notifications"]
|
| 42 |
+
logger.info("📡 Connected to MongoDB synchronously at %s", datetime.utcnow().isoformat())
|
| 43 |
+
|
| 44 |
+
# Retrieve secrets from Hugging Face
|
| 45 |
+
FCM_API_KEY = get_secret("FCM_API_KEY")
|
| 46 |
+
SECRET_KEY = get_secret("SECRET_KEY")
|
| 47 |
+
|
| 48 |
+
if not FCM_API_KEY or not SECRET_KEY:
|
| 49 |
+
logger.error("❌ Missing FCM_API_KEY or SECRET_KEY in Hugging Face Secrets")
|
| 50 |
+
raise ValueError("FCM_API_KEY and SECRET_KEY must be set in Hugging Face Secrets")
|
| 51 |
|
| 52 |
# FCM settings
|
|
|
|
| 53 |
push_service = FCMNotification(api_key=FCM_API_KEY)
|
| 54 |
|
| 55 |
async def send_push_notification(recipient_email, message):
|
|
|
|
| 58 |
user = await users_collection.find_one({"email": recipient_email})
|
| 59 |
device_token = user.get("device_token") if user and "device_token" in user else None
|
| 60 |
if not device_token:
|
| 61 |
+
logger.warning(f"No device token found for {recipient_email} at {datetime.utcnow().isoformat()}")
|
| 62 |
return
|
| 63 |
|
| 64 |
# Send push notification
|
|
|
|
| 66 |
registration_id=device_token,
|
| 67 |
message_title="Risk Alert",
|
| 68 |
message_body=message,
|
| 69 |
+
sound="default", # Plays a sound on the device
|
| 70 |
+
priority="high"
|
| 71 |
)
|
| 72 |
+
logger.info(f"Push notification sent to {recipient_email} at {datetime.utcnow().isoformat()}: {result}")
|
| 73 |
except Exception as e:
|
| 74 |
+
logger.error(f"Failed to send push notification to {recipient_email} at {datetime.utcnow().isoformat()}: {str(e)}")
|
| 75 |
|
| 76 |
# JWT settings
|
|
|
|
| 77 |
ALGORITHM = "HS256"
|
| 78 |
|
| 79 |
def setup_app(app: FastAPI):
|