|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
MODEL_FILE = 'pmad_prediction_model.joblib' |
|
|
|
|
|
MODEL_FEATURES = [ |
|
|
'PPD_History', 'Anxiety_History', 'Birth_Trauma', 'Support_Score', |
|
|
'Sentiment_Score', 'Keyword_Guilt_Freq', 'Keyword_Worry_Freq', |
|
|
'Keyword_Intrusive_Freq', 'Keyword_Delusion_Freq', |
|
|
'Avg_Sleep_Duration_hrs', 'Sleep_Fragmentation_Score', |
|
|
'Avg_Heart_Rate_Variability', 'Activity_Steps_Avg' |
|
|
] |
|
|
|
|
|
|
|
|
try: |
|
|
model = joblib.load(MODEL_FILE) |
|
|
print(f"β
Model '{MODEL_FILE}' loaded successfully.") |
|
|
except FileNotFoundError: |
|
|
print(f"β ERROR: Model file '{MODEL_FILE}' not found. Did you upload it?") |
|
|
model = None |
|
|
except Exception as e: |
|
|
print(f"β ERROR: Could not load model: {e}") |
|
|
model = None |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
def analyze_journal(journal_text): |
|
|
text = journal_text.lower() |
|
|
analysis = { |
|
|
'Sentiment_Score': 0.0, |
|
|
'Keyword_Guilt_Freq': text.count('guilt') + text.count('failing') + text.count('bad mom'), |
|
|
'Keyword_Worry_Freq': text.count('worry') + text.count('anxious') + text.count('scared'), |
|
|
'Keyword_Intrusive_Freq': text.count('intrusive') + text.count('harm'), |
|
|
'Keyword_Delusion_Freq': text.count('delusion') + text.count('not real') |
|
|
} |
|
|
|
|
|
|
|
|
negative_words = analysis['Keyword_Guilt_Freq'] + analysis['Keyword_Worry_Freq'] |
|
|
if negative_words > 2: |
|
|
analysis['Sentiment_Score'] = -0.7 |
|
|
elif negative_words > 0: |
|
|
analysis['Sentiment_Score'] = -0.3 |
|
|
|
|
|
if text.count('happy') > 0 or text.count('great') > 0: |
|
|
analysis['Sentiment_Score'] = 0.5 |
|
|
|
|
|
return analysis |
|
|
|
|
|
@app.route("/") |
|
|
def home(): |
|
|
return "Maia PMAD Model API (Complex) is running." |
|
|
|
|
|
@app.route("/predict", methods=['POST']) |
|
|
def predict(): |
|
|
if model is None: |
|
|
return jsonify({"error": "Model is not loaded"}), 500 |
|
|
|
|
|
try: |
|
|
|
|
|
data = request.json |
|
|
print(f"Received data: {data}") |
|
|
|
|
|
|
|
|
|
|
|
app_mood = data.get('mood', 3) |
|
|
app_sleep = data.get('sleep', 7.0) |
|
|
app_journal = data.get('journal', '') |
|
|
|
|
|
|
|
|
journal_analysis = analyze_journal(app_journal) |
|
|
|
|
|
|
|
|
if app_mood <= 2 and journal_analysis['Sentiment_Score'] > -0.5: |
|
|
journal_analysis['Sentiment_Score'] = -0.5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input_data = { |
|
|
|
|
|
'Sentiment_Score': journal_analysis['Sentiment_Score'], |
|
|
'Keyword_Guilt_Freq': journal_analysis['Keyword_Guilt_Freq'], |
|
|
'Keyword_Worry_Freq': journal_analysis['Keyword_Worry_Freq'], |
|
|
'Keyword_Intrusive_Freq': journal_analysis['Keyword_Intrusive_Freq'], |
|
|
'Keyword_Delusion_Freq': journal_analysis['Keyword_Delusion_Freq'], |
|
|
|
|
|
|
|
|
'Avg_Sleep_Duration_hrs': app_sleep, |
|
|
|
|
|
|
|
|
'PPD_History': 0, |
|
|
'Anxiety_History': 0, |
|
|
'Birth_Trauma': 0, |
|
|
'Support_Score': 3.0, |
|
|
'Sleep_Fragmentation_Score': 0.5, |
|
|
'Avg_Heart_Rate_Variability': 50.0, |
|
|
'Activity_Steps_Avg': 4000 |
|
|
} |
|
|
|
|
|
|
|
|
df = pd.DataFrame([input_data], columns=MODEL_FEATURES) |
|
|
print(f"Model input vector:\n{df.to_string()}") |
|
|
|
|
|
|
|
|
prediction_proba = model.predict_proba(df) |
|
|
|
|
|
|
|
|
class_map = {0: 'Healthy', 1: 'PPD', 2: 'PPA', 3: 'P-OCD', 4: 'Severe/PPP/PTSD'} |
|
|
probabilities = prediction_proba[0] |
|
|
|
|
|
output = { |
|
|
class_map[i]: prob for i, prob in enumerate(probabilities) |
|
|
} |
|
|
|
|
|
print(f"Prediction: {output}") |
|
|
return jsonify(output) |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Error during prediction: {e}") |
|
|
return jsonify({"error": str(e)}), 400 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
app.run(host='0.0.0.0', port=7860) |