sayyedAhmed
commited on
Commit
•
a885b39
1
Parent(s):
265cf85
file added and deleted
Browse files- app.py +0 -87
- inference.py +79 -18
- requirements.txt +2 -1
app.py
DELETED
@@ -1,87 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import torch
|
3 |
-
import numpy as np
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import seaborn as sns
|
6 |
-
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
|
7 |
-
import joblib
|
8 |
-
|
9 |
-
# Load the model and scaler
|
10 |
-
def load_model(model_path, scaler_path):
|
11 |
-
model_state = torch.load(model_path)
|
12 |
-
model = LSTMPredictor(
|
13 |
-
input_dim=model_state['model_architecture']['input_dim'],
|
14 |
-
hidden_dim=model_state['model_architecture']['hidden_dim'],
|
15 |
-
output_dim=model_state['model_architecture']['output_dim'],
|
16 |
-
forecast_horizon=model_state['model_architecture']['forecast_horizon'],
|
17 |
-
n_layers=model_state['model_architecture']['n_layers'],
|
18 |
-
dropout=model_state['model_architecture']['dropout']
|
19 |
-
)
|
20 |
-
model.load_state_dict(model_state['model_state_dict'])
|
21 |
-
scaler = joblib.load(scaler_path)
|
22 |
-
return model, scaler
|
23 |
-
|
24 |
-
# Prepare Streamlit interface
|
25 |
-
st.title("Crisis Severity Prediction Model Evaluation")
|
26 |
-
st.sidebar.title("Model Evaluation Dashboard")
|
27 |
-
|
28 |
-
# Upload model and scaler files
|
29 |
-
model_file = st.sidebar.file_uploader("Upload Trained Model", type=["pth", "pt"])
|
30 |
-
scaler_file = st.sidebar.file_uploader("Upload Scaler File", type=["pkl"])
|
31 |
-
|
32 |
-
if model_file and scaler_file:
|
33 |
-
# Load model and scaler
|
34 |
-
model, scaler = load_model(model_file, scaler_file)
|
35 |
-
|
36 |
-
# Example of how to prepare test data (adjust for actual data)
|
37 |
-
X_test = np.array([[...]]) # Test data input
|
38 |
-
y_test = np.array([[...]]) # Actual target values
|
39 |
-
|
40 |
-
# Scale the test data using the loaded scaler
|
41 |
-
scaled_X_test = scaler.transform(X_test)
|
42 |
-
|
43 |
-
# Convert to tensor
|
44 |
-
X_test_tensor = torch.FloatTensor(scaled_X_test)
|
45 |
-
|
46 |
-
# Get predictions
|
47 |
-
model.eval()
|
48 |
-
with torch.no_grad():
|
49 |
-
predictions = model(X_test_tensor)
|
50 |
-
|
51 |
-
# Evaluate and display metrics (using first 3 months for example)
|
52 |
-
y_true = y_test
|
53 |
-
y_pred = predictions.numpy()
|
54 |
-
|
55 |
-
metrics = {}
|
56 |
-
for month in range(3): # Assuming forecast for 3 months
|
57 |
-
month_metrics = {
|
58 |
-
'mse': mean_squared_error(y_true[:, month], y_pred[:, month]),
|
59 |
-
'rmse': np.sqrt(mean_squared_error(y_true[:, month], y_pred[:, month])),
|
60 |
-
'mae': mean_absolute_error(y_true[:, month], y_pred[:, month]),
|
61 |
-
'r2': r2_score(y_true[:, month], y_pred[:, month])
|
62 |
-
}
|
63 |
-
metrics[f'month_{month+1}'] = month_metrics
|
64 |
-
|
65 |
-
# Display metrics
|
66 |
-
st.subheader("Model Performance Metrics:")
|
67 |
-
for month, month_metrics in metrics.items():
|
68 |
-
st.write(f"{month.upper()}:")
|
69 |
-
for metric_name, metric_value in month_metrics.items():
|
70 |
-
st.write(f"{metric_name.upper()}: {metric_value:.4f}")
|
71 |
-
|
72 |
-
# Visualization (actual vs predicted)
|
73 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
74 |
-
for month in range(3):
|
75 |
-
ax.scatter(y_true[:, month], y_pred[:, month], alpha=0.5, label=f'Month {month+1}')
|
76 |
-
ax.plot([0, 5], [0, 5], 'r--')
|
77 |
-
ax.set_xlabel('Actual Severity Index')
|
78 |
-
ax.set_ylabel('Predicted Severity Index')
|
79 |
-
ax.set_title('Actual vs Predicted')
|
80 |
-
ax.legend()
|
81 |
-
|
82 |
-
st.pyplot(fig)
|
83 |
-
|
84 |
-
# Option to download plot
|
85 |
-
st.download_button("Download Evaluation Plot", "evaluation_plot.png")
|
86 |
-
else:
|
87 |
-
st.warning("Please upload both the trained model and scaler files.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inference.py
CHANGED
@@ -1,24 +1,85 @@
|
|
1 |
-
import requests
|
2 |
-
import json
|
3 |
-
import os
|
4 |
|
5 |
-
# Your Hugging Face model URL
|
6 |
-
API_URL = "sayyedAhmed/Crisis_Severity_Predictor_LSTM" # Replace with your model's URL
|
7 |
|
8 |
-
# Load your Hugging Face API token
|
9 |
-
API_KEY = os.getenv("HF_API_KEY") # Ensure the API key is stored in the environment or replace with the actual key
|
10 |
|
11 |
-
headers = {
|
12 |
-
|
13 |
-
|
14 |
-
}
|
15 |
|
16 |
-
payload = {
|
17 |
-
|
18 |
-
}
|
19 |
|
20 |
-
# Make the POST request to Hugging Face Inference API
|
21 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
# Print the response (the predictions)
|
24 |
-
print(response.json())
|
|
|
1 |
+
# import requests
|
2 |
+
# import json
|
3 |
+
# import os
|
4 |
|
5 |
+
# # Your Hugging Face model URL
|
6 |
+
# API_URL = "sayyedAhmed/Crisis_Severity_Predictor_LSTM" # Replace with your model's URL
|
7 |
|
8 |
+
# # Load your Hugging Face API token
|
9 |
+
# API_KEY = os.getenv("HF_API_KEY") # Ensure the API key is stored in the environment or replace with the actual key
|
10 |
|
11 |
+
# headers = {
|
12 |
+
# "Authorization": f"Bearer {API_KEY}",
|
13 |
+
# "Content-Type": "application/json"
|
14 |
+
# }
|
15 |
|
16 |
+
# payload = {
|
17 |
+
# "inputs": "Your test input here" # Replace this with the actual input for your model
|
18 |
+
# }
|
19 |
|
20 |
+
# # Make the POST request to Hugging Face Inference API
|
21 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
22 |
+
|
23 |
+
# # Print the response (the predictions)
|
24 |
+
# print(response.json())
|
25 |
+
import torch
|
26 |
+
import numpy as np
|
27 |
+
|
28 |
+
# Define the model architecture (this should match the one used during training)
|
29 |
+
class LSTMPredictor(torch.nn.Module):
|
30 |
+
def __init__(self, input_dim, hidden_dim, output_dim, forecast_horizon, n_layers, dropout):
|
31 |
+
super(LSTMPredictor, self).__init__()
|
32 |
+
self.lstm = torch.nn.LSTM(input_dim, hidden_dim, n_layers, dropout=dropout, batch_first=True)
|
33 |
+
self.fc = torch.nn.Linear(hidden_dim, output_dim)
|
34 |
+
self.forecast_horizon = forecast_horizon
|
35 |
+
|
36 |
+
def forward(self, x):
|
37 |
+
# Forward pass through LSTM
|
38 |
+
lstm_out, _ = self.lstm(x)
|
39 |
+
# Only get the output from the last time step
|
40 |
+
out = self.fc(lstm_out[:, -1, :])
|
41 |
+
return out
|
42 |
+
|
43 |
+
# Load the model
|
44 |
+
def load_model(model_path):
|
45 |
+
model_state = torch.load(model_path)
|
46 |
+
model = LSTMPredictor(
|
47 |
+
input_dim=model_state['model_architecture']['input_dim'],
|
48 |
+
hidden_dim=model_state['model_architecture']['hidden_dim'],
|
49 |
+
output_dim=model_state['model_architecture']['output_dim'],
|
50 |
+
forecast_horizon=model_state['model_architecture']['forecast_horizon'],
|
51 |
+
n_layers=model_state['model_architecture']['n_layers'],
|
52 |
+
dropout=model_state['model_architecture']['dropout']
|
53 |
+
)
|
54 |
+
model.load_state_dict(model_state['model_state_dict'])
|
55 |
+
model.eval() # Set model to evaluation mode
|
56 |
+
return model
|
57 |
+
|
58 |
+
# Inference function
|
59 |
+
def predict(model, features):
|
60 |
+
# Convert input features to tensor
|
61 |
+
input_tensor = torch.FloatTensor(features)
|
62 |
+
|
63 |
+
# Get model prediction
|
64 |
+
with torch.no_grad():
|
65 |
+
predictions = model(input_tensor).numpy() # No gradients needed for inference
|
66 |
+
|
67 |
+
return predictions.tolist()
|
68 |
+
|
69 |
+
# Main function to load the model and make predictions
|
70 |
+
if __name__ == "__main__":
|
71 |
+
# Load the trained model
|
72 |
+
model = load_model('lstm_crisis_severity_predictor_20241116_092126.pt') # Replace with actual model path
|
73 |
+
|
74 |
+
# Example input data (features) - Replace with actual features
|
75 |
+
test_features = np.array([[1.23, 4.56, 7.89, 10.11]]) # Example test features
|
76 |
+
|
77 |
+
# Reshape for LSTM: (batch_size, seq_len, input_dim)
|
78 |
+
test_features = test_features.reshape((test_features.shape[0], 1, test_features.shape[1]))
|
79 |
+
|
80 |
+
# Get predictions
|
81 |
+
predictions = predict(model, test_features)
|
82 |
+
|
83 |
+
# Output predictions
|
84 |
+
print("Predictions:", predictions)
|
85 |
|
|
|
|
requirements.txt
CHANGED
@@ -23,4 +23,5 @@ python-dotenv
|
|
23 |
pytest
|
24 |
joblib
|
25 |
plotly
|
26 |
-
requests
|
|
|
|
23 |
pytest
|
24 |
joblib
|
25 |
plotly
|
26 |
+
requests
|
27 |
+
streamlit
|