Spaces:
Sleeping

pcostypepred / app.py
nivith's picture
Update app.py
1a2d266 verified
Raw History Blame Contribute Delete
1.57 kB
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)