dhhd255 commited on
Commit
0e7dc80
·
1 Parent(s): 76f07f2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -13
main.py CHANGED
@@ -1,27 +1,27 @@
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 = {}
@@ -31,5 +31,8 @@ def image_classifier(inp):
31
 
32
  return result
33
 
34
- demo = gr.Interface(fn=image_classifier, inputs='image', outputs='label')
35
- demo.launch()
 
 
 
 
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)