Spaces:
Sleeping

File size: 1,565 Bytes
943c754
 
 
 
 
 
 
 
1a2d266
943c754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify, render_template_string
import pandas as pd
import joblib
from xgboost import XGBClassifier

app = Flask(__name__)

model = XGBClassifier()
model.load_model("PCOSXGB2.bin")
X_train_columns = ['Weight', 'Height', 'BG', 'bpm', 'RR', 'cycle', 'CL', 'Pregnant', 'Abortions', 'Hip', 'Waist', 'WG', 'HG', 'FF', 'HL', 'Pimples', 'RE', 'BP_Systo', 'BP_Diasto']
categorical_columns = ['BG', 'Pregnant', 'WG', 'HG', 'HL', 'FF', 'Pimples', 'RE', 'cycle']
mapping = {
    'No': 0, 'Yes': 1,
    'A+': 11, 'A-': 12, 'B+': 13, 'B-': 14,
    'O+': 15, 'O-': 16, 'AB+': 17, 'AB-': 18,
    'Regular': 2, 'Neutral': 5, 'Irregular': 4
}

with open("PCOSpred.html", "r", encoding="utf-8") as f:
    template_html = f.read()

@app.route('/')
def home():
    return render_template_string(template_html)

@app.route('/predict', methods=['POST'])
def predict():
    try:
        data = request.get_json()
        df = pd.DataFrame([data])
        for col in categorical_columns:
            if col in df.columns:
                df[col] = df[col].replace(mapping)
        df = pd.get_dummies(df, drop_first=True)
        for col in X_train_columns:
            if col not in df.columns:
                df[col] = 0
        df = df[X_train_columns]
        prediction = model.predict(df)[0]
        result = "PCOS Positive" if prediction == 1 else "PCOS Negative"
        return jsonify({'prediction': result})
    except Exception as e:
        return jsonify({'error': str(e)})

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=7860)