File size: 1,141 Bytes
f131230
 
 
 
a7e404e
f131230
 
 
 
a7e404e
 
 
f131230
 
 
 
 
 
 
 
 
 
a7e404e
f131230
 
a7e404e
f131230
eec3199
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import torch
from torchvision import transforms
from PIL import Image
from transformers import SwinForImageClassification, AutoImageProcessor
import os

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load image processor
processor = AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224")

# Load model
model = SwinForImageClassification.from_pretrained(
    "microsoft/swin-tiny-patch4-window7-224",
    num_labels=2,
    ignore_mismatched_sizes=True
)
model.load_state_dict(torch.load("model/oral_cancer_swin_new.pth", map_location=device))
model.to(device)
model.eval()

labels = ["Cancer", "Non-Cancer"]

def predict(image):
    inputs = processor(images=image, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
        pred = torch.argmax(outputs.logits, dim=1).item()
    return labels[pred]

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Oral Cancer Detection",
    description="Upload a tongue image to detect whether it shows signs of Cancer or not."
)

demo.launch()