Spaces:
Runtime error
Runtime error
| import subprocess | |
| import sys | |
| # β Function to install missing packages efficiently | |
| def install(package): | |
| try: | |
| __import__(package.split("==")[0]) # Try to import package before installing | |
| except ImportError: | |
| subprocess.run([sys.executable, "-m", "pip", "install", package]) | |
| # β List of dependencies to install | |
| dependencies = [ | |
| "torch>=2.0.0", | |
| "torchvision>=0.15.0", | |
| "transformers", | |
| "gradio", | |
| "pillow", | |
| "pandas", | |
| "opencv-python-headless", | |
| "scikit-learn==1.3.0" | |
| ] | |
| # β Install dependencies | |
| for package in dependencies: | |
| install(package) | |
| import subprocess | |
| import sys | |
| # β Function to install missing packages | |
| def install(package): | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", package]) | |
| # β Ensure required libraries are installed | |
| for package in ["torch", "torchvision", "transformers", "gradio", "pillow", "pandas", "opencv-python", "scikit-learn"]: | |
| try: | |
| __import__(package) | |
| except ImportError: | |
| install(package) | |
| # β Import libraries after installation | |
| import torch | |
| import torch.nn as nn | |
| import torchvision.transforms as transforms | |
| import torchvision.models as models | |
| import pandas as pd | |
| from PIL import Image | |
| import gradio as gr | |
| from sklearn.preprocessing import LabelEncoder | |
| # β Load metadata | |
| CSV_PATH = "HAM10000_metadata.csv" | |
| DATA_PATH = "ham10000_images/" | |
| df = pd.read_csv(CSV_PATH) | |
| label_encoder = LabelEncoder() | |
| df["label"] = label_encoder.fit_transform(df["dx"]) # Convert labels to numbers | |
| classes = label_encoder.classes_ # Get class names | |
| # β Define image transformation | |
| transform = transforms.Compose([ | |
| transforms.Resize((224, 224)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) | |
| ]) | |
| # β Load a pre-trained EfficientNet model | |
| model = models.efficientnet_b0(pretrained=True) | |
| num_features = model.classifier[1].in_features | |
| model.classifier[1] = nn.Linear(num_features, len(classes)) # Adjust for 7 classes | |
| model.load_state_dict(torch.load("ham10000_model.pth", map_location=torch.device('cpu'))) | |
| model.eval() | |
| # β Function to classify skin disease | |
| def classify_skin_disease(image): | |
| image = Image.fromarray(image) # Convert to PIL image | |
| image = transform(image).unsqueeze(0) # Apply transformations | |
| with torch.no_grad(): | |
| output = model(image) | |
| probabilities = torch.nn.functional.softmax(output[0], dim=0) | |
| # Convert probabilities to dictionary | |
| results = {classes[i]: f"{probabilities[i].item():.2%}" for i in range(len(classes))} | |
| return results | |
| # β Create Gradio Interface | |
| iface = gr.Interface( | |
| fn=classify_skin_disease, | |
| inputs=gr.Image(type="numpy"), | |
| outputs=gr.Label(num_top_classes=3), | |
| title="π©Ί AI Skin Disease Classifier", | |
| description="π· Upload a skin lesion image and the model will classify it.", | |
| examples=["example_eczema.jpg", "example_melanoma.jpg"], # Add sample images | |
| ) | |
| # β Run Gradio App | |
| if __name__ == "__main__": | |
| iface.launch() |