GodfreyOwino commited on
Commit
7686070
·
verified ·
1 Parent(s): 99320a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -20
app.py CHANGED
@@ -2,9 +2,8 @@ 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_download
6
  import joblib
7
- import os
8
 
9
  app = FastAPI()
10
 
@@ -17,29 +16,17 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
20
- REPO_ID = "GodfreyOwino/NPK_needs_mode2"
21
  FILENAME = "npk_needs_model.joblib"
22
 
23
- # Use a directory we're sure to have write permissions to
24
- MODEL_DIR = "/tmp/model"
25
-
26
- def get_model():
27
- os.makedirs(MODEL_DIR, exist_ok=True)
28
- try:
29
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, cache_dir=MODEL_DIR)
30
- return joblib.load(model_path)
31
- except Exception as e:
32
- print(f"Error downloading/loading model: {e}")
33
- print(f"Error type: {type(e)}")
34
- print(f"Error details: {str(e)}")
35
- raise HTTPException(status_code=500, detail="Unable to download or find the model.")
36
-
37
  try:
38
- model = get_model()
39
  print("Model loaded successfully")
40
  except Exception as e:
41
- print(f"Error initializing model: {e}")
42
- raise HTTPException(status_code=500, detail="Failed to initialize the model.")
 
 
43
 
44
  class InputData(BaseModel):
45
  features: list[float]
 
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
  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]