Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch
|
|
| 3 |
import torch.nn as nn
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
class AddModel(nn.Module):
|
| 8 |
def __init__(self):
|
| 9 |
super(AddModel, self).__init__()
|
|
@@ -19,31 +19,28 @@ class AddModel(nn.Module):
|
|
| 19 |
x = self.fc3(x)
|
| 20 |
return x
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def load_model(model_path):
|
| 24 |
model = AddModel()
|
| 25 |
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 26 |
model.eval() # Set the model to evaluation mode
|
| 27 |
return model
|
| 28 |
|
| 29 |
-
#
|
| 30 |
def predict_sum(model, x1, x2):
|
| 31 |
with torch.no_grad():
|
| 32 |
input_tensor = torch.tensor([[x1, x2]], dtype=torch.float32)
|
| 33 |
prediction = model(input_tensor)
|
| 34 |
return prediction.item()
|
| 35 |
|
| 36 |
-
# Streamlit app
|
| 37 |
def main():
|
| 38 |
st.title("Sum Predictor using Neural Network")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
model_path = "./models/best_model.pth" # Update with your model path
|
| 42 |
if os.path.exists(model_path):
|
| 43 |
model = load_model(model_path)
|
| 44 |
st.success("Model loaded successfully.")
|
| 45 |
|
| 46 |
-
# User input for prediction
|
| 47 |
x1 = st.number_input("Enter the first number:", value=0.0)
|
| 48 |
x2 = st.number_input("Enter the second number:", value=0.0)
|
| 49 |
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# the model architecture
|
| 7 |
class AddModel(nn.Module):
|
| 8 |
def __init__(self):
|
| 9 |
super(AddModel, self).__init__()
|
|
|
|
| 19 |
x = self.fc3(x)
|
| 20 |
return x
|
| 21 |
|
| 22 |
+
# load the model from a specified path
|
| 23 |
def load_model(model_path):
|
| 24 |
model = AddModel()
|
| 25 |
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 26 |
model.eval() # Set the model to evaluation mode
|
| 27 |
return model
|
| 28 |
|
| 29 |
+
#predictions
|
| 30 |
def predict_sum(model, x1, x2):
|
| 31 |
with torch.no_grad():
|
| 32 |
input_tensor = torch.tensor([[x1, x2]], dtype=torch.float32)
|
| 33 |
prediction = model(input_tensor)
|
| 34 |
return prediction.item()
|
| 35 |
|
|
|
|
| 36 |
def main():
|
| 37 |
st.title("Sum Predictor using Neural Network")
|
| 38 |
+
|
| 39 |
+
model_path = "./models/MA1T.pth"
|
|
|
|
| 40 |
if os.path.exists(model_path):
|
| 41 |
model = load_model(model_path)
|
| 42 |
st.success("Model loaded successfully.")
|
| 43 |
|
|
|
|
| 44 |
x1 = st.number_input("Enter the first number:", value=0.0)
|
| 45 |
x2 = st.number_input("Enter the second number:", value=0.0)
|
| 46 |
|