File size: 2,261 Bytes
21011da
 
 
 
 
 
feca41c
0b8107d
21011da
b6cd364
 
0b8107d
21011da
 
 
 
ff9efc0
21011da
feca41c
21011da
 
 
 
 
 
 
 
 
 
662feb4
 
21011da
 
 
2ffb657
 
21011da
662feb4
21011da
b6cd364
aee20ee
b6cd364
40d2593
21011da
 
 
76bdd82
 
2ffb657
 
21011da
76bdd82
21011da
 
 
 
 
 
 
 
 
 
 
 
 
c44a196
21011da
2ffb657
21011da
b6cd364
21011da
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pandas as pd
import numpy as np
import joblib


# Load your trained model and encoders
xgb_model = joblib.load("model/transexpress_xgb_model.joblib")
encoders = joblib.load("model/transexpress_encoders.joblib")

# Function to handle unseen labels during encoding
def safe_transform(encoder, column):
    classes = encoder.classes_
    return [encoder.transform([x])[0] if x in classes else -1 for x in column]

# Define FastAPI app
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Endpoint for making predictions
@app.post("/predict")
def predict(
    customer_name: str,
    customer_address: str,
    customer_phone: str,
    customer_email: str,
    weight: int,
    cod: int,
    pickup_address: str,
    destination_city_name: str):
    # Convert input data to DataFrame

    if not destination_city_name.strip():
        destination_city_name = 'Missing'
    print(destination_city_name)   
    input_data = {
        'customer_name': customer_name,
        'customer_address': customer_address,
        'customer_phone_no': customer_phone,
        'client_email': customer_email,
        'weight': weight,
        'cod': cod,
        'pickup_address':pickup_address,
        'destination_branch_name':destination_city_name
    }
    input_df = pd.DataFrame([input_data])

    # Encode categorical variables using the same encoders used during training
    for col in input_df.columns:
        if col in encoders:
            input_df[col] = safe_transform(encoders[col], input_df[col])

    # Predict and obtain probabilities
    pred = xgb_model.predict(input_df)
    pred_proba = xgb_model.predict_proba(input_df)

    # Output
    predicted_status = "Unknown" if pred[0] == -1 else encoders['status_name'].inverse_transform([pred])[0]
    probability = pred_proba[0][pred[0]] * 100 if pred[0] != -1 else "Unknown"
    print(input_data,predicted_status,probability)

    if predicted_status == "Returned to Client":
       probability = 100 - probability

    return {"Probability": round(probability,2)}