dhhd255 commited on
Commit
36f8da9
·
1 Parent(s): 41b1fab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -5,23 +5,30 @@ from PIL import Image
5
 
6
  # Authenticate and download the EfficientNet model from Hugging Face Spaces
7
  fs = HfFileSystem()
8
- model_path = 'dhhd255/efficientnet_b3/efficientnet_b3.pt'
9
- with fs.open(model_path, 'rb') as f:
10
- model_content = f.read()
11
 
12
  # Save the EfficientNet model file to disk
13
  efficientnet_model_file = 'efficientnet_b3.pt'
14
  with open(efficientnet_model_file, 'wb') as f:
15
- f.write(model_content)
16
 
17
- # Load the EfficientNet model onto the CPU
18
- model = EfficientNet.from_pretrained('efficientnet-b3')
19
- model.load_state_dict(torch.load(efficientnet_model_file, map_location=torch.device('cpu')))
 
20
 
21
- # Load your custom model onto the CPU
22
  custom_model_file = 'best_model.pth'
23
- model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
 
24
 
 
 
 
 
 
25
  model.eval()
26
 
27
  # Define a function that takes an image as input and uses the model for inference
@@ -48,16 +55,16 @@ def image_classifier(image):
48
  # Return the result
49
  return outputs[0].numpy(), predicted_label
50
 
51
- # Create a Streamlit app with an image upload input
52
- uploaded_file = st.file_uploader('Upload an image')
53
- if uploaded_file is not None:
54
- # Convert the UploadedFile object to a NumPy array
55
- image = Image.open(uploaded_file)
56
- image = np.array(image)
57
-
58
- # Use the image for inference
59
- predictions, predicted_label = image_classifier(image)
60
-
61
- # Display the result
62
- st.write(f'Predictions: {predictions}')
63
- st.write(f'Predicted label: {predicted_label}')
 
5
 
6
  # Authenticate and download the EfficientNet model from Hugging Face Spaces
7
  fs = HfFileSystem()
8
+ efficientnet_model_path = 'dhhd255/efficientnet_b3/efficientnet_b3.pt'
9
+ with fs.open(efficientnet_model_path, 'rb') as f:
10
+ efficientnet_model_content = f.read()
11
 
12
  # Save the EfficientNet model file to disk
13
  efficientnet_model_file = 'efficientnet_b3.pt'
14
  with open(efficientnet_model_file, 'wb') as f:
15
+ f.write(efficientnet_model_content)
16
 
17
+ # Authenticate and download your custom model from Hugging Face Spaces
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
+ # Save your custom model file to disk
23
  custom_model_file = 'best_model.pth'
24
+ with open(custom_model_file, 'wb') as f:
25
+ f.write(custom_model_content)
26
 
27
+ # Load the EfficientNet model onto the CPU
28
+ model = torch.load(efficientnet_model_file)
29
+
30
+ # Load your custom model onto the CPU
31
+ model.load_state_dict(torch.load(custom_model_file))
32
  model.eval()
33
 
34
  # Define a function that takes an image as input and uses the model for inference
 
55
  # Return the result
56
  return outputs[0].numpy(), predicted_label
57
 
58
+ # Load and preprocess the image
59
+ img_path = '/content/test_image_healthy.png'
60
+ img = Image.open(img_path)
61
+ img = data_transform(img)
62
+
63
+ # Add a batch dimension and move the image to the device
64
+ img = img.unsqueeze(0).to(device)
65
+
66
+ # Perform inference
67
+ with torch.no_grad():
68
+ outputs = model(img)
69
+ _, predicted = torch.max(outputs.data, 1)
70
+ print(f'Predicted class: {predicted.item()}')