import gradio as gr from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor from PIL import Image import torch import numpy as np # Load processor processor = SegformerImageProcessor(do_reduce_labels=False) # Load pretrained SegFormer structure model = SegformerForSemanticSegmentation.from_pretrained( "nvidia/segformer-b0-finetuned-ade-512-512", num_labels=7, ignore_mismatched_sizes=True ) # Load trained weights from file in same directory model.load_state_dict(torch.load("trained_model.pt", map_location=torch.device("cpu"))) model.eval() # Prediction function def segment_image(input_image): inputs = processor(images=input_image, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy() normalized_mask = (pred_mask * (255 // 7)).astype(np.uint8) # 7 classes output_image = Image.fromarray(normalized_mask) # Resize mask (3x) scale_factor = 3 new_size = (output_image.width * scale_factor, output_image.height * scale_factor) bigger_output = output_image.resize(new_size, resample=Image.NEAREST) return bigger_output # Gradio Interface with submit button demo = gr.Interface( fn=segment_image, inputs=gr.Image(type="pil", label="Upload Blood Smear Image"), outputs=gr.Image(type="pil", label="Predicted Grayscale Mask"), title="Malaria Blood Smear Segmentation (Custom SegFormer)", description="Upload a blood smear image. This app uses a custom trained SegFormer model (7 malaria-specific classes) to segment it.", examples=["1.png", "2.png", "3.png"], # examples in same directory live=False ) if __name__ == "__main__": demo.launch()