File size: 2,115 Bytes
2c5cec5
02acf5d
0953fe9
1dc0fbb
02acf5d
dfbae18
02acf5d
 
dfbae18
02acf5d
 
 
 
 
dfbae18
02acf5d
 
dfbae18
02acf5d
8dc467a
02acf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfbae18
 
 
 
02acf5d
 
 
 
 
 
 
 
 
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 streamlit as st
import tensorflow as tf
from PIL import Image
import os
from huggingface_hub import hf_hub_url, set_access_token  # Import Hugging Face utilities

# Title of the Streamlit app
st.title("Yellow Rust Severity Prediction")

# Load Hugging Face API token from environment
huggingface_api_token = os.getenv("HUGGINGFACE_TOKEN")
if huggingface_api_token is None:
    st.error("YellowRust API token not found in environment. Please set it.")
    st.stop()

# Set Hugging Face token for authentication
set_access_token(huggingface_api_token)

# Model repository details
model_repo_id = "shaheer-data/Yellow-Rust-Prediction"
model_file_path = "final_meta_model.keras"

# Construct the model URL
st.write("Loading model from Hugging Face repo:", model_repo_id)
model_url = hf_hub_url(model_repo_id, model_file_path)
loaded_model = tf.keras.models.load_model(model_url)  # Load model using tf.keras directly

# Function to make predictions
def predict_image(image):
    image = image.resize((224, 224))  # Resize to match model input dimensions
    image_array = tf.keras.preprocessing.image.img_to_array(image)
    image_array = tf.expand_dims(image_array, axis=0)  # Expand dimensions for batch prediction
    predictions = loaded_model.predict(image_array)
    return predictions

# Class labels for Yellow Rust severity levels
CLASS_LABELS = [
    "Healthy",
    "Mild Severity",
    "Moderate Severity",
    "Severe Severity",
    "Very Severe",
    "Extreme Severity"
]

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

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

    # Display progress bar
    with st.spinner("Making predictions..."):
        predictions = predict_image(image)
        predicted_class = predictions.argmax(axis=-1)
        st.write(f"Predicted Severity Level: {CLASS_LABELS[predicted_class[0]]} with confidence {predictions[0][predicted_class[0]]:.2f}")

else:
    st.write("Please upload an image file to make predictions.")