emp-admin's picture
Upload 4 files
627c614 verified
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import pandas as pd
import numpy as np
# Load Models
with open("risk_model.pkl", "rb") as f: risk_model = pickle.load(f)
with open("advice_model.pkl", "rb") as f: advice_model = pickle.load(f)
app = FastAPI()
# --- 🌟 THE ULTRA-ATTRACTIVE LIBRARY 🌟 ---
ADVICE_LIBRARY = {
0: {
"title": "Clear Skies, Clear Head",
"emoji": "🌿",
"body": "The atmosphere is stable and calm. It’s the perfect window for deep focus work or a tough workout while your migraine threshold is high. Enjoy the clarity!"
},
1: {
"title": "Rapid Pressure Drop",
"emoji": "📉",
"body": "The barometer is diving fast (storm incoming). This acts like a vacuum on your blood vessels. Consider taking your preventative meds now if you are sensitive. Steady yourself; this shift is temporary."
},
2: {
"title": "The Pressure Squeeze",
"emoji": "📈",
"body": "Pressure is spiking upwards. While the sky might be clearing, this can clamp down on sinuses, causing tightness behind the eyes. Hydrate well to help your body equalize."
},
3: {
"title": "The 'Sauna' Effect",
"emoji": "🥵",
"body": "It’s not just hot; the air is heavy with humidity. This 'soup' prevents your body from cooling down and can silently trigger dehydration headaches. Sip electrolyte water constantly today."
},
4: {
"title": "High Wind Warning",
"emoji": "🌬️",
"body": "Turbulent winds are whipping up allergens and creating pressure fluctuations around your ears. This chaos often triggers jaw clenching. Protect your ears with a scarf or beanie if you step outside."
},
5: {
"title": "High UV Glare",
"emoji": "😎",
"body": "The sun is piercingly bright, creating a high-contrast environment that strains the visual cortex. This is a prime trigger for ocular migraines. Don't leave the house without polarized sunglasses."
},
6: {
"title": "Bitter Cold Tension",
"emoji": "❄️",
"body": "The air is biting cold and dry, causing an automatic tensing of your neck and scalp muscles (the 'bracing' reflex). Keep your neck warm with a scarf and humidify your room to prevent tension headaches."
},
7: {
"title": "Drastic Temp Drop",
"emoji": "🥶",
"body": "The temperature has plummeted since yesterday, shocking your vascular system into constriction. Your body is burning energy to stay warm. Dress in layers and keep your blood sugar steady."
},
8: {
"title": "Heat Shock",
"emoji": "🌡️",
"body": "A sudden heat spike is causing rapid vasodilation—swelling your blood vessels quickly. This rush can lead to a throbbing sensation. Stay in cool, climate-controlled environments to calm your vascular response."
},
9: {
"title": "Heavy Dampness",
"emoji": "🌫️",
"body": "The air is thick with fog and moisture, creating that 'heavy head' feeling. You might feel lethargic or dull. Try some invigorating breathwork or peppermint oil to cut through the brain fog."
}
}
class WeatherInput(BaseModel):
temp_c: float
pressure_hpa: float
humidity: float
wind_kph: float
uv_index: int
pressure_drop: float
temp_change: float
@app.get("/")
def home(): return {"status": "Biometeorology AI is Active"}
@app.post("/predict")
def predict(input_data: WeatherInput):
# Prepare Data
df = pd.DataFrame([input_data.dict()])
# 1. Predict Risk
risk_pred = risk_model.predict(df)[0]
risk_score = int(max(0, min(100, risk_pred)))
# 2. Predict Advice
advice_id = int(advice_model.predict(df)[0])
content = ADVICE_LIBRARY.get(advice_id, ADVICE_LIBRARY[0])
return {
"risk_score": risk_score,
"risk_level": "High" if risk_score > 60 else ("Moderate" if risk_score > 30 else "Low"),
"condition": {
"id": advice_id,
"title": content["title"],
"emoji": content["emoji"],
"text": content["body"]
}
}