import torch import torch.nn as nn import torchvision.transforms as transforms import gradio as gr from PIL import Image class ConvModel(nn.Module): def __init__(self): super().__init__() self.cnn1 = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2) ) self.cnn2 = nn.Sequential( nn.Conv2d(16, 32, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2) ) self.fc = nn.Sequential( nn.Flatten(), nn.Linear(32 * 56 * 56, 2) ) def forward(self, x): x = self.cnn1(x) x = self.cnn2(x) x = self.fc(x) return x model = ConvModel() model.load_state_dict(torch.load("conv_model.pth", map_location="cpu")) model.eval() class_names=['NORMAL', 'PNEUMONIA'] transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) def predict(img): img = transform(img).unsqueeze(0) with torch.inference_mode(): pred_probs = torch.softmax(model(img), dim=1) pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))} return pred_labels_and_probs title = "Zatürre Bulucu" description = "Gönderilen fotoğrafa göre Sağlıklı mı yoksa Zatürre mi olduğunu tahmin eder." demo = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs=[gr.Label(num_top_classes=2, label="Predictions")], title=title, description=description ) demo.launch(debug=False, share=True)