prasad6145 commited on
Commit
4404380
·
verified ·
1 Parent(s): 947d560

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -41
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
- import gradio as gr
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 upload it to Hugging Face Space.")
13
-
14
-
15
- # Load the trained model
16
- model = tf.keras.models.load_model("sleep_cognition_model.h5")
17
-
18
- # Define prediction function
19
- def predict(TST, SE, WASO, REM_Sleep, Deep_Sleep, Number_of_Awakenings):
20
- input_data = np.array([[TST, SE, WASO, REM_Sleep, Deep_Sleep, Number_of_Awakenings]])
21
- prediction = model.predict(input_data)
22
- return {
23
- "Reaction Time": float(prediction[0][0]),
24
- "Memory Recall Accuracy": float(prediction[0][1]),
25
- "Attention Score": float(prediction[0][2]),
26
- "Executive Function Score": float(prediction[0][3]),
27
- "Mental Fatigue Index": float(prediction[0][4]),
28
- }
29
-
30
- # Define Gradio interface
31
- iface = gr.Interface(
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)