shimaa22 commited on
Commit
a9c292c
·
verified ·
1 Parent(s): 8b80314

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -22
app.py CHANGED
@@ -1,46 +1,34 @@
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()
 
1
  import gradio as gr
2
  from PIL import Image
3
+ from ultralytics import YOLO
4
  import torch
 
5
 
6
  # ------------------------
7
+ MODEL_PATH = "best.pt"
8
+ model = YOLO(MODEL_PATH)
 
 
 
 
9
  CLASS_NAMES = ["ax", "co", "sa"]
10
 
11
  # ------------------------
12
  # prediction
13
  # ------------------------
14
  def predict_orientation(image: Image.Image):
15
+ results = model.predict(source=image, imgsz=224, device="cpu")
16
+ probs = results[0].probs
17
+ pred = torch.argmax(probs)
18
+ orientation = CLASS_NAMES[pred.item()]
19
+ confidence = round(probs[pred].item(), 2)
 
 
 
 
 
 
 
 
20
 
21
  return f"Orientation: {orientation} | Confidence: {confidence}"
22
 
23
  # ------------------------
24
+ # Gradio Interface
25
  # ------------------------
26
  iface = gr.Interface(
27
  fn=predict_orientation,
28
  inputs=gr.Image(type="pil"),
29
  outputs="text",
30
  title="MRI Orientation Predictor",
31
+ description="Upload your image and the model outputs prediction and confidence."
32
  )
33
 
34
  iface.launch()