Aadityaramrame commited on
Commit
9018b21
ยท
verified ยท
1 Parent(s): e8d8efc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -1,28 +1,41 @@
1
  # app.py
2
  from fastapi import FastAPI, UploadFile, File
 
3
  from keras.models import load_model
 
4
  from PIL import Image
5
  import numpy as np
6
- from huggingface_hub import hf_hub_download
7
 
8
- # ๐Ÿ”น Download your model from Hugging Face Hub
9
- MODEL_PATH = hf_hub_download(repo_id="aadityaramrame/blood-cell-cancer-detector",
10
- filename="cancer_classifier.h5")
 
 
11
 
12
- # ๐Ÿ”น Load the model
13
  model = load_model(MODEL_PATH)
14
 
15
  # ๐Ÿ”น Initialize FastAPI app
16
- app = FastAPI(title="Blood Cell Cancer Detection API")
17
 
18
  @app.get("/")
19
- def root():
20
- return {"message": "API is running successfully ๐Ÿš€"}
21
 
22
  @app.post("/predict")
23
  async def predict(file: UploadFile = File(...)):
24
- image = Image.open(file.file).resize((64, 64)) # adjust size as per training
25
- img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
26
- prediction = model.predict(img_array)
27
- result = int(np.argmax(prediction))
28
- return {"prediction": result}
 
 
 
 
 
 
 
 
 
 
1
  # app.py
2
  from fastapi import FastAPI, UploadFile, File
3
+ from fastapi.responses import JSONResponse
4
  from keras.models import load_model
5
+ from huggingface_hub import hf_hub_download
6
  from PIL import Image
7
  import numpy as np
8
+ import uvicorn
9
 
10
+ # ๐Ÿ”น Download model from Hugging Face Hub
11
+ MODEL_PATH = hf_hub_download(
12
+ repo_id="aadityaramrame/blood-cell-cancer-detector",
13
+ filename="cancer_classifier.h5"
14
+ )
15
 
16
+ # ๐Ÿ”น Load the trained model
17
  model = load_model(MODEL_PATH)
18
 
19
  # ๐Ÿ”น Initialize FastAPI app
20
+ app = FastAPI(title="Cancer Detection API")
21
 
22
  @app.get("/")
23
+ async def root():
24
+ return {"message": "๐Ÿš€ Cancer Detection API is live!"}
25
 
26
  @app.post("/predict")
27
  async def predict(file: UploadFile = File(...)):
28
+ try:
29
+ image = Image.open(file.file).convert("RGB").resize((64, 64)) # use training size
30
+ img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
31
+ prediction = model.predict(img_array)
32
+ result = int(np.argmax(prediction))
33
+ return JSONResponse(content={"prediction": result})
34
+ except Exception as e:
35
+ return JSONResponse(content={"error": str(e)}, status_code=500)
36
+
37
+
38
+ # ๐Ÿ”น This line is CRUCIAL for Hugging Face Spaces
39
+ # It ensures the FastAPI app is visible when the Space starts
40
+ if __name__ == "__main__":
41
+ uvicorn.run("app:app", host="0.0.0.0", port=7860)