willco-afk commited on
Commit
476047a
·
verified ·
1 Parent(s): 1559e4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -1,11 +1,24 @@
 
1
  import streamlit as st
2
- from transformers import TFAutoModelForImageClassification, AutoFeatureExtractor
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
- # Load the model and feature extractor
7
- model = TFAutoModelForImageClassification.from_pretrained("willco-afk/tree-test") # Replace with your repo
8
- feature_extractor = AutoFeatureExtractor.from_pretrained(model.config._name_or_path)
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Streamlit UI
11
  st.title("Christmas Tree Classifier")
@@ -17,17 +30,19 @@ if uploaded_file is not None:
17
  # Display the uploaded image
18
  image = Image.open(uploaded_file)
19
  st.image(image, caption="Uploaded Image.", use_column_width=True)
 
 
20
 
21
  # Preprocess the image
22
- inputs = feature_extractor(images=image, return_tensors="tf")
 
 
23
 
24
  # Make prediction
25
- logits = model(**inputs).logits
26
- predicted_class_idx = tf.math.argmax(logits, axis=-1)[0]
27
 
28
- # Map class index to label
29
- class_names = model.config.id2label # Get class names from model config
30
- predicted_class = class_names[predicted_class_idx]
31
 
32
  # Display the prediction
33
- st.write(f"Prediction: **{predicted_class}**")
 
1
+ import os
2
  import streamlit as st
3
+ import tensorflow as tf
4
  from PIL import Image
5
  import numpy as np
6
+ from huggingface_hub import login, hf_hub_download
7
 
8
+ # Authenticate with Hugging Face token (if available)
9
+ hf_token = os.environ.get("HF_TOKEN")
10
+ if hf_token:
11
+ login(token=hf_token)
12
+
13
+ # Download and load the model from the Hugging Face Hub
14
+ repo_id = os.environ.get("MODEL_ID", "willco-afk/tree-test-x") # Get repo ID from secret or default
15
+ filename = "your_trained_model.keras" # Updated filename
16
+ cache_dir = "./models" # Local directory to cache the model
17
+ os.makedirs(cache_dir, exist_ok=True)
18
+ model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
19
+
20
+ # Load the model
21
+ model = tf.keras.models.load_model(model_path)
22
 
23
  # Streamlit UI
24
  st.title("Christmas Tree Classifier")
 
30
  # Display the uploaded image
31
  image = Image.open(uploaded_file)
32
  st.image(image, caption="Uploaded Image.", use_column_width=True)
33
+ st.write("")
34
+ st.write("Classifying...")
35
 
36
  # Preprocess the image
37
+ image = image.resize((224, 224)) # Resize to match your model's input size
38
+ image_array = np.array(image) / 255.0 # Normalize pixel values
39
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
40
 
41
  # Make prediction
42
+ prediction = model.predict(image_array)
 
43
 
44
+ # Get predicted class
45
+ predicted_class = "Decorated" if prediction[0][0] >= 0.5 else "Undecorated"
 
46
 
47
  # Display the prediction
48
+ st.write(f"Prediction: {predicted_class}")