Mohuu0601 commited on
Commit
5da8948
·
verified ·
1 Parent(s): e7634e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoFeatureExtractor, AutoModelForImageClassification
2
+ from PIL import Image
3
+ import gradio as gr
4
+
5
+ # Load the pretrained model and feature extractor
6
+ feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
7
+ model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
8
+
9
+ # Define the function to classify images
10
+ def classify_image(image):
11
+ image = Image.fromarray(image).convert("RGB") # Convert input image to RGB
12
+ inputs = feature_extractor(images=image, return_tensors="pt") # Preprocess image
13
+ outputs = model(**inputs) # Get model predictions
14
+ predicted_class_idx = outputs.logits.argmax(-1).item() # Get predicted class index
15
+ return model.config.id2label[predicted_class_idx] # Return class label
16
+
17
+ # Create a Gradio app interface
18
+ app = gr.Interface(
19
+ fn=classify_image, # Function to run
20
+ inputs=gr.Image(type="numpy"), # Input: Image
21
+ outputs="text", # Output: Predicted class label
22
+ title="Image Classification App" # App title
23
+ )
24
+
25
+ # Launch the app
26
+ app.launch()