GodfreyOwino commited on
Commit
982fa2c
·
verified ·
1 Parent(s): 7686070

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -2,8 +2,9 @@ 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
 
@@ -16,17 +17,19 @@ app.add_middleware(
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]
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  import numpy as np
 
5
  import joblib
6
+ import requests
7
+ from io import BytesIO
8
 
9
  app = FastAPI()
10
 
 
17
  allow_headers=["*"],
18
  )
19
 
20
+ MODEL_URL = "https://huggingface.co/GodfreyOwino/NPK_needs_mode2/resolve/main/npk_needs_model.joblib"
 
21
 
22
  try:
23
+ response = requests.get(MODEL_URL)
24
+ response.raise_for_status()
25
+ model_bytes = BytesIO(response.content)
26
+ model = joblib.load(model_bytes)
27
  print("Model loaded successfully")
28
  except Exception as e:
29
  print(f"Error loading model: {e}")
30
  print(f"Error type: {type(e)}")
31
  print(f"Error details: {str(e)}")
32
+ raise HTTPException(status_code=500, detail="Unable to load the model.")
33
 
34
  class InputData(BaseModel):
35
  features: list[float]