ExtraaLearn_API / app.py
Sirisha335's picture
Upload folder using huggingface_hub
41e0d10 verified
Raw
History Blame Contribute Delete
2.56 kB
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app
app = Flask(__name__)
# Load the model
model = joblib.load('Extraalearn.joblib')
EXPECTED_FEATURES = [
'age', 'website_visits', 'time_spent_on_website', 'page_views_per_visit',
'current_occupation_Student', 'current_occupation_Unemployed',
'first_interaction_Website', 'profile_completed_Low',
'profile_completed_Medium', 'last_activity_Phone Activity',
'last_activity_Website Activity', 'print_media_type1_Yes',
'print_media_type2_Yes', 'digital_media_Yes',
'educational_channels_Yes', 'referral_Yes'
]
@app.get('/')
def home():
return 'ExtraaLearn Lead Conversion API is Running'
@app.post('/v1/predict')
def predict():
try:
data = request.get_json()
p = {}
p['age'] = float(data.get('age', 0))
p['website_visits'] = float(data.get('website_visits', 0))
p['time_spent_on_website'] = float(data.get('time_spent_on_website', 0))
p['page_views_per_visit'] = float(data.get('page_views_per_visit', 0))
p['current_occupation_Student'] = 1 if data.get('current_occupation') == 'Student' else 0
p['current_occupation_Unemployed'] = 1 if data.get('current_occupation') == 'Unemployed' else 0
p['first_interaction_Website'] = 1 if data.get('first_interaction') == 'Website' else 0
p['profile_completed_Low'] = 1 if data.get('profile_completed') == 'Low' else 0
p['profile_completed_Medium'] = 1 if data.get('profile_completed') == 'Medium' else 0
p['last_activity_Phone Activity'] = 1 if data.get('last_activity') == 'Phone Activity' else 0
p['last_activity_Website Activity'] = 1 if data.get('last_activity') == 'Website Activity' else 0
p['print_media_type1_Yes'] = 1 if data.get('print_media_type1') == 'Yes' else 0
p['print_media_type2_Yes'] = 1 if data.get('print_media_type2') == 'Yes' else 0
p['digital_media_Yes'] = 1 if data.get('digital_media') == 'Yes' else 0
p['educational_channels_Yes'] = 1 if data.get('educational_channels') == 'Yes' else 0
p['referral_Yes'] = 1 if data.get('referral') == 'Yes' else 0
df_final = pd.DataFrame([p])[EXPECTED_FEATURES]
prediction_proba = model.predict_proba(df_final)[:, 1].tolist()[0]
return jsonify({'Conversion_Probability': float(prediction_proba)})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)