Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,31 @@
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU and force CPU mode
|
3 |
|
4 |
-
|
5 |
-
import numpy as np
|
6 |
-
import tensorflow as tf
|
7 |
|
|
|
8 |
model_path = "sleep_cognition_model.h5"
|
9 |
|
10 |
-
# Check if the model file exists before loading
|
11 |
if not os.path.exists(model_path):
|
12 |
-
raise FileNotFoundError(f"⚠️ Model file not found: {model_path}. Please
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
fn=predict,
|
33 |
-
inputs=[
|
34 |
-
gr.Number(label="Total Sleep Time (TST)"),
|
35 |
-
gr.Number(label="Sleep Efficiency (SE)"),
|
36 |
-
gr.Number(label="Wake After Sleep Onset (WASO)"),
|
37 |
-
gr.Number(label="REM Sleep (%)"),
|
38 |
-
gr.Number(label="Deep Sleep (%)"),
|
39 |
-
gr.Number(label="Number of Awakenings"),
|
40 |
-
],
|
41 |
-
outputs="json",
|
42 |
-
title="Sleep & Cognitive Function Predictor",
|
43 |
-
description="Enter sleep parameters to predict cognitive function scores.",
|
44 |
-
)
|
45 |
-
|
46 |
-
# Run the app
|
47 |
-
iface.launch()
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
import os
|
|
|
5 |
|
6 |
+
app = Flask(__name__)
|
|
|
|
|
7 |
|
8 |
+
# Load the model
|
9 |
model_path = "sleep_cognition_model.h5"
|
10 |
|
|
|
11 |
if not os.path.exists(model_path):
|
12 |
+
raise FileNotFoundError(f"⚠️ Model file not found: {model_path}. Please check the path.")
|
13 |
+
|
14 |
+
model = tf.keras.models.load_model(model_path)
|
15 |
+
|
16 |
+
@app.route("/", methods=["GET"])
|
17 |
+
def home():
|
18 |
+
return jsonify({"message": "Sleep Cognition Model API is running!"})
|
19 |
+
|
20 |
+
@app.route("/predict", methods=["POST"])
|
21 |
+
def predict():
|
22 |
+
try:
|
23 |
+
data = request.json["input"]
|
24 |
+
data = np.array(data).reshape(1, -1) # Reshape input data if needed
|
25 |
+
prediction = model.predict(data)
|
26 |
+
return jsonify({"prediction": prediction.tolist()})
|
27 |
+
except Exception as e:
|
28 |
+
return jsonify({"error": str(e)}), 400
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|