ArxAlfa
commited on
Commit
•
9b694a7
1
Parent(s):
8ceaff8
Update model weights and Docker image
Browse files- app.py +71 -61
- dataset/agricultural_yield_test.csv +0 -0
- dataset/agricultural_yield_train.csv +0 -0
- dataset/housing_price_dataset.csv +0 -0
- docker-compose.yml +3 -3
- model.joblib +0 -0
- model_weights.pth +0 -0
- requirements.txt +1 -0
- test.py +5 -0
app.py
CHANGED
@@ -3,13 +3,13 @@ import torch.nn as nn
|
|
3 |
import torch.optim as optim
|
4 |
import numpy as np
|
5 |
from fastapi import FastAPI, UploadFile, File
|
6 |
-
from sklearn.model_selection import KFold
|
7 |
from sklearn.metrics import mean_squared_error
|
8 |
-
|
|
|
9 |
import csv
|
10 |
import io
|
11 |
|
12 |
-
from joblib import load, dump
|
13 |
|
14 |
|
15 |
# Define the DNN model
|
@@ -32,95 +32,105 @@ class DNN(nn.Module):
|
|
32 |
|
33 |
|
34 |
# Load the model
|
35 |
-
model = DNN(input_size=
|
36 |
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
# Create a new FastAPI app instance
|
41 |
app = FastAPI(docs_url="/", redoc_url="/new_redoc")
|
42 |
|
43 |
|
44 |
# Create a POST endpoint
|
45 |
-
@app.get(
|
|
|
|
|
46 |
def generate(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
):
|
53 |
-
global model
|
54 |
-
|
55 |
-
# Apply the encoder to the neighborhood input
|
56 |
-
neighborhood_encoded = encoder.transform([[neighborhood]]).toarray()[0]
|
57 |
|
58 |
# Combine all inputs
|
59 |
-
input_data = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
input_data = torch.tensor([input_data], dtype=torch.float32)
|
|
|
62 |
prediction = model(input_data)
|
63 |
-
return {"
|
64 |
|
65 |
|
66 |
@app.post("/train")
|
67 |
-
async def train(
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
72 |
|
73 |
-
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
data_np = np.delete(data_np, 3, axis=1)
|
78 |
-
data_np = np.concatenate((data_np, encoded_columns.toarray()), axis=1)
|
79 |
-
data_np = np.array(data_np, dtype=float)
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
|
84 |
-
#
|
85 |
-
|
86 |
-
|
|
|
|
|
87 |
|
88 |
# Convert data to torch tensors
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Define loss function and optimizer
|
93 |
criterion = nn.MSELoss()
|
94 |
-
optimizer = optim.Adam(model.parameters(), lr=0.
|
95 |
|
96 |
-
|
97 |
-
kf = KFold(n_splits=4)
|
98 |
-
accuracies = []
|
99 |
-
|
100 |
-
epochs = 25 # Define the number of epochs
|
101 |
|
102 |
for epoch in range(epochs):
|
103 |
-
|
104 |
-
X_train, X_test = X[train_index], X[test_index]
|
105 |
-
y_train, y_test = y[train_index], y[test_index]
|
106 |
-
|
107 |
-
optimizer.zero_grad()
|
108 |
-
|
109 |
-
# Forward pass
|
110 |
-
outputs = model(X_train)
|
111 |
-
loss = criterion(outputs, y_train.unsqueeze(1))
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
|
125 |
|
126 |
-
return {"
|
|
|
3 |
import torch.optim as optim
|
4 |
import numpy as np
|
5 |
from fastapi import FastAPI, UploadFile, File
|
|
|
6 |
from sklearn.metrics import mean_squared_error
|
7 |
+
import pandas as pd
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
import csv
|
10 |
import io
|
11 |
|
12 |
+
# from joblib import load, dump
|
13 |
|
14 |
|
15 |
# Define the DNN model
|
|
|
32 |
|
33 |
|
34 |
# Load the model
|
35 |
+
model = DNN(input_size=6, hidden_size=256, output_size=1)
|
36 |
|
37 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
38 |
+
model = model.to(device)
|
39 |
+
model.load_state_dict(torch.load("model_weights.pth", map_location=device))
|
40 |
|
41 |
# Create a new FastAPI app instance
|
42 |
app = FastAPI(docs_url="/", redoc_url="/new_redoc")
|
43 |
|
44 |
|
45 |
# Create a POST endpoint
|
46 |
+
@app.get(
|
47 |
+
"/generate/{Soil_Quality}/{Seed_Variety}/{Fertilizer_Amount_kg_per_hectare}/{Sunny_Days}/{Rainfall_mm}/{Irrigation_Schedule}"
|
48 |
+
)
|
49 |
def generate(
|
50 |
+
Soil_Quality: float,
|
51 |
+
Seed_Variety: float,
|
52 |
+
Fertilizer_Amount_kg_per_hectare: float,
|
53 |
+
Sunny_Days: float,
|
54 |
+
Rainfall_mm: float,
|
55 |
+
Irrigation_Schedule: float,
|
56 |
):
|
57 |
+
global model
|
|
|
|
|
|
|
58 |
|
59 |
# Combine all inputs
|
60 |
+
input_data = [
|
61 |
+
Soil_Quality,
|
62 |
+
Seed_Variety,
|
63 |
+
Fertilizer_Amount_kg_per_hectare,
|
64 |
+
Sunny_Days,
|
65 |
+
Rainfall_mm,
|
66 |
+
Irrigation_Schedule,
|
67 |
+
]
|
68 |
|
69 |
input_data = torch.tensor([input_data], dtype=torch.float32)
|
70 |
+
input_data = input_data.to(device)
|
71 |
prediction = model(input_data)
|
72 |
+
return {"prediction": prediction.item()}
|
73 |
|
74 |
|
75 |
@app.post("/train")
|
76 |
+
async def train(
|
77 |
+
trainDatafile: UploadFile = File(...),
|
78 |
+
testDatafile: UploadFile = File(...),
|
79 |
+
epochs: int = 100,
|
80 |
+
):
|
81 |
+
global model
|
82 |
|
83 |
+
contents1 = await trainDatafile.read()
|
84 |
+
train_data = pd.read_csv(io.StringIO(contents1.decode("utf-8")))
|
85 |
|
86 |
+
contents2 = await testDatafile.read()
|
87 |
+
test_data = pd.read_csv(io.StringIO(contents2.decode("utf-8")))
|
|
|
|
|
|
|
88 |
|
89 |
+
# Load the training and testing data
|
90 |
+
# test_data = pd.read_csv("dataset/agricultural_yield_test.csv")
|
91 |
|
92 |
+
# Convert data to numpy arrays
|
93 |
+
X_train = train_data.drop("Yield_kg_per_hectare", axis=1).values
|
94 |
+
y_train = train_data["Yield_kg_per_hectare"].values
|
95 |
+
X_test = test_data.drop("Yield_kg_per_hectare", axis=1).values
|
96 |
+
y_test = test_data["Yield_kg_per_hectare"].values
|
97 |
|
98 |
# Convert data to torch tensors
|
99 |
+
X_train = torch.tensor(X_train, dtype=torch.float32)
|
100 |
+
X_train = X_train.to(device)
|
101 |
+
y_train = torch.tensor(y_train, dtype=torch.float32)
|
102 |
+
y_train = y_train.to(device)
|
103 |
+
|
104 |
+
X_test = torch.tensor(X_test, dtype=torch.float32)
|
105 |
+
X_test = X_test.to(device)
|
106 |
+
y_test = torch.tensor(y_test, dtype=torch.float32)
|
107 |
|
108 |
# Define loss function and optimizer
|
109 |
criterion = nn.MSELoss()
|
110 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
111 |
|
112 |
+
rmseList = []
|
|
|
|
|
|
|
|
|
113 |
|
114 |
for epoch in range(epochs):
|
115 |
+
optimizer.zero_grad()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
+
# Forward pass
|
118 |
+
outputs = model(X_train)
|
119 |
+
loss = criterion(outputs, y_train.unsqueeze(1))
|
120 |
|
121 |
+
# Backward pass and optimization
|
122 |
+
loss.backward()
|
123 |
+
optimizer.step()
|
124 |
|
125 |
+
predictions = model(X_test)
|
126 |
+
rmse = np.sqrt(
|
127 |
+
mean_squared_error(
|
128 |
+
y_test.cpu().detach().numpy(), predictions.cpu().detach().numpy()
|
129 |
+
)
|
130 |
+
)
|
131 |
+
print(f"Epoch: {epoch+1}, RMSE: {float(rmse)}")
|
132 |
+
rmseList.append(float(rmse))
|
133 |
|
134 |
+
torch.save(model.state_dict(), "model_weights.pth")
|
135 |
|
136 |
+
return {"rmse": rmseList}
|
dataset/agricultural_yield_test.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
dataset/agricultural_yield_train.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
dataset/housing_price_dataset.csv
DELETED
The diff for this file is too large to render.
See raw diff
|
|
docker-compose.yml
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
version: '3.8'
|
2 |
services:
|
3 |
your-service:
|
4 |
-
image:
|
|
|
5 |
ports:
|
6 |
- "7860:7860"
|
7 |
deploy:
|
8 |
resources:
|
9 |
limits:
|
10 |
-
memory:
|
11 |
-
shm_size: 2G
|
12 |
reservations:
|
13 |
devices:
|
14 |
- driver: nvidia
|
|
|
1 |
version: '3.8'
|
2 |
services:
|
3 |
your-service:
|
4 |
+
image: mi-imagen
|
5 |
+
shm_size: '2gb'
|
6 |
ports:
|
7 |
- "7860:7860"
|
8 |
deploy:
|
9 |
resources:
|
10 |
limits:
|
11 |
+
memory: 2G
|
|
|
12 |
reservations:
|
13 |
devices:
|
14 |
- driver: nvidia
|
model.joblib
DELETED
Binary file (8.19 kB)
|
|
model_weights.pth
ADDED
Binary file (274 kB). View file
|
|
requirements.txt
CHANGED
@@ -29,3 +29,4 @@ uvloop==0.19.0
|
|
29 |
watchgod==0.8.2
|
30 |
websockets==12.0
|
31 |
torch
|
|
|
|
29 |
watchgod==0.8.2
|
30 |
websockets==12.0
|
31 |
torch
|
32 |
+
pandas
|
test.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
test_data = pd.read_csv("dataset/agricultural_yield_test.csv")
|
4 |
+
|
5 |
+
print(test_data.head())
|