Spaces:
Sleeping
Sleeping
UnknownKeek@2005 commited on
Commit ·
14fc358
1
Parent(s): 0ea5695
Commit 3
Browse files- Dockerfile +9 -5
- app.py +20 -13
Dockerfile
CHANGED
|
@@ -5,18 +5,22 @@ FROM python:3.9-slim
|
|
| 5 |
|
| 6 |
WORKDIR /app
|
| 7 |
|
|
|
|
| 8 |
COPY requirements.txt requirements.txt
|
| 9 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 10 |
|
| 11 |
-
#
|
| 12 |
COPY data.csv .
|
| 13 |
-
RUN ls -la # Debug: List files to confirm data.csv exists
|
| 14 |
|
| 15 |
-
# Then copy the rest of the files
|
| 16 |
COPY . .
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
RUN
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Use Uvicorn server with appropriate logging
|
| 22 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug"]
|
|
|
|
| 5 |
|
| 6 |
WORKDIR /app
|
| 7 |
|
| 8 |
+
# Install required packages
|
| 9 |
COPY requirements.txt requirements.txt
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
|
| 12 |
+
# Copy data file first
|
| 13 |
COPY data.csv .
|
|
|
|
| 14 |
|
| 15 |
+
# Then copy the rest of the application files
|
| 16 |
COPY . .
|
| 17 |
|
| 18 |
+
# Create a directory for model storage with proper permissions
|
| 19 |
+
RUN mkdir -p /tmp/models && \
|
| 20 |
+
chmod 777 /tmp/models
|
| 21 |
+
|
| 22 |
+
# Debug: List all files and permissions
|
| 23 |
+
RUN ls -la && ls -la /tmp/models
|
| 24 |
|
| 25 |
# Use Uvicorn server with appropriate logging
|
| 26 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug"]
|
app.py
CHANGED
|
@@ -14,6 +14,13 @@ app = FastAPI(
|
|
| 14 |
version="1.0.0"
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Global variables
|
| 18 |
data = None
|
| 19 |
model = None
|
|
@@ -27,11 +34,11 @@ features = ['Average Annual Rainfall (inches)', 'Soil Suitability (0 to 1)',
|
|
| 27 |
try:
|
| 28 |
if os.path.exists('data.csv'):
|
| 29 |
data = pd.read_csv('data.csv')
|
| 30 |
-
if os.path.exists(
|
| 31 |
-
with open(
|
| 32 |
model = pickle.load(f)
|
| 33 |
-
if os.path.exists(
|
| 34 |
-
with open(
|
| 35 |
scaler = pickle.load(f)
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error loading data or model: {e}")
|
|
@@ -165,7 +172,7 @@ async def startup_event():
|
|
| 165 |
return
|
| 166 |
|
| 167 |
# Check if model and scaler need to be created
|
| 168 |
-
if not os.path.exists(
|
| 169 |
print("Training model and creating necessary files...")
|
| 170 |
|
| 171 |
try:
|
|
@@ -222,11 +229,11 @@ async def startup_event():
|
|
| 222 |
|
| 223 |
model.fit(X_train_scaled, y_train)
|
| 224 |
|
| 225 |
-
print("Saving model and scaler...")
|
| 226 |
-
# Save the model and scaler
|
| 227 |
-
with open(
|
| 228 |
pickle.dump(model, f)
|
| 229 |
-
with open(
|
| 230 |
pickle.dump(scaler, f)
|
| 231 |
|
| 232 |
print("Model and scaler saved successfully!")
|
|
@@ -239,17 +246,17 @@ async def startup_event():
|
|
| 239 |
# Load model and scaler if they exist but weren't loaded
|
| 240 |
if model is None:
|
| 241 |
try:
|
| 242 |
-
with open(
|
| 243 |
model = pickle.load(f)
|
| 244 |
-
print("Successfully loaded existing model")
|
| 245 |
except Exception as e:
|
| 246 |
print(f"Failed to load model: {e}")
|
| 247 |
|
| 248 |
if scaler is None:
|
| 249 |
try:
|
| 250 |
-
with open(
|
| 251 |
scaler = pickle.load(f)
|
| 252 |
-
print("Successfully loaded existing scaler")
|
| 253 |
except Exception as e:
|
| 254 |
print(f"Failed to load scaler: {e}")
|
| 255 |
|
|
|
|
| 14 |
version="1.0.0"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Create a models directory if it doesn't exist
|
| 18 |
+
os.makedirs('/tmp/models', exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# Define model paths using the temporary directory
|
| 21 |
+
MODEL_PATH = '/tmp/models/model.pkl'
|
| 22 |
+
SCALER_PATH = '/tmp/models/scaler.pkl'
|
| 23 |
+
|
| 24 |
# Global variables
|
| 25 |
data = None
|
| 26 |
model = None
|
|
|
|
| 34 |
try:
|
| 35 |
if os.path.exists('data.csv'):
|
| 36 |
data = pd.read_csv('data.csv')
|
| 37 |
+
if os.path.exists(MODEL_PATH):
|
| 38 |
+
with open(MODEL_PATH, 'rb') as f:
|
| 39 |
model = pickle.load(f)
|
| 40 |
+
if os.path.exists(SCALER_PATH):
|
| 41 |
+
with open(SCALER_PATH, 'rb') as f:
|
| 42 |
scaler = pickle.load(f)
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Error loading data or model: {e}")
|
|
|
|
| 172 |
return
|
| 173 |
|
| 174 |
# Check if model and scaler need to be created
|
| 175 |
+
if not os.path.exists(MODEL_PATH) or not os.path.exists(SCALER_PATH):
|
| 176 |
print("Training model and creating necessary files...")
|
| 177 |
|
| 178 |
try:
|
|
|
|
| 229 |
|
| 230 |
model.fit(X_train_scaled, y_train)
|
| 231 |
|
| 232 |
+
print(f"Saving model to {MODEL_PATH} and scaler to {SCALER_PATH}...")
|
| 233 |
+
# Save the model and scaler to the temporary directory
|
| 234 |
+
with open(MODEL_PATH, 'wb') as f:
|
| 235 |
pickle.dump(model, f)
|
| 236 |
+
with open(SCALER_PATH, 'wb') as f:
|
| 237 |
pickle.dump(scaler, f)
|
| 238 |
|
| 239 |
print("Model and scaler saved successfully!")
|
|
|
|
| 246 |
# Load model and scaler if they exist but weren't loaded
|
| 247 |
if model is None:
|
| 248 |
try:
|
| 249 |
+
with open(MODEL_PATH, 'rb') as f:
|
| 250 |
model = pickle.load(f)
|
| 251 |
+
print(f"Successfully loaded existing model from {MODEL_PATH}")
|
| 252 |
except Exception as e:
|
| 253 |
print(f"Failed to load model: {e}")
|
| 254 |
|
| 255 |
if scaler is None:
|
| 256 |
try:
|
| 257 |
+
with open(SCALER_PATH, 'rb') as f:
|
| 258 |
scaler = pickle.load(f)
|
| 259 |
+
print(f"Successfully loaded existing scaler from {SCALER_PATH}")
|
| 260 |
except Exception as e:
|
| 261 |
print(f"Failed to load scaler: {e}")
|
| 262 |
|