-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
109 lines (90 loc) · 4.9 KB
/
app.py
File metadata and controls
109 lines (90 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, render_template, jsonify, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
app = Flask(__name__)
# 1. REQUIRED: Secret key for session encryption
app.secret_key = 'health_project_2026_secret'
# Database Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'iamrahul'
app.config['MYSQL_DB'] = 'health_project'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
# 2. NEW ROUTE: Patient Intake Form
@app.route('/', methods=['GET', 'POST'])
def intake():
if request.method == 'POST':
# Capture details from intake.html form
session['patient_name'] = request.form.get('name')
session['patient_age'] = request.form.get('age')
session['patient_id_ref'] = request.form.get('p_id')
# --- ADD THESE THREE MISSING LINES ---
session['blood_group'] = request.form.get('blood_group')
session['weight'] = request.form.get('weight')
session['potential_diseases'] = request.form.get('potential_diseases')
# Redirect to the dashboard once details are saved
return redirect(url_for('dashboard'))
# If GET, just show the registration form
return render_template('intake.html')
# 3. MODIFIED ROUTE: Dashboard
@app.route('/dashboard')
def dashboard():
# Security: If no name in session, send back to intake
if 'patient_name' not in session:
return redirect(url_for('intake'))
# Pass session data to index.html (Ensure names match your {{ variables }} in HTML)
return render_template('index.html',
name=session.get('patient_name'),
age=session.get('patient_age'),
p_ref=session.get('patient_id_ref'),
# --- ADD THESE THREE MISSING MAPPINGS ---
blood_group=session.get('blood_group'),
weight=session.get('weight'),
potential_diseases=session.get('potential_diseases'))
def analyze_glucose(glucose_level):
"""
Provides a precise, descriptive, and actionable AI suggestion
based on 2026 clinical glucose guidelines (mg/dL).
"""
if glucose_level < 70:
return f"🚨 Hypoglycemia Alert: Glucose level is critically low ({glucose_level} mg/dL). Monitor for dizziness, confusion, or tremor. Immediate action: Administer 15g of fast-acting carbohydrate (e.g., 4oz juice) and retest in 15 minutes."
elif glucose_level > 200:
return f"⚠️ Critical Hyperglycemia: Glucose level is significantly elevated ({glucose_level} mg/dL). This may indicate poor metabolic control or an acute event. Recommended action: Immediate medical review and potential intervention required. Check for ketones if applicable."
elif glucose_level >= 140 and glucose_level <= 200:
return f"🟡 Elevated Glucose (Hyperglycemia Risk): Level is elevated ({glucose_level} mg/dL), potentially a post-meal spike or early sign of prediabetes. Recommended action: Monitor fasting levels closely and consult a clinician for further screening."
else:
# Normal Range: 70-139 mg/dL
return f"✅ Normal Range: Glucose level is optimal ({glucose_level} mg/dL). Continue routine monitoring and maintain current diet/medication schedule."
@app.route('/api/vitals')
def get_vitals():
# ... (Keep existing database fetching logic) ...
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM refined_health_data ORDER BY timestamp DESC LIMIT 10")
data = cur.fetchall()
cur.close()
# AI scoring logic for each record
for record in data:
# ... (Keep existing BPM, SpO2 AI risk score calculation) ...
score = 0
if record['spo2'] < 95: score += 40
if record['bpm'] > 100 or record['bpm'] < 60: score += 30
if record['hrv'] < 50: score += 20
# Add a 10-point penalty if glucose is abnormal
if record['glucose'] < 70 or record['glucose'] > 140: score += 10
record['risk_score'] = min(score, 100)
# ... (Keep existing ai_level and msg logic based on risk_score) ...
if record['risk_score'] >= 70:
record['ai_level'] = "CRITICAL"
record['msg'] = "Immediate medical intervention required."
elif record['risk_score'] >= 30:
record['ai_level'] = "WARNING"
record['msg'] = "Condition deteriorating. Increase monitoring."
else:
record['ai_level'] = "STABLE"
record['msg'] = "Patient is stable. Continue routine monitoring."
# NEW: Add specific glucose recommendation
record['glucose_msg'] = analyze_glucose(record['glucose'])
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)