Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,46 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
from huggingface_hub import HfFileSystem
|
5 |
from PIL import Image
|
6 |
|
7 |
# Authenticate and download the custom model from Hugging Face Spaces
|
8 |
fs = HfFileSystem()
|
9 |
-
model_path = 'dhhd255/main_model/best_model.
|
10 |
with fs.open(model_path, 'rb') as f:
|
11 |
model_content = f.read()
|
12 |
|
13 |
# Save the model file to disk
|
14 |
-
with open('best_model.
|
15 |
f.write(model_content)
|
16 |
|
17 |
# Load your custom model
|
18 |
-
model =
|
|
|
19 |
|
20 |
# Define a function that takes an image as input and uses the model for inference
|
21 |
def image_classifier(image):
|
22 |
# Preprocess the input image
|
|
|
|
|
|
|
|
|
|
|
23 |
image = Image.fromarray(image)
|
24 |
-
image = image
|
25 |
-
image = image.
|
26 |
-
image = np.array(image)
|
27 |
-
image = image / 255.0
|
28 |
-
image = np.expand_dims(image, axis=0)
|
29 |
-
image = np.expand_dims(image, axis=-1)
|
30 |
|
31 |
# Use your custom model for inference
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
predicted_index = np.argmax(predictions[0])
|
36 |
|
37 |
# Map the index to a class label
|
38 |
labels = ['Healthy', 'Parkinson']
|
39 |
-
predicted_label = labels[
|
40 |
|
41 |
# Return the result
|
42 |
-
return
|
43 |
|
44 |
# Create a Streamlit app with an image upload input
|
45 |
uploaded_file = st.file_uploader('Upload an image')
|
|
|
1 |
import streamlit as st
|
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 custom model from Hugging Face Spaces
|
8 |
fs = HfFileSystem()
|
9 |
+
model_path = 'dhhd255/main_model/best_model.pth'
|
10 |
with fs.open(model_path, 'rb') as f:
|
11 |
model_content = f.read()
|
12 |
|
13 |
# Save the model file to disk
|
14 |
+
with open('best_model.pth', 'wb') as f:
|
15 |
f.write(model_content)
|
16 |
|
17 |
# Load your custom model
|
18 |
+
model = torch.load('best_model.pth')
|
19 |
+
model.eval()
|
20 |
|
21 |
# Define a function that takes an image as input and uses the model for inference
|
22 |
def image_classifier(image):
|
23 |
# Preprocess the input image
|
24 |
+
data_transform = transforms.Compose([
|
25 |
+
transforms.Lambda(lambda x: x.convert('RGB')),
|
26 |
+
transforms.Resize((224, 224)),
|
27 |
+
transforms.ToTensor()
|
28 |
+
])
|
29 |
image = Image.fromarray(image)
|
30 |
+
image = data_transform(image)
|
31 |
+
image = image.unsqueeze(0)
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Use your custom model for inference
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = model(image)
|
36 |
+
_, predicted = torch.max(outputs.data, 1)
|
|
|
37 |
|
38 |
# Map the index to a class label
|
39 |
labels = ['Healthy', 'Parkinson']
|
40 |
+
predicted_label = labels[predicted.item()]
|
41 |
|
42 |
# Return the result
|
43 |
+
return outputs[0].numpy(), predicted_label
|
44 |
|
45 |
# Create a Streamlit app with an image upload input
|
46 |
uploaded_file = st.file_uploader('Upload an image')
|