Spaces:
Sleeping
Sleeping
File size: 2,128 Bytes
f0ecbdc e8c41c8 f0ecbdc e406126 f0ecbdc 9268827 f0ecbdc e406126 f0ecbdc e406126 9268827 f0ecbdc e406126 f0ecbdc 2d7c0db f0ecbdc e406126 f0ecbdc ae6c2da e406126 f0ecbdc e406126 78fc238 e406126 f0ecbdc e406126 f0ecbdc e406126 f0ecbdc e406126 ae6c2da |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import os
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
from huggingface_hub import login, hf_hub_download
# Authenticate with Hugging Face token (if available)
hf_token = os.environ.get("HF_TOKEN")
if hf_token:
login(token=hf_token)
# Download and load the model from the Hugging Face Hub
repo_id = os.environ.get("MODEL_ID", "willco-afk/tree-test-x") # Get repo ID from secret or default
filename = "your_trained_model.keras" # Updated filename
cache_dir = "./models" # Local directory to cache the model
os.makedirs(cache_dir, exist_ok=True)
model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
# Load the model
model = tf.keras.models.load_model(model_path)
# Streamlit UI
st.title("Christmas Tree Classifier")
st.write("Upload an image of a Christmas tree to classify it:")
# Create tabs here (after the main UI elements)
tab1, tab2 = st.tabs(["Christmas Tree Classifier", "Sample Images"])
# Tab 1: Christmas Tree Classifier
with tab1:
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# ... (Rest of the code for image processing and prediction) ...
# Tab 2: Sample Images
with tab2:
# ... (Code for Tab 2 remains the same) ...
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)
st.write("")
st.write("Classifying...")
# Preprocess the image
image = image.resize((224, 224)) # Resize to match your model's input size
image_array = np.array(image) / 255.0 # Normalize pixel values
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
# Make prediction
prediction = model.predict(image_array)
# Get predicted class
predicted_class = "Decorated" if prediction[0][0] >= 0.5 else "Undecorated"
# Display the prediction
st.write(f"Prediction: {predicted_class}") |