ArxAlfa commited on
Commit
9b694a7
1 Parent(s): 8ceaff8

Update model weights and Docker image

Browse files
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
- from sklearn.preprocessing import OneHotEncoder
 
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=7, hidden_size=128, output_size=1)
36
 
37
- # Initialize the OneHotEncoder
38
- encoder = OneHotEncoder(handle_unknown="ignore")
 
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("/generate/{squareFeet}/{bedrooms}/{bathrooms}/{neighborhood}/{yearBuilt}")
 
 
46
  def generate(
47
- squareFeet: float,
48
- bedrooms: float,
49
- bathrooms: float,
50
- neighborhood: str,
51
- yearBuilt: float,
 
52
  ):
53
- global model, encoder
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 = [squareFeet, bedrooms, bathrooms, *neighborhood_encoded, yearBuilt]
 
 
 
 
 
 
 
60
 
61
  input_data = torch.tensor([input_data], dtype=torch.float32)
 
62
  prediction = model(input_data)
63
- return {"output": prediction.item()}
64
 
65
 
66
  @app.post("/train")
67
- async def train(file: UploadFile = File(...)):
68
- global model, encoder
69
-
70
- contents = await file.read()
71
- data = list(csv.reader(io.StringIO(contents.decode("utf-8"))))
 
72
 
73
- data_np = np.array(data[1:], dtype=object)
 
74
 
75
- # Delete the fourth column
76
- encoded_columns = encoder.fit_transform(data_np[:, 3].reshape(-1, 1))
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
- # All columns except the last
82
- X = data_np[:, :-1]
83
 
84
- # Only the last column
85
- y = data_np[:, -1]
86
- y = np.ravel(y)
 
 
87
 
88
  # Convert data to torch tensors
89
- X = torch.tensor(X, dtype=torch.float32)
90
- y = torch.tensor(y, dtype=torch.float32)
 
 
 
 
 
 
91
 
92
  # Define loss function and optimizer
93
  criterion = nn.MSELoss()
94
- optimizer = optim.Adam(model.parameters(), lr=0.0001)
95
 
96
- # Fit the model
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
- for train_index, test_index in kf.split(X):
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
- # Backward pass and optimization
114
- loss.backward()
115
- optimizer.step()
116
 
117
- predictions = model(X_test)
118
- rmse = np.sqrt(mean_squared_error(y_test, predictions.detach().numpy()))
119
- accuracies.append(rmse)
120
 
121
- average_rmse = sum(accuracies) / len(accuracies)
122
- print(f"Epoch: {epoch+1}, Average RMSE: {average_rmse}")
 
 
 
 
 
 
123
 
124
- dump(model, "model.joblib")
125
 
126
- return {"filename": file.filename, "average_rmse": average_rmse}
 
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: prueba-ia
 
5
  ports:
6
  - "7860:7860"
7
  deploy:
8
  resources:
9
  limits:
10
- memory: 512M
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())