Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,49 +1,69 @@
|
|
1 |
-
from fastapi import FastAPI,
|
2 |
-
from fastapi.
|
3 |
-
import
|
4 |
-
import
|
|
|
|
|
5 |
|
6 |
-
from transformers import pipeline
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
temp = open("model/t.txt","w")
|
15 |
-
temp.write("aaaaaaaaaaaaa")
|
16 |
-
temp.close()
|
17 |
-
|
18 |
-
temp = open("model/t.txt","r")
|
19 |
|
|
|
20 |
app = FastAPI()
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
@app.post("/
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
import joblib
|
7 |
|
|
|
8 |
|
9 |
+
# Load your trained model and encoders
|
10 |
+
xgb_model = joblib.load("xgb_model.joblib")
|
11 |
+
encoders = joblib.load("encoders.joblib")
|
12 |
|
13 |
+
# Function to handle unseen labels during encoding
|
14 |
+
def safe_transform(encoder, column):
|
15 |
+
classes = encoder.classes_
|
16 |
+
return [encoder.transform([x])[0] if x in classes else -1 for x in column]
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Define FastAPI app
|
19 |
app = FastAPI()
|
20 |
+
app.add_middleware(
|
21 |
+
CORSMiddleware,
|
22 |
+
allow_origins=["*"],
|
23 |
+
allow_credentials=True,
|
24 |
+
allow_methods=["*"],
|
25 |
+
allow_headers=["*"],
|
26 |
+
)
|
27 |
+
|
28 |
+
# Endpoint for making predictions
|
29 |
+
@app.post("/predict")
|
30 |
+
def predict(customer_name: str,
|
31 |
+
customer_address: str,
|
32 |
+
customer_phone: str,
|
33 |
+
customer_email: str,
|
34 |
+
cod:str,
|
35 |
+
weight: str,
|
36 |
+
pickup_address: str,
|
37 |
+
origin_city_name: str,
|
38 |
+
destination_city_name: str):
|
39 |
+
# Convert input data to DataFrame
|
40 |
+
input_data = {
|
41 |
+
'customer_name': customer_name,
|
42 |
+
'customer_address': customer_address,
|
43 |
+
'customer_phone': customer_phone,
|
44 |
+
'customer_email': customer_email,
|
45 |
+
'cod': float(cod),
|
46 |
+
'weight': float(weight),
|
47 |
+
'pickup_address':pickup_address,
|
48 |
+
'origin_city.name':origin_city_name,
|
49 |
+
'destination_city.name':destination_city_name
|
50 |
+
}
|
51 |
+
input_df = pd.DataFrame([input_data])
|
52 |
+
|
53 |
+
# Encode categorical variables using the same encoders used during training
|
54 |
+
for col in input_df.columns:
|
55 |
+
if col in encoders:
|
56 |
+
input_df[col] = safe_transform(encoders[col], input_df[col])
|
57 |
+
|
58 |
+
# Predict and obtain probabilities
|
59 |
+
pred = xgb_model.predict(input_df)
|
60 |
+
pred_proba = xgb_model.predict_proba(input_df)
|
61 |
+
|
62 |
+
# Output
|
63 |
+
predicted_status = "Unknown" if pred[0] == -1 else encoders['status.name'].inverse_transform([pred])[0]
|
64 |
+
probability = pred_proba[0][pred[0]] * 100 if pred[0] != -1 else "Unknown"
|
65 |
+
|
66 |
+
if predicted_status == "RETURN TO CLIENT":
|
67 |
+
probability = 100 - probability
|
68 |
+
|
69 |
+
return {"Probability": round(probability,2)}
|