shimaa22 commited on
Commit
c8dcc80
·
verified ·
1 Parent(s): caba272

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -46
app.py CHANGED
@@ -1,46 +1,46 @@
1
- import gradio as gr
2
- from PIL import Image
3
- import torch
4
- from pathlib import Path
5
-
6
- # ------------------------
7
- # تحميل الموديل
8
- # ------------------------
9
- MODEL_PATH = Path("best.pt")
10
- model = torch.load(MODEL_PATH, map_location="cpu")
11
- model.eval()
12
-
13
- CLASS_NAMES = ["ax", "co", "sa"]
14
-
15
- # ------------------------
16
- # prediction
17
- # ------------------------
18
- def predict_orientation(image: Image.Image):
19
- import torchvision.transforms as transforms
20
- transform = transforms.Compose([
21
- transforms.Resize((224,224)),
22
- transforms.ToTensor(),
23
- ])
24
- img_tensor = transform(image).unsqueeze(0) # batch dimension
25
-
26
- with torch.no_grad():
27
- outputs = model(img_tensor)
28
- probs = torch.softmax(outputs, dim=1)
29
- conf, pred = torch.max(probs, 1)
30
- orientation = CLASS_NAMES[pred.item()]
31
- confidence = round(conf.item(), 2)
32
-
33
- return f"Orientation: {orientation} | Confidence: {confidence}"
34
-
35
- # ------------------------
36
- # Gradio
37
- # ------------------------
38
- iface = gr.Interface(
39
- fn=predict_orientation,
40
- inputs=gr.Image(type="pil"),
41
- outputs="text",
42
- title="MRI Orientation Predictor",
43
- description="upload your image and the model output prediction and confidence"
44
- )
45
-
46
- iface.launch()
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ from pathlib import Path
5
+
6
+ # ------------------------
7
+ # تحميل الموديل
8
+ # ------------------------
9
+ MODEL_PATH = Path("best.pt")
10
+ model = torch.load(MODEL_PATH, map_location="cpu",weights_only=False)
11
+ model.eval()
12
+
13
+ CLASS_NAMES = ["ax", "co", "sa"]
14
+
15
+ # ------------------------
16
+ # prediction
17
+ # ------------------------
18
+ def predict_orientation(image: Image.Image):
19
+ import torchvision.transforms as transforms
20
+ transform = transforms.Compose([
21
+ transforms.Resize((224,224)),
22
+ transforms.ToTensor(),
23
+ ])
24
+ img_tensor = transform(image).unsqueeze(0) # batch dimension
25
+
26
+ with torch.no_grad():
27
+ outputs = model(img_tensor)
28
+ probs = torch.softmax(outputs, dim=1)
29
+ conf, pred = torch.max(probs, 1)
30
+ orientation = CLASS_NAMES[pred.item()]
31
+ confidence = round(conf.item(), 2)
32
+
33
+ return f"Orientation: {orientation} | Confidence: {confidence}"
34
+
35
+ # ------------------------
36
+ # Gradio
37
+ # ------------------------
38
+ iface = gr.Interface(
39
+ fn=predict_orientation,
40
+ inputs=gr.Image(type="pil"),
41
+ outputs="text",
42
+ title="MRI Orientation Predictor",
43
+ description="upload your image and the model output prediction and confidence"
44
+ )
45
+
46
+ iface.launch()