Emmanuel Frimpong Asante
update space
ff5b28f
raw
history blame
3.07 kB
# app.py
import os
import time
import logging
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import tensorflow as tf
from routes.authentication import auth_router
# from routes.disease_detection import disease_router
# from routes.health_dashboard import dashboard_router
# from services.health_monitoring_service import evaluate_health_data, get_health_alerts, send_alerts
from huggingface_hub import login
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Check GPU availability for TensorFlow
gpu_devices = tf.config.list_physical_devices('GPU')
if gpu_devices:
logger.info(f"GPUs available: {len(gpu_devices)}")
else:
logger.info("No GPUs available. Using CPU.")
# Initialize FastAPI app and templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# Mount static files if directory exists
static_dir = "static"
if os.path.isdir(static_dir):
app.mount("/static", StaticFiles(directory=static_dir), name="static")
logger.info("Mounted static directory at /static")
else:
logger.error("Static directory not found.")
raise HTTPException(status_code=500, detail="Static directory not found.")
# Load Hugging Face Token
HF_TOKEN = os.environ.get('HF_Token')
if HF_TOKEN:
login(token=HF_TOKEN, add_to_git_credential=True)
else:
logger.warning("Hugging Face token not found in environment variables.")
# Include routers for authentication, disease detection, and health dashboard
app.include_router(auth_router, prefix="/auth", tags=["Authentication"])
# app.include_router(disease_router, prefix="/disease", tags=["Disease Detection"])
# app.include_router(dashboard_router, prefix="/dashboard", tags=["Dashboard"])
@app.get("/", response_class=HTMLResponse)
async def landing_page(request: Request):
"""Render the landing page template."""
logger.info("Landing page accessed")
return templates.TemplateResponse("landing.html", {"request": request})
# health metrics for periodic monitoring
health_metrics = {
"weight_loss_percentage": 6,
"mortality_rate": 1,
"reduced_feed_intake_percentage": 12
}
# def monitor_health():
# """Evaluate health data and log notifications if thresholds are crossed."""
# while True:
# # Evaluate health metrics and send alerts
# notifications = evaluate_health_data(health_metrics)
# if notifications["notifications"]:
# logger.info(f"Health Notifications: {notifications['notifications']}")
# # Optionally, send alerts to the farmer
# send_alerts(notifications["notifications"], os.getenv("FARMER_EMAIL"))
# time.sleep(3600) # Run every hour
#
# @app.on_event("startup")
# async def startup_event():
# """Initialize background health monitoring on startup."""
# logger.info("Starting background health monitoring.")
# BackgroundTasks().add_task(monitor_health)