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les
|
| 4 |
+
import warnings
|
| 5 |
+
import os
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
import matplotlib
|
| 9 |
+
matplotlib.use('Agg') # Use non-interactive backend
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder
|
| 13 |
+
import uuid
|
| 14 |
+
import asyncio
|
| 15 |
+
|
| 16 |
+
# Optimize TensorFlow for faster loading
|
| 17 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 18 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 19 |
+
tf.config.set_visible_devices([], 'GPU') # Use CPU only for faster startup
|
| 20 |
+
warnings.filterwarnings('ignore')
|
| 21 |
+
|
| 22 |
+
app = FastAPI(title="EV Battery Management System")
|
| 23 |
+
|
| 24 |
+
# Global variables to cache loaded models and data
|
| 25 |
+
model = None
|
| 26 |
+
scaler = None
|
| 27 |
+
data = None
|
| 28 |
+
label_encoders = {}
|
| 29 |
+
numeric_features = []
|
| 30 |
+
vehicle_type_to_model = {
|
| 31 |
+
"car": "Model A",
|
| 32 |
+
"bike": "Model B",
|
| 33 |
+
"scooter": "Model C",
|
| 34 |
+
"bus": "Model D"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Load models and data at startup
|
| 38 |
+
@app.on_event("startup")
|
| 39 |
+
async def load_models():
|
| 40 |
+
global model, scaler, data, label_encoders, numeric_features
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
print("Starting model and data loading...")
|
| 44 |
+
|
| 45 |
+
# Define file paths - check multiple locations
|
| 46 |
+
csv_paths = [
|
| 47 |
+
"ev_battery_charging_data.csv",
|
| 48 |
+
"../ev_battery_charging_data.csv",
|
| 49 |
+
os.path.join(os.path.dirname(__file__), "ev_battery_charging_data.csv"),
|
| 50 |
+
os.path.join(os.path.dirname(__file__), "..", "ev_battery_charging_data.csv")
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
model_paths = [
|
| 54 |
+
"ev_bms_colab_model.h5",
|
| 55 |
+
"../ev_bms_colab_model.h5",
|
| 56 |
+
os.path.join(os.path.dirname(__file__), "ev_bms_colab_model.h5"),
|
| 57 |
+
os.path.join(os.path.dirname(__file__), "..", "ev_bms_colab_model.h5")
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
# Find CSV file
|
| 61 |
+
csv_file = None
|
| 62 |
+
for path in csv_paths:
|
| 63 |
+
if os.path.exists(path):
|
| 64 |
+
csv_file = path
|
| 65 |
+
print(f"Found CSV file: {path}")
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
if csv_file is None:
|
| 69 |
+
print("Warning: CSV file not found, will use dummy data")
|
| 70 |
+
|
| 71 |
+
# Find model file
|
| 72 |
+
model_file = None
|
| 73 |
+
for path in model_paths:
|
| 74 |
+
if os.path.exists(path):
|
| 75 |
+
model_file = path
|
| 76 |
+
print(f"Found model file: {path}")
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
if model_file is None:
|
| 80 |
+
print("Warning: Model file not found, will use dummy model")
|
| 81 |
+
|
| 82 |
+
# Load data if available
|
| 83 |
+
if csv_file and os.path.exists(csv_file):
|
| 84 |
+
print("Loading CSV data...")
|
| 85 |
+
data = pd.read_csv(csv_file)
|
| 86 |
+
data.dropna(inplace=True)
|
| 87 |
+
|
| 88 |
+
# Handle categorical columns if they exist
|
| 89 |
+
categorical_columns = ['Charging Mode', 'Battery Type', 'EV Model']
|
| 90 |
+
existing_categorical = [col for col in categorical_columns if col in data.columns]
|
| 91 |
+
|
| 92 |
+
if existing_categorical:
|
| 93 |
+
label_encoders = {col: LabelEncoder().fit(data[col]) for col in existing_categorical}
|
| 94 |
+
for col in existing_categorical:
|
| 95 |
+
data[col] = label_encoders[col].transform(data[col])
|
| 96 |
+
|
| 97 |
+
# Define numeric features
|
| 98 |
+
exclude_cols = existing_categorical + ['Optimal Charging Duration Class']
|
| 99 |
+
numeric_features = [col for col in data.columns if col not in exclude_cols]
|
| 100 |
+
|
| 101 |
+
if numeric_features:
|
| 102 |
+
scaler = MinMaxScaler()
|
| 103 |
+
data[numeric_features] = scaler.fit_transform(data[numeric_features])
|
| 104 |
+
print(f"Processed {len(numeric_features)} numeric features")
|
| 105 |
+
else:
|
| 106 |
+
# Create dummy data if CSV not found
|
| 107 |
+
print("Creating dummy data...")
|
| 108 |
+
numeric_features = ['SOC (%)', 'Voltage (V)', 'Current (A)', 'Battery Temp (°C)',
|
| 109 |
+
'Ambient Temp (°C)', 'Charging Duration (min)',
|
| 110 |
+
'Degradation Rate (%)', 'Efficiency (%)', 'Charging Cycles']
|
| 111 |
+
|
| 112 |
+
# Create dummy dataset
|
| 113 |
+
np.random.seed(42)
|
| 114 |
+
dummy_data = {}
|
| 115 |
+
for feature in numeric_features:
|
| 116 |
+
dummy_data[feature] = np.random.uniform(0, 100, 1000)
|
| 117 |
+
|
| 118 |
+
data = pd.DataFrame(dummy_data)
|
| 119 |
+
scaler = MinMaxScaler()
|
| 120 |
+
data[numeric_features] = scaler.fit_transform(data[numeric_features])
|
| 121 |
+
|
| 122 |
+
# Load model if available
|
| 123 |
+
if model_file and os.path.exists(model_file):
|
| 124 |
+
print("Loading TensorFlow model...")
|
| 125 |
+
model = tf.keras.models.load_model(model_file, compile=False)
|
| 126 |
+
print("Model loaded successfully!")
|
| 127 |
+
else:
|
| 128 |
+
print("Model file not found, predictions will use dummy data")
|
| 129 |
+
|
| 130 |
+
print("Startup completed successfully!")
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"Startup error: {str(e)}")
|
| 134 |
+
# Don't raise the error, just log it - the app can still run with dummy data
|
| 135 |
+
|
| 136 |
+
# Add CORS middleware
|
| 137 |
+
app.add_middleware(
|
| 138 |
+
CORSMiddleware,
|
| 139 |
+
allow_origins=["*"],
|
| 140 |
+
allow_credentials=True,
|
| 141 |
+
allow_methods=["*"],
|
| 142 |
+
allow_headers=["*"],
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Mount static files
|
| 146 |
+
os.makedirs("static", exist_ok=True)
|
| 147 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 148 |
+
|
| 149 |
+
@app.get("/")
|
| 150 |
+
async def root():
|
| 151 |
+
return {"message": "EV Battery Management System API", "status": "running"}
|
| 152 |
+
|
| 153 |
+
@app.get("/health")
|
| 154 |
+
async def health_check():
|
| 155 |
+
global model, data, scaler
|
| 156 |
+
return {
|
| 157 |
+
"status": "healthy",
|
| 158 |
+
"model_loaded": model is not None,
|
| 159 |
+
"data_loaded": data is not None,
|
| 160 |
+
"scaler_loaded": scaler is not None
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
@app.get("/image/{filename}")
|
| 164 |
+
async def get_image(filename: str):
|
| 165 |
+
"""Serve images from static directory"""
|
| 166 |
+
file_path = os.path.join("static", filename)
|
| 167 |
+
if os.path.exists(file_path):
|
| 168 |
+
from fastapi.responses import FileResponse
|
| 169 |
+
return FileResponse(file_path, media_type="image/png")
|
| 170 |
+
raise HTTPException(status_code=404, detail="Image not found")
|
| 171 |
+
|
| 172 |
+
@app.post("/predict/")
|
| 173 |
+
async def predict(vehicle_type: str = Form(...)):
|
| 174 |
+
try:
|
| 175 |
+
print(f"Prediction request for vehicle type: {vehicle_type}")
|
| 176 |
+
|
| 177 |
+
# Use global variables
|
| 178 |
+
global model, scaler, data, numeric_features
|
| 179 |
+
|
| 180 |
+
# Validate vehicle type
|
| 181 |
+
if vehicle_type.lower() not in vehicle_type_to_model:
|
| 182 |
+
raise HTTPException(
|
| 183 |
+
status_code=400,
|
| 184 |
+
detail=f"Invalid vehicle type. Valid types: {list(vehicle_type_to_model.keys())}"
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
ev_model = vehicle_type_to_model[vehicle_type.lower()]
|
| 188 |
+
|
| 189 |
+
# Get sample data (either from real data or generate dummy data)
|
| 190 |
+
if data is not None and len(data) > 0:
|
| 191 |
+
# Use real data
|
| 192 |
+
sample_idx = np.random.randint(0, len(data))
|
| 193 |
+
original = data.iloc[sample_idx][numeric_features].values
|
| 194 |
+
else:
|
| 195 |
+
# Generate dummy data
|
| 196 |
+
print("Using dummy data for prediction")
|
| 197 |
+
original = np.random.uniform(0.1, 0.9, len(numeric_features))
|
| 198 |
+
|
| 199 |
+
# Make prediction
|
| 200 |
+
if model is not None and scaler is not None:
|
| 201 |
+
try:
|
| 202 |
+
# Scale input
|
| 203 |
+
original_reshaped = original.reshape(1, -1)
|
| 204 |
+
scaled_features = scaler.transform(original_reshaped)
|
| 205 |
+
|
| 206 |
+
# Reshape for model if needed
|
| 207 |
+
if len(scaled_features.shape) == 2:
|
| 208 |
+
scaled_features = scaled_features.reshape((1, scaled_features.shape[1], 1))
|
| 209 |
+
|
| 210 |
+
# Make prediction
|
| 211 |
+
prediction_scaled = model.predict(scaled_features, verbose=0)
|
| 212 |
+
prediction = scaler.inverse_transform(prediction_scaled.reshape(1, -1)).flatten()
|
| 213 |
+
except Exception as model_error:
|
| 214 |
+
print(f"Model prediction error: {model_error}")
|
| 215 |
+
# Fallback to dummy prediction
|
| 216 |
+
prediction = original + np.random.uniform(-0.1, 0.1, len(original))
|
| 217 |
+
else:
|
| 218 |
+
# Generate dummy prediction
|
| 219 |
+
prediction = original + np.random.uniform(-0.1, 0.1, len(original))
|
| 220 |
+
|
| 221 |
+
# Create visualization
|
| 222 |
+
try:
|
| 223 |
+
plt.figure(figsize=(12, 6))
|
| 224 |
+
plt.style.use('default')
|
| 225 |
+
|
| 226 |
+
index = np.arange(len(numeric_features))
|
| 227 |
+
bar_width = 0.35
|
| 228 |
+
|
| 229 |
+
bars1 = plt.bar(index - bar_width/2, original, bar_width,
|
| 230 |
+
label='Original', alpha=0.8, color='#2E86AB')
|
| 231 |
+
bars2 = plt.bar(index + bar_width/2, prediction, bar_width,
|
| 232 |
+
label='Predicted', alpha=0.8, color='#A23B72')
|
| 233 |
+
|
| 234 |
+
plt.xlabel('Parameters', fontsize=12)
|
| 235 |
+
plt.ylabel('Values', fontsize=12)
|
| 236 |
+
plt.title(f"{vehicle_type.title()} - Battery Parameters: Original vs Predicted", fontsize=14)
|
| 237 |
+
plt.xticks(index, numeric_features, rotation=45, ha='right')
|
| 238 |
+
plt.legend(fontsize=12)
|
| 239 |
+
plt.grid(True, alpha=0.3)
|
| 240 |
+
|
| 241 |
+
# Add value labels on bars
|
| 242 |
+
for bar in bars1:
|
| 243 |
+
height = bar.get_height()
|
| 244 |
+
plt.text(bar.get_x() + bar.get_width()/2., height,
|
| 245 |
+
f'{height:.2f}', ha='center', va='bottom', fontsize=8)
|
| 246 |
+
|
| 247 |
+
for bar in bars2:
|
| 248 |
+
height = bar.get_height()
|
| 249 |
+
plt.text(bar.get_x() + bar.get_width()/2., height,
|
| 250 |
+
f'{height:.2f}', ha='center', va='bottom', fontsize=8)
|
| 251 |
+
|
| 252 |
+
plt.tight_layout()
|
| 253 |
+
|
| 254 |
+
# Save plot
|
| 255 |
+
plot_filename = f"{uuid.uuid4().hex}.png"
|
| 256 |
+
plot_path = os.path.join("static", plot_filename)
|
| 257 |
+
plt.savefig(plot_path, dpi=100, bbox_inches='tight', facecolor='white')
|
| 258 |
+
plt.close()
|
| 259 |
+
|
| 260 |
+
print(f"Plot saved to: {plot_path}")
|
| 261 |
+
chart_url = f"/static/{plot_filename}"
|
| 262 |
+
|
| 263 |
+
except Exception as plot_error:
|
| 264 |
+
print(f"Plot generation error: {plot_error}")
|
| 265 |
+
chart_url = "/static/placeholder.png" # Use placeholder if plot fails
|
| 266 |
+
|
| 267 |
+
# Prepare table data
|
| 268 |
+
rows = []
|
| 269 |
+
for i, col in enumerate(numeric_features):
|
| 270 |
+
original_val = float(original[i])
|
| 271 |
+
predicted_val = float(prediction[i])
|
| 272 |
+
difference_val = predicted_val - original_val
|
| 273 |
+
|
| 274 |
+
rows.append({
|
| 275 |
+
"parameter": col,
|
| 276 |
+
"original": round(original_val, 4),
|
| 277 |
+
"predicted": round(predicted_val, 4),
|
| 278 |
+
"difference": round(difference_val, 4)
|
| 279 |
+
})
|
| 280 |
+
|
| 281 |
+
print("Prediction completed successfully")
|
| 282 |
+
|
| 283 |
+
return {
|
| 284 |
+
"status": "success",
|
| 285 |
+
"vehicle_type": vehicle_type,
|
| 286 |
+
"ev_model": ev_model,
|
| 287 |
+
"chart_url": chart_url,
|
| 288 |
+
"table_data": rows
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
except HTTPException:
|
| 292 |
+
raise
|
| 293 |
+
except Exception as e:
|
| 294 |
+
print(f"Prediction error: {e}")
|
| 295 |
+
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
| 296 |
+
|
| 297 |
+
@app.get("/vehicle-types")
|
| 298 |
+
async def get_vehicle_types():
|
| 299 |
+
return {"vehicle_types": list(vehicle_type_to_model.keys())}
|
| 300 |
+
|
| 301 |
+
# Add a warmup endpoint
|
| 302 |
+
@app.get("/warmup")
|
| 303 |
+
async def warmup():
|
| 304 |
+
"""Warmup endpoint to ensure models are loaded"""
|
| 305 |
+
global model, data, scaler
|
| 306 |
+
return {
|
| 307 |
+
"status": "ready",
|
| 308 |
+
"model_status": "loaded" if model is not None else "not_loaded",
|
| 309 |
+
"data_status": "loaded" if data is not None else "not_loaded",
|
| 310 |
+
"scaler_status": "loaded" if scaler is not None else "not_loaded"
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
if __name__ == "__main__":
|
| 314 |
+
import uvicorn
|
| 315 |
+
uvicorn.run(app, host="0.0.0.0", port=8000, timeout_keep_alive=120)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|
| 5 |
+
tensorflow
|
| 6 |
+
scikit-learn
|
| 7 |
+
matplotlib
|
| 8 |
+
python-multipart
|