Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import os
|
|
| 10 |
|
| 11 |
app = FastAPI(title="PulmoProbe AI API")
|
| 12 |
|
| 13 |
-
# Add CORS middleware to allow your
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
@@ -19,18 +19,21 @@ app.add_middleware(
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# --- Set
|
| 23 |
-
|
| 24 |
-
os.environ['HF_HOME'] =
|
| 25 |
-
print("
|
| 26 |
|
| 27 |
# --- Download and Load Model from Hugging Face Hub ---
|
| 28 |
-
# This points to your model repository
|
| 29 |
MODEL_REPO_ID = "costaspinto/PulmoProbe"
|
| 30 |
MODEL_FILENAME = "best_model.joblib"
|
| 31 |
|
| 32 |
print("Downloading model from Hugging Face Hub...")
|
| 33 |
-
model_path = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
model = joblib.load(model_path)
|
| 35 |
print("Model loaded successfully.")
|
| 36 |
|
|
@@ -50,7 +53,7 @@ class PatientData(BaseModel):
|
|
| 50 |
other_cancer: int
|
| 51 |
treatment_type: str
|
| 52 |
|
| 53 |
-
# ---
|
| 54 |
@app.get("/")
|
| 55 |
def read_root():
|
| 56 |
return {"message": "Welcome to the PulmoProbe AI API"}
|
|
@@ -65,7 +68,7 @@ def predict(data: PatientData):
|
|
| 65 |
|
| 66 |
return {
|
| 67 |
"risk": risk_level,
|
| 68 |
-
"confidence": f"{confidence_high_risk * 100:.1f}"
|
| 69 |
}
|
| 70 |
except Exception as e:
|
| 71 |
-
return {"error": str(e)}
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI(title="PulmoProbe AI API")
|
| 12 |
|
| 13 |
+
# Add CORS middleware to allow your frontend app to call the API
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# --- Set Hugging Face cache to a writable location ---
|
| 23 |
+
os.environ['HF_HOME'] = '/tmp/huggingface'
|
| 24 |
+
os.makedirs(os.environ['HF_HOME'], exist_ok=True)
|
| 25 |
+
print(f"HF_HOME set to {os.environ['HF_HOME']}")
|
| 26 |
|
| 27 |
# --- Download and Load Model from Hugging Face Hub ---
|
|
|
|
| 28 |
MODEL_REPO_ID = "costaspinto/PulmoProbe"
|
| 29 |
MODEL_FILENAME = "best_model.joblib"
|
| 30 |
|
| 31 |
print("Downloading model from Hugging Face Hub...")
|
| 32 |
+
model_path = hf_hub_download(
|
| 33 |
+
repo_id=MODEL_REPO_ID,
|
| 34 |
+
filename=MODEL_FILENAME,
|
| 35 |
+
cache_dir=os.environ['HF_HOME'] # ensures container-safe caching
|
| 36 |
+
)
|
| 37 |
model = joblib.load(model_path)
|
| 38 |
print("Model loaded successfully.")
|
| 39 |
|
|
|
|
| 53 |
other_cancer: int
|
| 54 |
treatment_type: str
|
| 55 |
|
| 56 |
+
# --- API Endpoints ---
|
| 57 |
@app.get("/")
|
| 58 |
def read_root():
|
| 59 |
return {"message": "Welcome to the PulmoProbe AI API"}
|
|
|
|
| 68 |
|
| 69 |
return {
|
| 70 |
"risk": risk_level,
|
| 71 |
+
"confidence": f"{confidence_high_risk * 100:.1f}%"
|
| 72 |
}
|
| 73 |
except Exception as e:
|
| 74 |
+
return {"error": str(e)}
|