Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update main.py
Browse files
    	
        main.py
    CHANGED
    
    | @@ -1,20 +1,13 @@ | |
| 1 | 
             
            from ultralytics import YOLO
         | 
|  | |
| 2 |  | 
|  | |
|  | |
| 3 |  | 
| 4 | 
             
            import asyncio
         | 
| 5 | 
             
            from fastapi import FastAPI
         | 
| 6 | 
             
            from fastapi.middleware.cors import CORSMiddleware
         | 
| 7 | 
             
            import requests
         | 
| 8 | 
            -
            import pandas as pd
         | 
| 9 | 
            -
            import json
         | 
| 10 | 
            -
            import os,datetime
         | 
| 11 | 
            -
            import pandas as pd
         | 
| 12 | 
            -
            from sklearn.model_selection import train_test_split, GridSearchCV
         | 
| 13 | 
            -
            from sklearn.preprocessing import LabelEncoder
         | 
| 14 | 
            -
            from xgboost import XGBClassifier
         | 
| 15 | 
            -
            from sklearn.metrics import accuracy_score, classification_report
         | 
| 16 | 
            -
            from joblib import dump, load
         | 
| 17 | 
            -
            import numpy as np
         | 
| 18 |  | 
| 19 |  | 
| 20 | 
             
            app = FastAPI()
         | 
| @@ -29,214 +22,7 @@ app.add_middleware( | |
| 29 |  | 
| 30 |  | 
| 31 |  | 
| 32 | 
            -
            def train_the_model(data):
         | 
| 33 | 
            -
                try:     
         | 
| 34 | 
            -
                    new_data = data
         | 
| 35 | 
            -
                    encoders = load('transexpress_encoders.joblib')
         | 
| 36 | 
            -
                    xgb_model = load('transexpress_xgb_model.joblib')
         | 
| 37 | 
            -
                    selected_columns = ['customer_name', 'customer_address', 'customer_phone_no',
         | 
| 38 | 
            -
                                        'weight','cod','pickup_address','client_number','destination_city',
         | 
| 39 | 
            -
                                        'status_name']
         | 
| 40 | 
            -
                    
         | 
| 41 | 
            -
                    new_data_filled = new_data[selected_columns].fillna('Missing')
         | 
| 42 | 
            -
                    for col, encoder in encoders.items():
         | 
| 43 | 
            -
                        if col in new_data_filled.columns:
         | 
| 44 | 
            -
                            unseen_categories = set(new_data_filled[col]) - set(encoder.classes_)
         | 
| 45 | 
            -
                            if unseen_categories:
         | 
| 46 | 
            -
                                for category in unseen_categories:
         | 
| 47 | 
            -
                                    encoder.classes_ = np.append(encoder.classes_, category)
         | 
| 48 | 
            -
                                new_data_filled[col] = encoder.transform(new_data_filled[col])
         | 
| 49 | 
            -
                            else:
         | 
| 50 | 
            -
                                new_data_filled[col] = encoder.transform(new_data_filled[col])
         | 
| 51 | 
            -
                    X_new = new_data_filled.drop('status_name', axis=1)
         | 
| 52 | 
            -
                    y_new = new_data_filled['status_name']
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                    X_train, X_test, y_train, y_test = train_test_split(X_new,y_new, test_size=0.2, random_state=42)
         | 
| 55 | 
            -
                    
         | 
| 56 | 
            -
                    xgb_model.fit(X_new, y_new)
         | 
| 57 | 
            -
                    dump(xgb_model,'transexpress_xgb_model.joblib')
         | 
| 58 | 
            -
                    
         | 
| 59 | 
            -
             | 
| 60 | 
            -
                    y_pred = xgb_model.predict(X_test)
         | 
| 61 | 
            -
                    accuracy = accuracy_score(y_test, y_pred)
         | 
| 62 | 
            -
                    classification_rep = classification_report(y_test, y_pred)
         | 
| 63 | 
            -
                    return accuracy,classification_rep,"Model finetuned with new data."
         | 
| 64 | 
            -
             | 
| 65 | 
            -
                    
         | 
| 66 | 
            -
                except:
         | 
| 67 | 
            -
                    data = data
         | 
| 68 | 
            -
                    
         | 
| 69 | 
            -
                    # Select columns
         | 
| 70 | 
            -
                    selected_columns = ['customer_name', 'customer_address', 'customer_phone_no',
         | 
| 71 | 
            -
                                        'weight','cod','pickup_address','client_number','destination_city',
         | 
| 72 | 
            -
                                        'status_name']
         | 
| 73 | 
            -
                    
         | 
| 74 | 
            -
                    # Handling missing values
         | 
| 75 | 
            -
                    data_filled = data[selected_columns].fillna('Missing')
         | 
| 76 | 
            -
                    
         | 
| 77 | 
            -
                    # Encoding categorical variables
         | 
| 78 | 
            -
                    encoders = {col: LabelEncoder() for col in selected_columns if data_filled[col].dtype == 'object'}
         | 
| 79 | 
            -
                    for col, encoder in encoders.items():
         | 
| 80 | 
            -
                        data_filled[col] = encoder.fit_transform(data_filled[col])
         | 
| 81 | 
            -
                    
         | 
| 82 | 
            -
                    # Splitting the dataset
         | 
| 83 | 
            -
                    X = data_filled.drop('status_name', axis=1)
         | 
| 84 | 
            -
                    y = data_filled['status_name']
         | 
| 85 | 
            -
                    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
         | 
| 86 | 
            -
                    
         | 
| 87 | 
            -
                    # Setup the hyperparameter grid to search
         | 
| 88 | 
            -
                    param_grid = {
         | 
| 89 | 
            -
                        'max_depth': [3, 4, 5],
         | 
| 90 | 
            -
                        'learning_rate': [0.01, 0.1, 0.4],
         | 
| 91 | 
            -
                        'n_estimators': [100, 200, 300],
         | 
| 92 | 
            -
                        'subsample': [0.8, 0.9, 1],
         | 
| 93 | 
            -
                        'colsample_bytree': [0.3, 0.7]
         | 
| 94 | 
            -
                    }
         | 
| 95 | 
            -
                    
         | 
| 96 | 
            -
                    # Initialize the classifier
         | 
| 97 | 
            -
                    xgb = XGBClassifier(use_label_encoder=False, eval_metric='logloss')
         | 
| 98 | 
            -
                    
         | 
| 99 | 
            -
                    # Setup GridSearchCV
         | 
| 100 | 
            -
                    grid_search = GridSearchCV(xgb, param_grid, cv=2, n_jobs=-1, scoring='accuracy')
         | 
| 101 | 
            -
                    
         | 
| 102 | 
            -
                    # Fit the grid search to the data
         | 
| 103 | 
            -
                    grid_search.fit(X_train, y_train)
         | 
| 104 | 
            -
                    
         | 
| 105 | 
            -
                    # Get the best parameters
         | 
| 106 | 
            -
                    best_params = grid_search.best_params_
         | 
| 107 | 
            -
                    print("Best parameters:", best_params)
         | 
| 108 | 
            -
                    
         | 
| 109 | 
            -
                    # Train the model with best parameters
         | 
| 110 | 
            -
                    best_xgb = XGBClassifier(**best_params, use_label_encoder=False, eval_metric='logloss')
         | 
| 111 | 
            -
                    best_xgb.fit(X_train, y_train)
         | 
| 112 | 
            -
                    
         | 
| 113 | 
            -
                    # Predict on the test set
         | 
| 114 | 
            -
                    y_pred = best_xgb.predict(X_test)
         | 
| 115 | 
            -
                    y_pred_proba = best_xgb.predict_proba(X_test)
         | 
| 116 | 
            -
                    
         | 
| 117 | 
            -
                    # Evaluate the model
         | 
| 118 | 
            -
                    accuracy = accuracy_score(y_test, y_pred)
         | 
| 119 | 
            -
                    classification_rep = classification_report(y_test, y_pred)
         | 
| 120 | 
            -
                    
         | 
| 121 | 
            -
                    # Save the model
         | 
| 122 | 
            -
                    model_filename = 'transexpress_xgb_model.joblib'
         | 
| 123 | 
            -
                    dump(best_xgb, model_filename)
         | 
| 124 | 
            -
                    
         | 
| 125 | 
            -
                    # Save the encoders
         | 
| 126 | 
            -
                    encoders_filename = 'transexpress_encoders.joblib'
         | 
| 127 | 
            -
                    dump(encoders, encoders_filename)
         | 
| 128 | 
            -
                    
         | 
| 129 | 
            -
                    return accuracy,classification_rep,"base Model trained"
         | 
| 130 | 
            -
                
         | 
| 131 | 
            -
            @app.get("/trigger_the_data_fecher")
         | 
| 132 | 
            -
            async def your_continuous_function(page: str,paginate: str):
         | 
| 133 | 
            -
                print("data fetcher running.....")
         | 
| 134 | 
            -
                        
         | 
| 135 | 
            -
                # Initialize an empty DataFrame to store the combined data
         | 
| 136 | 
            -
                combined_df = pd.DataFrame()
         | 
| 137 | 
            -
                        
         | 
| 138 | 
            -
                # Update the payload for each page
         | 
| 139 | 
            -
                url = "https://report.transexpress.lk/api/orders/delivery-success-rate/return-to-client-orders?page="+page+"&per_page="+paginate
         | 
| 140 | 
            -
                
         | 
| 141 | 
            -
                payload = {}
         | 
| 142 | 
            -
                headers = {
         | 
| 143 | 
            -
                  'Cookie': 'development_trans_express_session=NaFDGzh5WQCFwiortxA6WEFuBjsAG9GHIQrbKZ8B'
         | 
| 144 | 
            -
                }
         | 
| 145 | 
            -
                        
         | 
| 146 | 
            -
                response = requests.request("GET", url, headers=headers, data=payload)
         | 
| 147 | 
            -
                        
         | 
| 148 | 
            -
                # Sample JSON response
         | 
| 149 | 
            -
                json_response = response.json()
         | 
| 150 | 
            -
                # Extracting 'data' for conversion
         | 
| 151 | 
            -
                data = json_response["return_to_client_orders"]['data']
         | 
| 152 | 
            -
             | 
| 153 | 
            -
                data_count = len(data)  
         | 
| 154 | 
            -
                
         | 
| 155 | 
            -
                df = pd.json_normalize(data)
         | 
| 156 | 
            -
                
         | 
| 157 | 
            -
                        
         | 
| 158 | 
            -
                df['status_name'] = df['status_name'].replace('Partially Delivered', 'Delivered')
         | 
| 159 | 
            -
                df['status_name'] = df['status_name'].replace('Received by Client', 'Returned to Client')
         | 
| 160 | 
            -
                
         | 
| 161 | 
            -
                print("data collected from page : "+page)
         | 
| 162 | 
            -
                return "done"
         | 
| 163 | 
            -
                #data.to_csv("new.csv")
         | 
| 164 | 
            -
                
         | 
| 165 | 
            -
                #accuracy,classification_rep,message = train_the_model(df)
         | 
| 166 | 
            -
             | 
| 167 | 
            -
                #return {"message":message,"page_number":page,"data_count":data_count,"accuracy":accuracy,"classification_rep":classification_rep}
         | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 170 | 
            -
                
         | 
| 171 | 
            -
             | 
| 172 | 
            -
            @app.get("/get_latest_model_updated_time")
         | 
| 173 | 
            -
            async def model_updated_time():
         | 
| 174 | 
            -
                try:
         | 
| 175 | 
            -
                    m_time_encoder = os.path.getmtime('transexpress_encoders.joblib')
         | 
| 176 | 
            -
                    m_time_model = os.path.getmtime('transexpress_xgb_model.joblib')
         | 
| 177 | 
            -
                    return {"base model created time ":datetime.datetime.fromtimestamp(m_time_encoder),
         | 
| 178 | 
            -
                            "last model updated time":datetime.datetime.fromtimestamp(m_time_model)}
         | 
| 179 | 
            -
                except:
         | 
| 180 | 
            -
                    return {"no model found so first trained the model using data fecther"}
         | 
| 181 | 
            -
             | 
| 182 | 
            -
             | 
| 183 | 
            -
             | 
| 184 | 
            -
             | 
| 185 | 
            -
             | 
| 186 | 
            -
            # Endpoint for making predictions
         | 
| 187 | 
            -
            @app.post("/predict")
         | 
| 188 | 
            -
            def predict(
         | 
| 189 | 
            -
                customer_name: str,
         | 
| 190 | 
            -
                customer_address: str,
         | 
| 191 | 
            -
                customer_phone: str,
         | 
| 192 | 
            -
                weight: int,
         | 
| 193 | 
            -
                cod: int,
         | 
| 194 | 
            -
                pickup_address: str,
         | 
| 195 | 
            -
                client_number:str,
         | 
| 196 | 
            -
                destination_city:str
         | 
| 197 | 
            -
                ):
         | 
| 198 | 
            -
             | 
| 199 | 
            -
             | 
| 200 | 
            -
                try:
         | 
| 201 | 
            -
                    # Load your trained model and encoders
         | 
| 202 | 
            -
                    xgb_model = load('transexpress_xgb_model.joblib')
         | 
| 203 | 
            -
                    encoders = load('transexpress_encoders.joblib')
         | 
| 204 | 
            -
                except:
         | 
| 205 | 
            -
                    return {"no model found so first trained the model using data fecther"}
         | 
| 206 | 
            -
             | 
| 207 |  | 
| 208 | 
            -
             | 
| 209 | 
            -
             | 
| 210 | 
            -
             | 
| 211 | 
            -
                    return [encoder.transform([x])[0] if x in classes else -1 for x in column] 
         | 
| 212 | 
            -
                    
         | 
| 213 | 
            -
                # Convert input data to DataFrame
         | 
| 214 | 
            -
                input_data = {
         | 
| 215 | 
            -
                    'customer_name': customer_name,
         | 
| 216 | 
            -
                    'customer_address': customer_address,
         | 
| 217 | 
            -
                    'customer_phone_no': customer_phone,
         | 
| 218 | 
            -
                    'weight': weight,
         | 
| 219 | 
            -
                    'cod': cod,
         | 
| 220 | 
            -
                    'pickup_address':pickup_address,
         | 
| 221 | 
            -
                    'client_number':client_number,
         | 
| 222 | 
            -
                    'destination_city':destination_city
         | 
| 223 | 
            -
                }
         | 
| 224 | 
            -
                input_df = pd.DataFrame([input_data])
         | 
| 225 | 
            -
             | 
| 226 | 
            -
                # Encode categorical variables using the same encoders used during training
         | 
| 227 | 
            -
                for col in input_df.columns:
         | 
| 228 | 
            -
                    if col in encoders:
         | 
| 229 | 
            -
                        input_df[col] = safe_transform(encoders[col], input_df[col])
         | 
| 230 | 
            -
             | 
| 231 | 
            -
                # Predict and obtain probabilities
         | 
| 232 | 
            -
                pred = xgb_model.predict(input_df)
         | 
| 233 | 
            -
                pred_proba = xgb_model.predict_proba(input_df)
         | 
| 234 | 
            -
             | 
| 235 | 
            -
                # Output
         | 
| 236 | 
            -
                predicted_status = "Unknown" if pred[0] == -1 else encoders['status_name'].inverse_transform([pred])[0]
         | 
| 237 | 
            -
                probability = pred_proba[0][pred[0]] * 100 if pred[0] != -1 else "Unknown"
         | 
| 238 | 
            -
             | 
| 239 | 
            -
                if predicted_status == "Returned to Client":
         | 
| 240 | 
            -
                   probability = 100 - probability
         | 
| 241 | 
            -
             | 
| 242 | 
            -
                return {"Probability": round(probability,2)}
         | 
|  | |
| 1 | 
             
            from ultralytics import YOLO
         | 
| 2 | 
            +
            model = YOLO('best.pt')  # load a custom model
         | 
| 3 |  | 
| 4 | 
            +
            # Predict with the model
         | 
| 5 | 
            +
            results = model('test.jpg')  # predict on an image
         | 
| 6 |  | 
| 7 | 
             
            import asyncio
         | 
| 8 | 
             
            from fastapi import FastAPI
         | 
| 9 | 
             
            from fastapi.middleware.cors import CORSMiddleware
         | 
| 10 | 
             
            import requests
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 11 |  | 
| 12 |  | 
| 13 | 
             
            app = FastAPI()
         | 
|  | |
| 22 |  | 
| 23 |  | 
| 24 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 25 |  | 
| 26 | 
            +
            @app.get("/predict")
         | 
| 27 | 
            +
            async def get_prediction(page: str,paginate: str):
         | 
| 28 | 
            +
                  return "done"
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  |