-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
314 lines (261 loc) · 10.8 KB
/
Copy pathmain.py
File metadata and controls
314 lines (261 loc) · 10.8 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
from dotenv import load_dotenv
load_dotenv()
import os
import pandas as pd
import joblib
import threading
from flask import Flask, request, render_template_string, jsonify
from flask_cors import CORS
from speech.pipeline import record_until_silence
from speech.stt import transcribe
from speech.llm import ask, reset_conversation
from speech.tts import speak, synthesize_wav_bytes
from flask import Response
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Load the artifacts created by train.py
try:
model = joblib.load("framingham_rf_model.pkl")
saved_features = joblib.load("feature_names.pkl")
print("Model and features loaded into memory.")
except FileNotFoundError:
print("Error: Pickle files not found. Run train.py first!")
exit()
# ---------------------------------------------------------------------------
# SPEECH THREAD
# ---------------------------------------------------------------------------
def speech_loop():
try:
speak("Hi! I'm your heart health assistant. You can ask me about your cardiovascular risk results or heart health in general.")
except Exception as e:
print(f"[TTS startup error]: {e}")
while True:
try:
audio = record_until_silence()
if len(audio) == 0:
print(" ⚠️ No audio captured, retrying...")
continue
text = transcribe(audio)
if not text:
print(" 💭 Please try again...")
continue
print(f"Patient: {text}")
if any(w in text.lower() for w in ["goodbye", "bye", "exit", "quit"]):
speak("Take care, and stay heart healthy!")
break
if any(w in text.lower() for w in ["reset", "start over"]):
reset_conversation()
speak("Sure, let's start fresh. What would you like to know?")
continue
reply = ask(text)
print(f"Assistant: {reply}")
speak(reply)
except Exception as e:
print(f"[Speech loop error]: {e}")
continue
# ---------------------------------------------------------------------------
# WEB UI SECTION
# ---------------------------------------------------------------------------
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Framingham CVD Predictor</title>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 40px; background-color: #f0f2f5; color: #333; }
.container { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); max-width: 600px; margin: auto; }
h2 { color: #1a73e8; border-bottom: 2px solid #e8eaed; padding-bottom: 10px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
label { font-size: 0.9rem; font-weight: bold; display: block; margin-bottom: 5px; }
input { width: 100%; padding: 10px; border: 1px solid #dadce0; border-radius: 6px; box-sizing: border-box; }
.full-width { grid-column: span 2; }
button { background: #1a73e8; color: white; border: none; padding: 12px; border-radius: 6px; cursor: pointer; width: 100%; font-size: 1rem; margin-top: 10px; transition: background 0.3s; }
button:hover { background: #1557b0; }
.result { margin-top: 25px; padding: 20px; border-radius: 8px; background: #e8f0fe; border-left: 5px solid #1a73e8; }
</style>
</head>
<body>
<div class="container">
<h2>CVD 10-Year Risk Predictor</h2>
<form method="POST">
<div class="form-grid">
{% for feat in features %}
<div>
<label>{{ feat }}</label>
<input type="number" step="any" name="{{ feat }}" required>
</div>
{% endfor %}
</div>
<button type="submit">Calculate Risk Score</button>
</form>
{% if probability %}
<div class="result">
<strong>Analysis Result:</strong>
<p>Probability: {{ probability }}%</p>
<h3>Risk Category: {{ risk }}</h3>
</div>
{% endif %}
</div>
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def home():
probability = None
risk = None
if request.method == "POST":
try:
input_dict = {feat: [float(request.form[feat])] for feat in saved_features}
input_df = pd.DataFrame(input_dict)[saved_features]
prob = model.predict_proba(input_df)[0][1]
probability = round(prob * 100, 2)
risk = "Low Risk" if probability < 10 else "Moderate Risk" if probability < 20 else "High Risk"
except Exception as e:
return f"<h3>Form Error: {e}</h3>"
return render_template_string(HTML_TEMPLATE, features=saved_features, probability=probability, risk=risk)
# ---------------------------------------------------------------------------
# API SECTION
# ---------------------------------------------------------------------------
@app.route("/api/predict", methods=["POST", "OPTIONS"])
def api_predict():
"""
API Endpoint for CVD prediction.
Expects: JSON object with all feature names as keys.
Returns: JSON with probability and risk category.
"""
if request.method == "OPTIONS":
return "", 200
data = request.get_json()
print(f"Received data from frontend: {data}")
if not data:
return jsonify({"error": "No input data provided"}), 400
try:
print(f"Required features: {saved_features}")
# 1. Convert incoming JSON to DataFrame
# We wrap values in lists [v] because pandas expects a list for scalar values
input_dict = {feat: [float(data[feat])] for feat in saved_features}
input_df = pd.DataFrame(input_dict)
# 2. Reorder columns to match model
input_df = input_df[saved_features]
# 3. Predict
prob = model.predict_proba(input_df)[0][1]
probability_percent = round(prob * 100, 2)
# 4. Categorize
if probability_percent < 10:
risk = "Low Risk"
elif probability_percent < 20:
risk = "Moderate Risk"
else:
risk = "High Risk"
response_data = {
"status": "success",
"cvd_probability_percent": probability_percent,
"risk_category": risk,
"units": "10-year risk"
}
print(f"Sending response: {response_data}")
return jsonify(response_data)
except KeyError as e:
error_msg = f"Missing required feature: {str(e)}"
print(f"KeyError: {error_msg}")
return jsonify({"status": "error", "message": error_msg}), 400
except Exception as e:
error_msg = str(e)
print(f"Exception: {error_msg}")
return jsonify({"status": "error", "message": error_msg}), 500
@app.route("/api/chat", methods=["POST", "OPTIONS"])
def chat_endpoint():
"""Chat endpoint for the React frontend chat box.
Expects: JSON with 'message' field
Returns: JSON with 'response' field"""
if request.method == "OPTIONS":
return "", 200
data = request.get_json()
user_message = data.get("message", "").strip()
if not user_message:
return jsonify({"error": "No message provided"}), 400
try:
reply = ask(user_message)
return jsonify({
"status": "success",
"response": reply
})
except Exception as e:
print(f"[Chat error]: {e}")
return jsonify({
"status": "error",
"error": str(e)
}), 500
@app.route("/api/chat/reset", methods=["POST", "OPTIONS"])
def reset_chat():
"""Reset the conversation history"""
if request.method == "OPTIONS":
return "", 200
try:
reset_conversation()
return jsonify({"status": "success", "message": "Conversation reset"})
except Exception as e:
return jsonify({"status": "error", "error": str(e)}), 500
@app.route("/api/speak", methods=["POST", "OPTIONS"])
def speak_endpoint():
"""Speak text using the backend TTS (optional).
By default, the React frontend uses browser text-to-speech.
Use this endpoint if you want the backend to handle audio playback.
"""
if request.method == "OPTIONS":
return "", 200
data = request.get_json()
text = data.get("text", "").strip()
if not text:
return jsonify({"error": "No text provided"}), 400
try:
# Keep original server-side playback behavior for local use
speak(text)
return jsonify({"status": "success", "message": "Speaking..."})
except Exception as e:
print(f"[Speak error]: {e}")
return jsonify({"status": "error", "error": str(e)}), 500
@app.route("/api/speak_audio", methods=["POST", "OPTIONS"])
def speak_audio_endpoint():
"""Return WAV audio bytes (Rachel voice) for frontend playback."""
if request.method == "OPTIONS":
return "", 200
data = request.get_json()
text = data.get("text", "").strip()
if not text:
return jsonify({"error": "No text provided"}), 400
try:
wav_bytes = synthesize_wav_bytes(text)
return Response(wav_bytes, mimetype="audio/wav")
except Exception as e:
print(f"[Speak audio error]: {e}")
return jsonify({"status": "error", "error": str(e)}), 500
@app.route("/api/explain", methods=["POST"])
def explain_result():
"""Call this from your React frontend after a prediction to have the
assistant speak the result aloud to the patient."""
data = request.get_json()
probability = data.get("probability")
risk = data.get("risk")
prompt = (
f"The patient's 10-year cardiovascular risk score just came back as {probability}%, "
f"which is classified as {risk}. Please explain what this means in simple, reassuring "
f"terms and give one actionable lifestyle tip."
)
reply = ask(prompt)
speak(reply)
return jsonify({"status": "spoken", "message": reply})
if __name__ == "__main__":
# 🎙️ Voice assistant mode DISABLED for web UI
# The React frontend now handles all voice interactions through the chatbox button
# To re-enable terminal voice assistant, uncomment the lines below:
#
# speech_thread = threading.Thread(target=speech_loop, daemon=True)
# speech_thread.start()
# CVD Prediction API runs on port 5001 (separate from LLM service)
# Note: host='0.0.0.0' allows access from other devices on the same network
# Use_reloader=False prevents duplicate speech threads when in debug mode
print("🚀 Backend running on http://127.0.0.1:5001")
print("🎙️ Voice control: Only React frontend (no terminal voice input)")
print("💬 Chat API: POST http://127.0.0.1:5001/api/chat")
app.run(debug=True, port=5001, use_reloader=False)