|
a |
|
b/test.py |
|
|
1 |
from fastapi import FastAPI |
|
|
2 |
import numpy as np |
|
|
3 |
import tensorflow as tf |
|
|
4 |
from pydantic import BaseModel |
|
|
5 |
|
|
|
6 |
model = tf.keras.models.load_model("heart_monitor_lstm.h5") |
|
|
7 |
scaler_mean = np.load("scaler.npy") |
|
|
8 |
print(scaler_mean) |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
app = FastAPI() |
|
|
12 |
latest_prediction = {"status": "Waiting...", "confidence": 0.0} |
|
|
13 |
|
|
|
14 |
class HeartData(BaseModel): |
|
|
15 |
heart_rate: float |
|
|
16 |
spo2: float |
|
|
17 |
|
|
|
18 |
@app.post("/predict") |
|
|
19 |
def predict_heart_condition(data: HeartData): |
|
|
20 |
global latest_prediction |
|
|
21 |
input_data = np.array([[data.heart_rate, data.spo2]]) - scaler_mean |
|
|
22 |
input_data = input_data.reshape(1, 1, 2) |
|
|
23 |
|
|
|
24 |
prediction = model.predict(input_data)[0][0] |
|
|
25 |
result = "Abnormal" if prediction > 0.5 else "Normal" |
|
|
26 |
|
|
|
27 |
latest_prediction = {"status": result, "confidence": round(prediction * 100, 1)} |
|
|
28 |
return latest_prediction |
|
|
29 |
|
|
|
30 |
@app.get("/get_latest_prediction") |
|
|
31 |
def get_latest_prediction(): |
|
|
32 |
return latest_prediction |