Bhuvanesh24 commited on
Commit ·
2a200cd
1
Parent(s): 145227f
Model pipeline
Browse files- app.py +19 -28
- src/__pycache__/model.cpython-311.pyc +0 -0
app.py
CHANGED
|
@@ -1,65 +1,56 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
-
import json # Import json for safer parsing
|
| 5 |
from src.model import LSTM # Adjust to your model path
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 9 |
-
model_path = "./water_forecast_2.pth"
|
| 10 |
model = LSTM(input_size=8, lstm_layer_sizes=[128, 128, 128], output_size=3).to(device)
|
| 11 |
model.load_state_dict(torch.load(model_path, map_location=device, weights_only=True))
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
# Define the prediction function
|
| 15 |
def predict_water_usage(state_idx, target_year, structured_data):
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
structured_data = json.loads(structured_data) if isinstance(structured_data, str) else structured_data
|
| 19 |
-
except json.JSONDecodeError:
|
| 20 |
-
return {"error": "Invalid JSON format for structured data."}
|
| 21 |
-
|
| 22 |
-
if state_idx not in structured_data or len(structured_data[state_idx]) < 5:
|
| 23 |
return {"error": "Structured data must include 5 years of data for the specified state."}
|
| 24 |
|
| 25 |
# Convert structured data for model input (extract values for model)
|
| 26 |
-
data_values = [list(values) for
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Ensure the data has the right shape for the model
|
| 29 |
if len(data_values) != 5: # Check if there are exactly 5 years of data
|
| 30 |
return {"error": "Structured data should have 5 years of data."}
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
tensor_data = torch.tensor(data_values, dtype=torch.float32).to(device)
|
| 33 |
-
|
| 34 |
# Get model output
|
| 35 |
with torch.no_grad():
|
| 36 |
output = model(tensor_data)
|
| 37 |
-
|
| 38 |
return {"prediction": output.tolist()}
|
| 39 |
|
| 40 |
# Configure Gradio interface
|
| 41 |
inputs = [
|
| 42 |
gr.Number(label="State Index"), # Numeric input for state index
|
| 43 |
gr.Number(label="Target Year"), # Numeric input for target year
|
| 44 |
-
gr.
|
| 45 |
-
label="Structured Data (JSON format)",
|
| 46 |
-
lines=10,
|
| 47 |
-
placeholder="""{
|
| 48 |
-
"state_idx": {
|
| 49 |
-
"2020": [value1, value2, ..., value8],
|
| 50 |
-
"2021": [value1, value2, ..., value8],
|
| 51 |
-
"2022": [value1, value2, ..., value8],
|
| 52 |
-
"2023": [value1, value2, ..., value8],
|
| 53 |
-
"2024": [value1, value2, ..., value8]
|
| 54 |
-
}
|
| 55 |
-
}"""
|
| 56 |
-
)
|
| 57 |
]
|
| 58 |
|
| 59 |
outputs = gr.JSON(label="Prediction")
|
| 60 |
|
|
|
|
| 61 |
interface = gr.Interface(fn=predict_water_usage, inputs=inputs, outputs=outputs)
|
| 62 |
|
| 63 |
-
# Launch
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
interface.launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from src.model import LSTM # Adjust to your model path
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 8 |
+
model_path = "./water_forecast_2.pth" # Path to the model file
|
| 9 |
model = LSTM(input_size=8, lstm_layer_sizes=[128, 128, 128], output_size=3).to(device)
|
| 10 |
model.load_state_dict(torch.load(model_path, map_location=device, weights_only=True))
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
# Define the prediction function
|
| 14 |
def predict_water_usage(state_idx, target_year, structured_data):
|
| 15 |
+
# structured_data is now a dictionary directly, no need to parse it
|
| 16 |
+
if len(structured_data) < 5:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return {"error": "Structured data must include 5 years of data for the specified state."}
|
| 18 |
|
| 19 |
# Convert structured data for model input (extract values for model)
|
| 20 |
+
data_values = [list(values) for values in structured_data.values()]
|
| 21 |
+
print("Structured Data:", structured_data)
|
| 22 |
+
print("Data Values:", data_values)
|
| 23 |
|
| 24 |
# Ensure the data has the right shape for the model
|
| 25 |
if len(data_values) != 5: # Check if there are exactly 5 years of data
|
| 26 |
return {"error": "Structured data should have 5 years of data."}
|
| 27 |
+
|
| 28 |
+
# Check if data_values contains only numeric data
|
| 29 |
+
for year_data in data_values:
|
| 30 |
+
if not all(isinstance(val, (int, float)) for val in year_data):
|
| 31 |
+
return {"error": "All values in structured data should be numeric."}
|
| 32 |
+
|
| 33 |
+
# Convert data_values to tensor
|
| 34 |
tensor_data = torch.tensor(data_values, dtype=torch.float32).to(device)
|
| 35 |
+
|
| 36 |
# Get model output
|
| 37 |
with torch.no_grad():
|
| 38 |
output = model(tensor_data)
|
| 39 |
+
|
| 40 |
return {"prediction": output.tolist()}
|
| 41 |
|
| 42 |
# Configure Gradio interface
|
| 43 |
inputs = [
|
| 44 |
gr.Number(label="State Index"), # Numeric input for state index
|
| 45 |
gr.Number(label="Target Year"), # Numeric input for target year
|
| 46 |
+
gr.JSON(label="Structured Data") # JSON input for structured data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
]
|
| 48 |
|
| 49 |
outputs = gr.JSON(label="Prediction")
|
| 50 |
|
| 51 |
+
# Set up the Gradio Interface
|
| 52 |
interface = gr.Interface(fn=predict_water_usage, inputs=inputs, outputs=outputs)
|
| 53 |
|
| 54 |
+
# Launch Gradio
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
interface.launch(show_error=True)
|
src/__pycache__/model.cpython-311.pyc
ADDED
|
Binary file (5.56 kB). View file
|
|
|