# import requests # import json # import os # # Your Hugging Face model URL # API_URL = "sayyedAhmed/Crisis_Severity_Predictor_LSTM" # Replace with your model's URL # # Load your Hugging Face API token # API_KEY = os.getenv("HF_API_KEY") # Ensure the API key is stored in the environment or replace with the actual key # headers = { # "Authorization": f"Bearer {API_KEY}", # "Content-Type": "application/json" # } # payload = { # "inputs": "Your test input here" # Replace this with the actual input for your model # } # # Make the POST request to Hugging Face Inference API # response = requests.post(API_URL, headers=headers, json=payload) # # Print the response (the predictions) # print(response.json()) import torch import numpy as np # Define the model architecture (this should match the one used during training) class LSTMPredictor(torch.nn.Module): def __init__(self, input_dim, hidden_dim, output_dim, forecast_horizon, n_layers, dropout): super(LSTMPredictor, self).__init__() self.lstm = torch.nn.LSTM(input_dim, hidden_dim, n_layers, dropout=dropout, batch_first=True) self.fc = torch.nn.Linear(hidden_dim, output_dim) self.forecast_horizon = forecast_horizon def forward(self, x): # Forward pass through LSTM lstm_out, _ = self.lstm(x) # Only get the output from the last time step out = self.fc(lstm_out[:, -1, :]) return out # Load the model def load_model(model_path): model_state = torch.load(model_path) model = LSTMPredictor( input_dim=model_state['model_architecture']['input_dim'], hidden_dim=model_state['model_architecture']['hidden_dim'], output_dim=model_state['model_architecture']['output_dim'], forecast_horizon=model_state['model_architecture']['forecast_horizon'], n_layers=model_state['model_architecture']['n_layers'], dropout=model_state['model_architecture']['dropout'] ) model.load_state_dict(model_state['model_state_dict']) model.eval() # Set model to evaluation mode return model # Inference function def predict(model, features): # Convert input features to tensor input_tensor = torch.FloatTensor(features) # Get model prediction with torch.no_grad(): predictions = model(input_tensor).numpy() # No gradients needed for inference return predictions.tolist() # Main function to load the model and make predictions if __name__ == "__main__": # Load the trained model model = load_model('lstm_crisis_severity_predictor_20241116_092126.pt') # Replace with actual model path # Example input data (features) - Replace with actual features test_features = np.array([[1.23, 4.56, 7.89, 10.11]]) # Example test features # Reshape for LSTM: (batch_size, seq_len, input_dim) test_features = test_features.reshape((test_features.shape[0], 1, test_features.shape[1])) # Get predictions predictions = predict(model, test_features) # Output predictions print("Predictions:", predictions)