File size: 1,231 Bytes
059669c
64c4207
34bd185
64c4207
efe09c2
64c4207
34bd185
059669c
34bd185
e8c41c8
059669c
efe09c2
 
 
6be805d
efe09c2
64c4207
efe09c2
 
 
 
64c4207
efe09c2
34bd185
64c4207
 
34bd185
 
 
 
 
 
efe09c2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import streamlit as st
from transformers import TFAutoModelForImageClassification, AutoFeatureExtractor
from PIL import Image
import numpy as np

# Load the model and feature extractor
model = TFAutoModelForImageClassification.from_pretrained(os.environ.get("MODEL_ID", "willco-afk/tree-test-x")) 
feature_extractor = AutoFeatureExtractor.from_pretrained(model.config._name_or_path)


# Streamlit UI
st.title("Christmas Tree Classifier")
st.write("Upload an image of a Christmas tree to classify it:")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image.", use_column_width=True)

    # Preprocess the image
    inputs = feature_extractor(images=image, return_tensors="tf")

    # Make prediction
    logits = model(**inputs).logits
    predicted_class_idx = tf.math.argmax(logits, axis=-1)[0]
    
    # Map class index to label
    class_names = model.config.id2label  # Get class names from model config
    predicted_class = class_names[predicted_class_idx]

    # Display the prediction
    st.write(f"Prediction: **{predicted_class}**")