Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoImageProcessor, AutoModelForImageClassification | |
| from PIL import Image | |
| import torch | |
| # Load model and processor | |
| model_id = "sheikh987/Skin_Cancer-Image_Classification" | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| model = AutoModelForImageClassification.from_pretrained(model_id) | |
| # Prediction function | |
| def classify_image(img): | |
| inputs = processor(images=img, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| predicted_class_idx = logits.argmax(-1).item() | |
| predicted_class = model.config.id2label[predicted_class_idx] | |
| confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_idx].item() | |
| return {predicted_class: confidence} | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=classify_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Label(num_top_classes=3), | |
| title="Skin Cancer Image Classifier", | |
| description="Upload an image of skin lesion to classify." | |
| ) | |
| interface.launch() |