File size: 2,564 Bytes
b3eb32f
066a3ac
 
 
b3eb32f
41e0d10
9931cf1
41e0d10
 
066a3ac
b3eb32f
9c7ed55
 
 
 
 
 
 
 
 
 
9931cf1
b3eb32f
41e0d10
b3eb32f
9931cf1
e4c0391
066a3ac
 
e4c0391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e0d10
e4c0391
41e0d10
 
066a3ac
 
b3eb32f
 
9931cf1
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
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)