jonathanIckovich commited on
Commit
6e22c6a
·
1 Parent(s): db19107
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -1,16 +1,33 @@
1
  import gradio as gr
2
- from fastai.vision.all import *
 
3
 
 
 
 
 
4
 
 
 
5
 
6
- learn = load_learner('ImageDifferentiator.pkl')
7
-
8
- labels = learn.dls.vocab
9
  def predict(img):
10
-
11
- img = PILImage.create(img)
12
- pred, pred_idx, probs = learn.predict(img)
 
 
 
 
 
 
 
 
13
  return {labels[i]: float(probs[i]) for i in range(len(labels))}
14
 
15
- iface = gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3))
 
 
 
 
 
16
  iface.launch(share=True)
 
1
  import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoTokenizer
3
+ from PIL import Image
4
 
5
+ # Load Hugging Face model and tokenizer
6
+ model_name = 'ImageDifferentiator' # Replace with the specific model name
7
+ model = AutoModelForImageClassification.from_pretrained(model_name)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
+ # Get class labels from the model configuration
11
+ labels = model.config.id2label
12
 
 
 
 
13
  def predict(img):
14
+ # Tokenize and preprocess the image
15
+ inputs = tokenizer(img, return_tensors="pt")
16
+
17
+ # Make prediction using the Hugging Face model
18
+ outputs = model(**inputs)
19
+ logits = outputs.logits
20
+
21
+ # Get class probabilities
22
+ probs = torch.nn.functional.softmax(logits, dim=-1)[0].tolist()
23
+
24
+ # Create result dictionary
25
  return {labels[i]: float(probs[i]) for i in range(len(labels))}
26
 
27
+ iface = gr.Interface(
28
+ fn=predict,
29
+ inputs=gr.inputs.Image(shape=(512, 512)),
30
+ outputs=gr.outputs.Label(num_top_classes=3)
31
+ )
32
+
33
  iface.launch(share=True)