Asimuddin11 commited on
Commit
c552f48
·
verified ·
1 Parent(s): 7243c1a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Set Streamlit page config
7
+ st.set_page_config(
8
+ page_title="ViT Image Classifier",
9
+ layout="centered",
10
+ page_icon="🖼️",
11
+ )
12
+
13
+ # Title
14
+ st.title("🧠 ViT Image Classification")
15
+ st.write("This app uses the **Vision Transformer (ViT)** model `google/vit-base-patch16-224` to classify uploaded images.")
16
+
17
+ # Load pipeline only once using caching
18
+ @st.cache_resource
19
+ def load_pipeline():
20
+ return pipeline("image-classification", model="google/vit-base-patch16-224")
21
+
22
+ pipe = load_pipeline()
23
+
24
+ # Upload an image
25
+ uploaded_file = st.file_uploader("📤 Upload an image", type=["jpg", "jpeg", "png"])
26
+
27
+ if uploaded_file is not None:
28
+ # Display the image
29
+ image = Image.open(uploaded_file).convert("RGB")
30
+ st.image(image, caption="Uploaded Image", use_column_width=True)
31
+
32
+ # Run classification
33
+ with st.spinner("🔍 Classifying..."):
34
+ result = pipe(image)
35
+
36
+ # Display results
37
+ st.subheader("📊 Top Predictions")
38
+ for i, prediction in enumerate(result):
39
+ st.write(f"{i+1}. **{prediction['label']}** with score **{prediction['score']:.4f}**")