mirsaid5455 commited on
Commit
e4421be
1 Parent(s): 64b756b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -29
app.py CHANGED
@@ -1,48 +1,34 @@
1
  import streamlit as st
2
- import torch
3
  from PIL import Image
4
  import numpy as np
5
  import pathlib
 
6
 
7
- # Load the PyTorch model
8
- model_path = "model.pkl"
9
- model = torch.load(model_path, map_location=torch.device('cpu'))
10
- model.eval()
11
 
12
- # Load the Fastai X-ray model
13
- xray_model_path = "xraydet.pkl"
14
- xray_model = torch.load(xray_model_path, map_location=torch.device('cpu'))
15
- xray_model.eval()
16
 
17
  def process_image(image):
18
  try:
19
  img = image.resize((224, 224))
20
  img_fastai = Image.fromarray(np.array(img))
21
-
22
- # Preprocess image for the Fastai model
23
- tfms = setup_aug_tfms([Resize(460)], mode='center')
24
- img_fastai = PILImage.create(img_fastai).apply_tfms(tfms).resize((224, 224)).convert('RGB')
25
- img_fastai = ToTensor()(img_fastai).unsqueeze(0)
26
-
27
- # Predict using the X-ray model
28
- pred_xray, _, _ = xray_model.predict(img_fastai)
29
 
30
  if pred_xray == '1':
31
- # Predict using the main model
32
- with torch.no_grad():
33
- pred = model(img_fastai)
34
- pred_id = torch.argmax(pred, dim=1).item()
35
- probs = torch.softmax(pred, dim=1).numpy()[0]
36
- labels = ['NORMAL', 'PNEUMONIA']
37
- pred_label = labels[pred_id]
38
 
39
- return pred_label, pred_id, probs
40
  else:
41
- pred_label, pred_id, probs = -1, -1, -1
42
- return pred_label, pred_id, probs
43
  except Exception:
44
- pred_label, pred_id, probs = -1, -1, -1
45
- return pred_label, pred_id, probs
46
 
47
  # Main function to run the Streamlit app
48
  def main():
 
1
  import streamlit as st
2
+ from fastai.vision.all import *
3
  from PIL import Image
4
  import numpy as np
5
  import pathlib
6
+ import plotly as px
7
 
8
+ # Temporary fix for pathlib.PosixPath on Windows
9
+ temp = pathlib.PosixPath
10
+ pathlib.PosixPath = pathlib.WindowsPath
 
11
 
12
+ # Load the Fastai Learner model
13
+ learn = load_learner('model.pkl')
14
+ learn_xray = load_learner("xraydet.pkl")
 
15
 
16
  def process_image(image):
17
  try:
18
  img = image.resize((224, 224))
19
  img_fastai = Image.fromarray(np.array(img))
20
+ pred_xray, _, _ = learn_xray.predict(image)
 
 
 
 
 
 
 
21
 
22
  if pred_xray == '1':
23
+ pred, pred_id, probs = learn.predict(img_fastai)
 
 
 
 
 
 
24
 
25
+ return pred, pred_id, probs
26
  else:
27
+ pred, pred_id, probs = -1,-1,-1
28
+ return pred, pred_id, probs
29
  except Exception:
30
+ pred, pred_id, probs = -1,-1,-1
31
+ return pred, pred_id, probs
32
 
33
  # Main function to run the Streamlit app
34
  def main():