dhhd255 commited on
Commit
e3e1e9b
·
1 Parent(s): cb5705d

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -0
main.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModel
3
+ from PIL import Image
4
+ import numpy as np
5
+ import streamlit as st
6
+
7
+ # Set the device
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+
10
+ # Load the trained model from the Hugging Face Hub
11
+ model = AutoModel.from_pretrained('dhhd255/parkinsons_pred12')
12
+
13
+ # Move the model to the device
14
+ model = model.to(device)
15
+
16
+ # Use Streamlit to upload an image
17
+ uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
18
+ if uploaded_file is not None:
19
+ # Load and resize the image
20
+ image_size = (224, 224)
21
+ new_image = Image.open(uploaded_file).convert('RGB').resize(image_size)
22
+ new_image = np.array(new_image)
23
+ new_image = torch.from_numpy(new_image).transpose(0, 2).float().unsqueeze(0)
24
+
25
+ # Move the data to the device
26
+ new_image = new_image.to(device)
27
+
28
+ # Make predictions using the trained model
29
+ with torch.no_grad():
30
+ predictions = model(new_image)
31
+ logits = predictions.last_hidden_state
32
+ logits = logits.view(logits.shape[0], -1)
33
+ feature_reducer = nn.Linear(logits.shape[1], num_classes)
34
+
35
+ logits = logits.to(device)
36
+ feature_reducer = feature_reducer.to(device)
37
+
38
+ logits = feature_reducer(logits)
39
+ predicted_class = torch.argmax(logits, dim=1).item()
40
+ if(predicted_class == 0):
41
+ st.write('Predicted class: Parkinson\'s')
42
+ else:
43
+ st.write('Predicted class: Healthy')