Aadityaramrame commited on
Commit
8aa6871
·
verified ·
1 Parent(s): ecb9c77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -31
app.py CHANGED
@@ -1,40 +1,52 @@
1
- import gradio as gr
2
- import tensorflow as tf
 
3
  import numpy as np
4
- from PIL import Image
5
 
6
- # Load the trained model
7
- model = tf.keras.models.load_model("cancer_classifier.h5")
 
8
 
9
- # Define the class names (make sure they match your dataset)
10
- class_names = ['Basophil', 'Eosinophil', 'Erythroblast', 'IG', 'lymphocyte', 'Monocyte', 'Neutrophil', 'Platelet']
11
 
12
- # Define prediction function
13
- def predict(image):
14
- image = image.resize((224, 224))
15
- img_array = np.array(image) / 255.0
16
- img_array = np.expand_dims(img_array, axis=0)
 
 
 
17
 
18
- prediction = model.predict(img_array)
19
- class_index = np.argmax(prediction)
20
- confidence = float(np.max(prediction))
 
21
 
22
- return {
23
- "Predicted Class": class_names[class_index],
24
- "Confidence": round(confidence * 100, 2)
 
 
 
 
25
  }
 
 
 
26
 
27
- # Gradio interface
28
- iface = gr.Interface(
29
- fn=predict,
30
- inputs=gr.Image(type="pil", label="Upload Blood Cell Image"),
31
- outputs=[
32
- gr.Label(label="Prediction"),
33
- ],
34
- title="🧬 Blood Cell Cancer Classifier",
35
- description="Upload a blood cell image to classify whether it's cancerous or not using a fine-tuned EfficientNetB3 model.",
36
- theme="gradio/soft"
37
- )
38
 
39
- if __name__ == "__main__":
40
- iface.launch()
 
 
 
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
 
6
+ # ---------- Load the Trained Model ----------
7
+ MODEL_PATH = "cancer_classifier.h5"
8
+ model = load_model(MODEL_PATH)
9
 
10
+ # ---------- Initialize FastAPI ----------
11
+ app = FastAPI(title="GenAI Health Insight API", version="2.0")
12
 
13
+ # ---------- Enable CORS (so your friend’s frontend can call it) ----------
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # Or set ["https://yourfrontend.vercel.app"] for extra safety
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
 
22
+ # ---------- Root Endpoint ----------
23
+ @app.get("/")
24
+ def home():
25
+ return {"message": "Welcome to GenAI Health Insight API 🚀"}
26
 
27
+ # ---------- Prediction Endpoint ----------
28
+ @app.post("/predict")
29
+ async def predict(request: Request):
30
+ """
31
+ Input example (JSON):
32
+ {
33
+ "features": [value1, value2, ..., valueN]
34
  }
35
+ """
36
+ data = await request.json()
37
+ input_data = data.get("features")
38
 
39
+ if input_data is None:
40
+ raise HTTPException(status_code=400, detail="Missing 'features' in request body")
41
+
42
+ # Prepare and predict
43
+ input_array = np.array(input_data).reshape(1, -1)
44
+ prediction = model.predict(input_array)
45
+ result = float(prediction[0][0])
46
+
47
+ return {"prediction": result, "status": "success"}
 
 
48
 
49
+ # ---------- Health Check Endpoint ----------
50
+ @app.get("/health")
51
+ def health():
52
+ return {"status": "healthy"}