Arafath10 commited on
Commit
21011da
1 Parent(s): 47628cc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +64 -44
main.py CHANGED
@@ -1,49 +1,69 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import StreamingResponse
3
- import os
4
- import io
 
 
5
 
6
- from transformers import pipeline
7
 
 
 
 
8
 
9
- pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
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
- @app.get("/")
26
- def read_root():
27
- output = pipe_flan("hi")
28
- return {"message": str(output[0]["generated_text"])}
29
-
30
- @app.post("/uploadfile/")
31
- async def create_upload_file(file: UploadFile = File(...)):
32
- try:
33
- # Save the file with a specific name
34
- file_path = "inputvoice.mp3"
35
-
36
- with open(file_path, "wb") as f:
37
- f.write(file.file.read())
38
-
39
- # Read the content of the saved file
40
- with open(file_path, "rb") as f:
41
- file_content = f.read()
42
-
43
- # Return the content as a streaming response
44
- return StreamingResponse(io.BytesIO(file_content), media_type="audio/mpeg", headers={"Content-Disposition": "inline; filename=inputvoice.mp3"})
45
-
46
- except PermissionError as e:
47
- return {"error": f"PermissionError: {str(e)}"}
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)}