runaksh commited on
Commit
ebd0a25
·
verified ·
1 Parent(s): d684065

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -12
app.py CHANGED
@@ -5,9 +5,7 @@ import torch
5
  import numpy as np
6
 
7
  # Load the pre-trained model and preprocessor (feature extractor)
8
- model_name_pneumonia = "runaksh/chest_xray_pneumonia_detection"
9
  model_name_tuberculosis = "runaksh/chest_xray_tuberculosis_detection"
10
- model_pneumonia = ViTForImageClassification.from_pretrained(model_name_pneumonia)
11
  model_tuberculosis = ViTForImageClassification.from_pretrained(model_name_tuberculosis)
12
  feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
13
 
@@ -15,31 +13,24 @@ def classify_image(image):
15
  # Convert the PIL Image to a format compatible with the feature extractor
16
  image = np.array(image)
17
  # Preprocess the image and prepare it for the model
18
- inputs_pneumonia = feature_extractor(images=image, return_tensors="pt")
19
  inputs_tuberculosis = feature_extractor(images=image, return_tensors="pt")
20
  # Make prediction
21
  with torch.no_grad():
22
- outputs_pneumonia = model_pneumonia(**inputs_pneumonia)
23
- logits_pneumonia = outputs_pneumonia.logits
24
  outputs_tuberculosis = model_tuberculosis(**inputs_tuberculosis)
25
  logits_tuberculosis = outputs_tuberculosis.logits
26
  # Retrieve the highest probability class label index
27
- predicted_class_idx_pneumonia = logits_pneumonia.argmax(-1).item()
28
  predicted_class_idx_tuberculosis = logits_tuberculosis.argmax(-1).item()
29
  # Define a manual mapping of label indices to human-readable labels
30
- index_to_label_pneumonia = {0: "Pneumonia = NO",1: "Pneumonia = YES"}
31
  index_to_label_tuberculosis = {0: "Tuberculosis = NO",1: "Tuberculosis = YES"}
32
  # Convert the index to the model's class label
33
- label_pneumonia = index_to_label_pneumonia.get(predicted_class_idx_pneumonia, "Unknown Label")
34
  label_tuberculosis = index_to_label_tuberculosis.get(predicted_class_idx_tuberculosis, "Unknown Label")
35
- label = label_pneumonia+".................."+label_tuberculosis
36
 
37
- return label
38
 
39
 
40
  # Create title, description and article strings
41
- title = "Automated Classification of Pneumonia and Tuberculosis using Machine Learning"
42
- description = "Upload your lungs Radiograph to find out if you are having Pneumonia or Tuberculosis"
43
 
44
  css_code = ".gradio-container {background: url(https://actionmenshealth.com/wp-content/uploads/2018/05/heart.jpg); background-size: cover;}"
45
 
 
5
  import numpy as np
6
 
7
  # Load the pre-trained model and preprocessor (feature extractor)
 
8
  model_name_tuberculosis = "runaksh/chest_xray_tuberculosis_detection"
 
9
  model_tuberculosis = ViTForImageClassification.from_pretrained(model_name_tuberculosis)
10
  feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
11
 
 
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_tuberculosis = feature_extractor(images=image, return_tensors="pt")
17
  # Make prediction
18
  with torch.no_grad():
 
 
19
  outputs_tuberculosis = model_tuberculosis(**inputs_tuberculosis)
20
  logits_tuberculosis = outputs_tuberculosis.logits
21
  # Retrieve the highest probability class label index
 
22
  predicted_class_idx_tuberculosis = logits_tuberculosis.argmax(-1).item()
23
  # Define a manual mapping of label indices to human-readable labels
 
24
  index_to_label_tuberculosis = {0: "Tuberculosis = NO",1: "Tuberculosis = YES"}
25
  # Convert the index to the model's class label
 
26
  label_tuberculosis = index_to_label_tuberculosis.get(predicted_class_idx_tuberculosis, "Unknown Label")
 
27
 
28
+ return label_tuberculosis
29
 
30
 
31
  # Create title, description and article strings
32
+ title = "Automated Classification of Tuberculosis using Machine Learning"
33
+ description = "Upload your lungs Radiograph to find out if you are having Tuberculosis"
34
 
35
  css_code = ".gradio-container {background: url(https://actionmenshealth.com/wp-content/uploads/2018/05/heart.jpg); background-size: cover;}"
36