sheikh987 commited on
Commit
655d58b
·
verified ·
1 Parent(s): 7a83468

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -29
main.py CHANGED
@@ -1,38 +1,43 @@
1
- import gradio as gr
2
- from transformers import AutoImageProcessor, AutoModelForImageClassification
3
- from PIL import Image
4
  import torch
 
 
 
5
 
6
- # Load model and processor
 
7
  model_id = "sheikh987/Skin_Cancer-Image_Classification"
8
  processor = AutoImageProcessor.from_pretrained(model_id)
9
  model = AutoModelForImageClassification.from_pretrained(model_id)
 
 
 
 
10
 
11
- # Prediction function
12
- def classify_image(img):
13
- inputs = processor(images=img, return_tensors="pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  with torch.no_grad():
15
  outputs = model(**inputs)
 
16
  logits = outputs.logits
17
-
18
- # Get the top prediction
19
- predicted_class_idx = logits.argmax(-1).item()
20
- predicted_class = model.config.id2label[predicted_class_idx]
21
-
22
- # Get the confidence score for the top prediction
23
- confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_idx].item()
24
-
25
- # Return the data in the format Gradio's Label component expects
26
- return {predicted_class: confidence}
27
-
28
- # Gradio interface
29
- interface = gr.Interface(
30
- fn=classify_image,
31
- inputs=gr.Image(type="pil"),
32
- outputs=gr.Label(num_top_classes=5), # Show top 5 classes
33
- title="Skin Cancer Image Classifier",
34
- description="Upload an image of skin lesion to classify."
35
- )
36
-
37
- # Launch the app and the API
38
- interface.launch()
 
1
+ import io
 
 
2
  import torch
3
+ from PIL import Image
4
+ from fastapi import FastAPI, File, UploadFile, HTTPException
5
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
6
 
7
+ # Log model loading
8
+ print("🚀 Starting model download...")
9
  model_id = "sheikh987/Skin_Cancer-Image_Classification"
10
  processor = AutoImageProcessor.from_pretrained(model_id)
11
  model = AutoModelForImageClassification.from_pretrained(model_id)
12
+ print("✅ Model loaded successfully.")
13
+
14
+ # FastAPI app
15
+ app = FastAPI(title="Skin Cancer Classifier API")
16
 
17
+ # Health check endpoint
18
+ @app.get("/status")
19
+ def status():
20
+ return {"status": "ok", "model": model_id}
21
+
22
+ # Image classification endpoint
23
+ @app.post("/predict/")
24
+ async def predict(file: UploadFile = File(...)):
25
+ if not file.content_type.startswith("image/"):
26
+ raise HTTPException(status_code=400, detail="Invalid image file")
27
+
28
+ try:
29
+ image_bytes = await file.read()
30
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
31
+ except Exception:
32
+ raise HTTPException(status_code=400, detail="Could not decode image")
33
+
34
+ inputs = processor(images=image, return_tensors="pt")
35
  with torch.no_grad():
36
  outputs = model(**inputs)
37
+
38
  logits = outputs.logits
39
+ idx = logits.argmax(-1).item()
40
+ label = model.config.id2label[idx]
41
+ confidence = torch.nn.functional.softmax(logits, dim=-1)[0][idx].item()
42
+
43
+ return {"label": label, "confidence": round(confidence, 4)}