dhhd255 commited on
Commit
b3ad1b5
·
1 Parent(s): 8ee7dc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
app.py CHANGED
@@ -1,43 +1,63 @@
 
1
  import torch
2
  from torchvision import transforms
3
- from huggingface_hub import HfApi, HfFolder
4
  from PIL import Image
5
 
6
  # Authenticate and download the EfficientNet model from Hugging Face
7
- api = HfApi()
8
- efficientnet_model_url = api.presigned_url('dhhd255/efficientnet_b3', filename='efficientnet_b3.pt').geturl()
9
- efficientnet_model_file = HfFolder.download_file(efficientnet_model_url, cache_dir=HfFolder.cache_dir())
10
-
11
- # Authenticate and download your custom model from Hugging Face
12
- custom_model_url = api.presigned_url('dhhd255/efficient_net_parkinsons', filename='best_model.pth').geturl()
13
- custom_model_file = HfFolder.download_file(custom_model_url, cache_dir=HfFolder.cache_dir())
14
 
15
  # Load the EfficientNet model onto the CPU
 
16
  model = torch.load(efficientnet_model_file, map_location=torch.device('cpu'))
17
 
18
- # Load the saved weights onto the CPU
19
- model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
 
 
20
 
21
- # Set the model to evaluation mode
 
 
22
  model.eval()
23
 
24
- # Define the image transform for inference
25
- data_transform = transforms.Compose([
26
- transforms.Lambda(lambda x: x.convert('RGB')),
27
- transforms.Resize((224, 224)),
28
- transforms.ToTensor()
29
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Load and preprocess the image
32
  img_path = '/content/test_image_healthy.png'
33
  img = Image.open(img_path)
34
  img = data_transform(img)
35
 
36
- # Add a batch dimension
37
  img = img.unsqueeze(0)
38
 
39
  # Perform inference
40
  with torch.no_grad():
41
- outputs = model(img)
42
- _, predicted = torch.max(outputs.data, 1)
43
- print(f'Predicted class: {predicted.item()}')
 
1
+ import io
2
  import torch
3
  from torchvision import transforms
4
+ from huggingface_hub import HfFileSystem
5
  from PIL import Image
6
 
7
  # Authenticate and download the EfficientNet model from Hugging Face
8
+ fs = HfFileSystem()
9
+ efficientnet_model_path = 'dhhd255/efficientnet_b3/efficientnet_b3.pt'
10
+ with fs.open(efficientnet_model_path, 'rb') as f:
11
+ efficientnet_model_content = f.read()
 
 
 
12
 
13
  # Load the EfficientNet model onto the CPU
14
+ efficientnet_model_file = io.BytesIO(efficientnet_model_content)
15
  model = torch.load(efficientnet_model_file, map_location=torch.device('cpu'))
16
 
17
+ # Authenticate and download your custom model from Hugging Face
18
+ custom_model_path = 'dhhd255/efficient_net_parkinsons/best_model.pth'
19
+ with fs.open(custom_model_path, 'rb') as f:
20
+ custom_model_content = f.read()
21
 
22
+ # Load your custom model onto the CPU
23
+ custom_model_file = io.BytesIO(custom_model_content)
24
+ model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
25
  model.eval()
26
 
27
+ # Define a function that takes an image as input and uses the model for inference
28
+ def image_classifier(image):
29
+ # Preprocess the input image
30
+ data_transform = transforms.Compose([
31
+ transforms.Lambda(lambda x: x.convert('RGB')),
32
+ transforms.Resize((224, 224)),
33
+ transforms.ToTensor()
34
+ ])
35
+ image = Image.fromarray(image)
36
+ image = data_transform(image)
37
+ image = image.unsqueeze(0)
38
+
39
+ # Use your custom model for inference
40
+ with torch.no_grad():
41
+ outputs = model(image)
42
+ _, predicted = torch.max(outputs.data, 1)
43
+
44
+ # Map the index to a class label
45
+ labels = ['Healthy', 'Parkinson']
46
+ predicted_label = labels[predicted.item()]
47
+
48
+ # Return the result
49
+ return outputs[0].numpy(), predicted_label
50
 
51
  # Load and preprocess the image
52
  img_path = '/content/test_image_healthy.png'
53
  img = Image.open(img_path)
54
  img = data_transform(img)
55
 
56
+ # Add a batch dimension and move the image to the device
57
  img = img.unsqueeze(0)
58
 
59
  # Perform inference
60
  with torch.no_grad():
61
+ outputs = model(img)
62
+ _, predicted = torch.max(outputs.data, 1)
63
+ print(f'Predicted class: {predicted.item()}')