Update app.py
Browse files
app.py
CHANGED
@@ -5,31 +5,31 @@ import torch
|
|
5 |
import numpy as np
|
6 |
|
7 |
# Load the pre-trained model and preprocessor (feature extractor)
|
8 |
-
|
9 |
-
|
10 |
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
11 |
|
12 |
def classify_image(image):
|
13 |
# Convert the PIL Image to a format compatible with the feature extractor
|
14 |
image = np.array(image)
|
15 |
# Preprocess the image and prepare it for the model
|
16 |
-
|
17 |
# Make prediction
|
18 |
with torch.no_grad():
|
19 |
-
|
20 |
-
|
21 |
# Retrieve the highest probability class label index
|
22 |
-
|
23 |
# Define a manual mapping of label indices to human-readable labels
|
24 |
-
|
25 |
0: "NORMAL",
|
26 |
1: "PNEUMONIA"
|
27 |
}
|
28 |
|
29 |
# Convert the index to the model's class label
|
30 |
-
|
31 |
|
32 |
-
return
|
33 |
|
34 |
# Create title, description and article strings
|
35 |
title = "Classification Demo"
|
@@ -43,5 +43,4 @@ iface = gr.Interface(fn=classify_image,
|
|
43 |
description=description)
|
44 |
|
45 |
# Launch the app
|
46 |
-
iface.launch()
|
47 |
-
|
|
|
5 |
import numpy as np
|
6 |
|
7 |
# Load the pre-trained model and preprocessor (feature extractor)
|
8 |
+
model_name = "runaksh/chest_xray_pneumonia_detection"
|
9 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
11 |
|
12 |
def classify_image(image):
|
13 |
# Convert the PIL Image to a format compatible with the feature extractor
|
14 |
image = np.array(image)
|
15 |
# Preprocess the image and prepare it for the model
|
16 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
17 |
# Make prediction
|
18 |
with torch.no_grad():
|
19 |
+
outputs = model(**inputs)
|
20 |
+
logits = outputs.logits
|
21 |
# Retrieve the highest probability class label index
|
22 |
+
predicted_class_idx = logits.argmax(-1).item()
|
23 |
# Define a manual mapping of label indices to human-readable labels
|
24 |
+
index_to_label = {
|
25 |
0: "NORMAL",
|
26 |
1: "PNEUMONIA"
|
27 |
}
|
28 |
|
29 |
# Convert the index to the model's class label
|
30 |
+
label = index_to_label.get(predicted_class_idx, "Unknown Label")
|
31 |
|
32 |
+
return label
|
33 |
|
34 |
# Create title, description and article strings
|
35 |
title = "Classification Demo"
|
|
|
43 |
description=description)
|
44 |
|
45 |
# Launch the app
|
46 |
+
iface.launch()
|
|