rishabh5752 commited on
Commit
efb2d12
1 Parent(s): 4ec4ec3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
+ from torchvision import models, transforms
5
+
6
+ # Load the pre-trained model
7
+ model = models.densenet121(pretrained=True)
8
+ model.eval()
9
+
10
+ # Define the image transformations
11
+ transform = transforms.Compose([
12
+ transforms.Resize(256),
13
+ transforms.CenterCrop(224),
14
+ transforms.ToTensor(),
15
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
16
+ ])
17
+
18
+ # Define the class labels
19
+ class_labels = ['Normal', 'Pneumonia']
20
+
21
+ # Create a function to make predictions
22
+ def predict(image):
23
+ # Preprocess the image
24
+ image = transform(image).unsqueeze(0)
25
+
26
+ # Make the prediction
27
+ with torch.no_grad():
28
+ output = model(image)
29
+ _, predicted_idx = torch.max(output, 1)
30
+ predicted_label = class_labels[predicted_idx.item()]
31
+
32
+ return predicted_label
33
+
34
+ # Create the Streamlit app
35
+ def main():
36
+ st.title("Pneumonia Detection")
37
+ st.write("Upload an image and the app will predict if it has pneumonia or not.")
38
+
39
+ # Upload and display the image
40
+ uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
41
+
42
+ if uploaded_image is not None:
43
+ image = Image.open(uploaded_image)
44
+ st.image(image, caption="Uploaded Image", use_column_width=True)
45
+
46
+ # Make a prediction
47
+ predicted_label = predict(image)
48
+ st.write("Prediction:", predicted_label)
49
+
50
+ # Run the app
51
+ if __name__ == '__main__':
52
+ main()