Aadityaramrame commited on
Commit
e8d8efc
·
verified ·
1 Parent(s): 8d2a2d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -49
app.py CHANGED
@@ -1,59 +1,28 @@
1
- from fastapi import FastAPI, Request, HTTPException
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from tensorflow.keras.models import load_model
 
4
  import numpy as np
5
- import tensorflow as tf
6
  from huggingface_hub import hf_hub_download
7
 
8
- # 🔹 Download model file from your Hugging Face repo
9
- MODEL_PATH = hf_hub_download(
10
- repo_id="aadityaramrame/blood-cell-cancer-detector", # 👈 your repo
11
- filename="cancer_classifier.h5"
12
- )
13
 
14
- # 🔹 Load model
15
  model = load_model(MODEL_PATH)
16
 
17
- # ---------- Initialize FastAPI ----------
18
- app = FastAPI(title="GenAI Health Insight API", version="2.0")
19
 
20
- # ---------- Enable CORS (so your friend’s frontend can call it) ----------
21
- app.add_middleware(
22
- CORSMiddleware,
23
- allow_origins=["*"], # Or set ["https://yourfrontend.vercel.app"] for extra safety
24
- allow_credentials=True,
25
- allow_methods=["*"],
26
- allow_headers=["*"],
27
- )
28
-
29
- # ---------- Root Endpoint ----------
30
  @app.get("/")
31
- def home():
32
- return {"message": "Welcome to GenAI Health Insight API 🚀"}
33
 
34
- # ---------- Prediction Endpoint ----------
35
  @app.post("/predict")
36
- async def predict(request: Request):
37
- """
38
- Input example (JSON):
39
- {
40
- "features": [value1, value2, ..., valueN]
41
- }
42
- """
43
- data = await request.json()
44
- input_data = data.get("features")
45
-
46
- if input_data is None:
47
- raise HTTPException(status_code=400, detail="Missing 'features' in request body")
48
-
49
- # Prepare and predict
50
- input_array = np.array(input_data).reshape(1, -1)
51
- prediction = model.predict(input_array)
52
- result = float(prediction[0][0])
53
-
54
- return {"prediction": result, "status": "success"}
55
-
56
- # ---------- Health Check Endpoint ----------
57
- @app.get("/health")
58
- def health():
59
- return {"status": "healthy"}
 
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}