Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 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 read the custom model from Hugging Face Spaces
|
| 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 |
+
model_file = h5py.File(BytesIO(r.content), 'r')
|
| 13 |
+
|
| 14 |
+
# Load your custom model
|
| 15 |
+
model = keras.models.load_model(model_file)
|
| 16 |
+
|
| 17 |
+
def image_classifier(inp):
|
| 18 |
+
# Preprocess the input image
|
| 19 |
+
inp = np.array(inp)
|
| 20 |
+
inp = inp / 255.0
|
| 21 |
+
inp = np.expand_dims(inp, axis=0)
|
| 22 |
+
|
| 23 |
+
# Use your custom model for inference
|
| 24 |
+
predictions = model.predict(inp)
|
| 25 |
+
|
| 26 |
+
# Process the predictions and return the result
|
| 27 |
+
result = {}
|
| 28 |
+
for i, prediction in enumerate(predictions[0]):
|
| 29 |
+
label = f'Label {i+1}'
|
| 30 |
+
result[label] = prediction
|
| 31 |
+
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(fn=image_classifier, inputs='image', outputs='label')
|
| 35 |
+
demo.launch()
|