Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="ViT Image Classifier")
|
| 6 |
+
|
| 7 |
+
st.title("ViT Image Classification")
|
| 8 |
+
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 12 |
+
|
| 13 |
+
pipe = load_model()
|
| 14 |
+
|
| 15 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 16 |
+
|
| 17 |
+
if uploaded_file is not None:
|
| 18 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 19 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 20 |
+
|
| 21 |
+
with st.spinner("Classifying..."):
|
| 22 |
+
preds = pipe(image)
|
| 23 |
+
|
| 24 |
+
st.subheader("Predictions")
|
| 25 |
+
for i, pred in enumerate(preds):
|
| 26 |
+
st.write(f"{i+1}. {pred['label']} ({pred['score']:.3f})")
|
| 27 |
+
|
| 28 |
+
# This is optional but sometimes helps initialization on some platforms
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
import streamlit.web.bootstrap
|
| 31 |
+
streamlit.web.bootstrap.run()
|