Update main.py
Browse files
main.py
CHANGED
@@ -1,27 +1,27 @@
|
|
1 |
-
import
|
2 |
from tensorflow import keras
|
3 |
import numpy as np
|
4 |
from huggingface_hub import HfApi
|
5 |
-
import h5py
|
6 |
-
from io import BytesIO
|
7 |
|
8 |
-
# Authenticate and
|
9 |
hf_api = HfApi()
|
10 |
model_url = hf_api.presigned_url('dhhd255', 'idk_test', filename='best_model.h5', token='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')
|
11 |
r = requests.get(model_url)
|
12 |
-
|
|
|
13 |
|
14 |
# Load your custom model
|
15 |
-
model = keras.models.load_model(
|
16 |
|
17 |
-
|
|
|
18 |
# Preprocess the input image
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
# Use your custom model for inference
|
24 |
-
predictions = model.predict(
|
25 |
|
26 |
# Process the predictions and return the result
|
27 |
result = {}
|
@@ -31,5 +31,8 @@ def image_classifier(inp):
|
|
31 |
|
32 |
return result
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
from tensorflow import keras
|
3 |
import numpy as np
|
4 |
from huggingface_hub import HfApi
|
|
|
|
|
5 |
|
6 |
+
# Authenticate and download the custom model from Hugging Face Spaces
|
7 |
hf_api = HfApi()
|
8 |
model_url = hf_api.presigned_url('dhhd255', 'idk_test', filename='best_model.h5', token='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')
|
9 |
r = requests.get(model_url)
|
10 |
+
with open('best_model.h5', 'wb') as f:
|
11 |
+
f.write(r.content)
|
12 |
|
13 |
# Load your custom model
|
14 |
+
model = keras.models.load_model('best_model.h5')
|
15 |
|
16 |
+
# Define a function that takes an image as input and uses the model for inference
|
17 |
+
def image_classifier(image):
|
18 |
# Preprocess the input image
|
19 |
+
image = np.array(image)
|
20 |
+
image = image / 255.0
|
21 |
+
image = np.expand_dims(image, axis=0)
|
22 |
|
23 |
# Use your custom model for inference
|
24 |
+
predictions = model.predict(image)
|
25 |
|
26 |
# Process the predictions and return the result
|
27 |
result = {}
|
|
|
31 |
|
32 |
return result
|
33 |
|
34 |
+
# Create a Streamlit app with an image upload input
|
35 |
+
image = st.file_uploader('Upload an image')
|
36 |
+
if image is not None:
|
37 |
+
result = image_classifier(image)
|
38 |
+
st.write(result)
|