falba commited on
Commit
68e3515
1 Parent(s): 7b49da6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ViTForImageClassification, ViTImageProcessor
2
+ from datasets import load_dataset
3
+ import gradio as gr
4
+
5
+ eval = load_dataset("Marxulia/asl_sign_languages_alphabets_v02", split="train")
6
+ eval = eval.rename_column('label', 'labels')
7
+ id2label = {str(i): lab for i, lab in enumerate(eval.features["labels"].names)}
8
+
9
+ trained_model = ViTForImageClassification.from_pretrained("falba/google-vit-base-ASL")
10
+ processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')
11
+
12
+
13
+ with gr.Blocks() as demo:
14
+ gallery = gr.Gallery([i for i in eval['image']])
15
+ statement = gr.Label()
16
+
17
+ def on_select(evt: gr.SelectData): # SelectData is a subclass of EventData
18
+ chosen_index = evt.index
19
+ chosen_image = eval['image'][chosen_index]
20
+ inputs = processor(images=chosen_image, return_tensors="pt")
21
+ outputs = trained_model(**inputs)
22
+ predicted_label_id = outputs.logits.argmax(-1).item()
23
+ predicted_label = id2label[str(predicted_label_id)]
24
+ actual_label = eval['labels'][chosen_index]
25
+ return f"Actual Label: {id2label[str(actual_label)]} | Predicted label: {predicted_label}"
26
+
27
+ gallery.select(on_select, None, statement)
28
+
29
+ demo.launch()