Ahmed235 commited on
Commit
392b986
1 Parent(s): 6e6f8d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -40
app.py CHANGED
@@ -1,17 +1,12 @@
1
- import json
2
  from PIL import Image
 
3
  import numpy as np
4
- from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
5
- from tensorflow.keras.models import load_model
6
- import ipywidgets as widgets
7
- from IPython.display import display
8
-
9
- model_path = 'final_teath_classifier.h5'
10
 
 
 
11
  model = tf.keras.models.load_model(model_path)
12
 
13
- # Load the model from Hugging Face model hub
14
-
15
  def preprocess_image(image: Image.Image) -> np.ndarray:
16
  # Resize the image to match input size
17
  image = image.resize((256, 256))
@@ -25,41 +20,21 @@ def predict_image(image_path):
25
  img = Image.open(image_path)
26
  # Preprocess the image
27
  img_array = preprocess_image(img)
28
- # Convert image array to string using base64 encoding (for text-based models)
29
- inputs = tokenizer.encode(img_array, return_tensors="tf")
30
- # Make prediction
31
- outputs = model(inputs)
32
- predictions = tf.nn.softmax(outputs.logits, axis=-1)
33
  predicted_class = np.argmax(predictions)
34
  if predicted_class == 0:
35
- predict_label = "Clean"
36
  else:
37
- predict_label = "Carries"
38
-
39
- return predict_label, predictions.numpy().flatten()
40
 
41
- # Create a file uploader widget
42
- uploader = widgets.FileUpload(accept="image/*", multiple=False)
43
 
44
- # Display the file uploader widget
45
- display(uploader)
 
46
 
47
- # Define a callback function to handle the uploaded image
48
- def on_upload(change):
49
- # Get the uploaded image file
50
- image_file = list(uploader.value.values())[0]["content"]
51
- # Save the image to a temporary file
52
- with open("temp_image.jpg", "wb") as f:
53
- f.write(image_file)
54
- # Get predictions for the uploaded image
55
- predict_label, logits = predict_image("temp_image.jpg")
56
- # Create a JSON object with the predictions
57
- predictions_json = {
58
- "predicted_class": predict_label,
59
- "evaluations": [f"{logit*100:.4f}%" for logit in logits]
60
- }
61
- # Print the JSON object
62
- print(json.dumps(predictions_json, indent=4))
63
 
64
- # Set the callback function for when a file is uploaded
65
- uploader.observe(on_upload, names="value")
 
 
 
1
  from PIL import Image
2
+ import tensorflow as tf
3
  import numpy as np
4
+ #from google.colab import files
 
 
 
 
 
5
 
6
+ #model_path = 'final_teath_classifier.h5'
7
+ # Load the model
8
  model = tf.keras.models.load_model(model_path)
9
 
 
 
10
  def preprocess_image(image: Image.Image) -> np.ndarray:
11
  # Resize the image to match input size
12
  image = image.resize((256, 256))
 
20
  img = Image.open(image_path)
21
  # Preprocess the image
22
  img_array = preprocess_image(img)
23
+ predictions = model.predict(img_array)
 
 
 
 
24
  predicted_class = np.argmax(predictions)
25
  if predicted_class == 0:
26
+ predict_label = "Clean"
27
  else:
28
+ predict_label = "Carries"
 
 
29
 
 
 
30
 
31
+ return predict_label,predictions.flatten()
32
+ # Upload the image
33
+ #uploaded = files.upload()
34
 
35
+ # Get the uploaded image file name
36
+ #image_path = list(uploaded.keys())[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ predict_label, logits = predict_image(image_path)
39
+ print("Predicted class:", predict_label)
40
+ print("Evaluate:", ', '.join(f"{logits*100:.4f}%" for logits in logits))