Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,37 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import numpy as np
|
4 |
-
from huggingface_hub import
|
5 |
import joblib
|
6 |
-
import os
|
7 |
-
from datetime import datetime, timedelta
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
remote_mtime = remote_info.lastModified
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
if os.path.exists(cached_path):
|
24 |
-
local_mtime = datetime.fromtimestamp(os.path.getmtime(cached_path))
|
25 |
-
|
26 |
-
if datetime.now() - local_mtime < UPDATE_FREQUENCY:
|
27 |
-
print("Using cached model (checked recently)")
|
28 |
-
return joblib.load(cached_path)
|
29 |
-
|
30 |
-
if remote_mtime > local_mtime:
|
31 |
-
print("Downloading updated model")
|
32 |
-
cached_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, force_download=True)
|
33 |
-
else:
|
34 |
-
print("Cached model is up-to-date")
|
35 |
-
else:
|
36 |
-
print("Downloading model for the first time")
|
37 |
-
cached_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
38 |
-
|
39 |
-
except Exception as e:
|
40 |
-
print(f"Error checking/downloading model: {e}")
|
41 |
-
print(f"Error type: {type(e)}")
|
42 |
-
print(f"Error details: {str(e)}")
|
43 |
-
raise HTTPException(status_code=500, detail="Unable to download or find the model.")
|
44 |
-
|
45 |
-
return joblib.load(cached_path)
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
class InputData(BaseModel):
|
51 |
features: list[float]
|
52 |
|
53 |
-
@app.post("/predict")
|
54 |
async def predict(data: InputData):
|
55 |
try:
|
56 |
input_data = np.array(data.features).reshape(1, -1)
|
@@ -59,6 +40,6 @@ async def predict(data: InputData):
|
|
59 |
except Exception as e:
|
60 |
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
61 |
|
62 |
-
@app.get("/")
|
63 |
async def root():
|
64 |
return {"message": "NPK Needs Prediction Model API"}
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from pydantic import BaseModel
|
4 |
import numpy as np
|
5 |
+
from huggingface_hub import hf_hub_url, cached_download
|
6 |
import joblib
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# Add CORS middleware
|
11 |
+
app.add_middleware(
|
12 |
+
CORSMiddleware,
|
13 |
+
allow_origins=["*"],
|
14 |
+
allow_credentials=True,
|
15 |
+
allow_methods=["*"],
|
16 |
+
allow_headers=["*"],
|
17 |
+
)
|
|
|
18 |
|
19 |
+
REPO_ID = "GodfreyOwino/NPK_needs_mode2" # Replace with your actual repo name
|
20 |
+
FILENAME = "npk_needs_model.joblib"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
try:
|
23 |
+
model = joblib.load(cached_download(hf_hub_url(REPO_ID, FILENAME)))
|
24 |
+
print("Model loaded successfully")
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Error loading model: {e}")
|
27 |
+
print(f"Error type: {type(e)}")
|
28 |
+
print(f"Error details: {str(e)}")
|
29 |
+
raise HTTPException(status_code=500, detail="Unable to download or find the model.")
|
30 |
|
31 |
class InputData(BaseModel):
|
32 |
features: list[float]
|
33 |
|
34 |
+
@app.post("/predict", tags=["Prediction"])
|
35 |
async def predict(data: InputData):
|
36 |
try:
|
37 |
input_data = np.array(data.features).reshape(1, -1)
|
|
|
40 |
except Exception as e:
|
41 |
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
42 |
|
43 |
+
@app.get("/", tags=["Root"])
|
44 |
async def root():
|
45 |
return {"message": "NPK Needs Prediction Model API"}
|