Jangai commited on
Commit
a6fcafd
·
verified ·
1 Parent(s): a57d0bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
- from transformers import AutoFeatureExtractor
4
  from PIL import Image
5
  import numpy as np
6
  import logging
@@ -9,10 +9,10 @@ import logging
9
  logging.basicConfig(level=logging.DEBUG)
10
 
11
  # Load the pre-trained model and feature extractor
12
- model_name = "hoangthan/image-classification"
13
  logging.info("Loading image processor and model...")
14
  feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
15
- model = tf.keras.models.load_model('https://huggingface.co/hoangthan/image-classification/resolve/main/tf_model.h5')
16
 
17
  # Define the prediction function
18
  def predict(image):
@@ -29,15 +29,15 @@ def predict(image):
29
  logging.debug("Converting NumPy array to PIL image...")
30
  image = Image.fromarray(image, 'RGBA').convert('RGB')
31
  logging.debug("Image converted successfully.")
32
-
33
- # Process the image for the model
34
- inputs = feature_extractor(images=image, return_tensors="np")
35
- pixel_values = inputs['pixel_values'][0]
36
 
37
- # Predict using the model
38
- preds = model.predict(np.expand_dims(pixel_values, axis=0))
39
- top_probs = tf.nn.softmax(preds[0])
40
- top_idxs = np.argsort(-top_probs)[:3]
 
 
 
 
41
  top_classes = [model.config.id2label[idx] for idx in top_idxs]
42
  result = {top_classes[i]: float(top_probs[i]) for i in range(3)}
43
  logging.info("Prediction successful.")
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoFeatureExtractor, AutoModelForImageClassification
4
  from PIL import Image
5
  import numpy as np
6
  import logging
 
9
  logging.basicConfig(level=logging.DEBUG)
10
 
11
  # Load the pre-trained model and feature extractor
12
+ model_name = "microsoft/quickdraw"
13
  logging.info("Loading image processor and model...")
14
  feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
15
+ model = AutoModelForImageClassification.from_pretrained(model_name)
16
 
17
  # Define the prediction function
18
  def predict(image):
 
29
  logging.debug("Converting NumPy array to PIL image...")
30
  image = Image.fromarray(image, 'RGBA').convert('RGB')
31
  logging.debug("Image converted successfully.")
 
 
 
 
32
 
33
+ logging.info("Processing image...")
34
+ inputs = feature_extractor(images=image, return_tensors="pt")
35
+ outputs = model(**inputs)
36
+ logits = outputs.logits
37
+ probs = torch.nn.functional.softmax(logits, dim=-1)
38
+ top_probs, top_idxs = probs.topk(3, dim=-1)
39
+ top_probs = top_probs.detach().numpy()[0]
40
+ top_idxs = top_idxs.detach().numpy()[0]
41
  top_classes = [model.config.id2label[idx] for idx in top_idxs]
42
  result = {top_classes[i]: float(top_probs[i]) for i in range(3)}
43
  logging.info("Prediction successful.")