Download src/streamlit_app.py from EnYa32/Date-Fruit-Classification-CNN: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/EnYa32/Date-Fruit-Classification-CNN/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/EnYa32/Date-Fruit-Classification-CNN/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/EnYa32/Date-Fruit-Classification-CNN/resolve/main/src/streamlit_app.py
2.56 kB
| # app_cnn.py --- Date Fruit Classifier using CNN (without Transfer Learning) | |
| import streamlit as st | |
| import numpy as np | |
| from PIL import Image | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| import warnings | |
| warnings.filterwarnings("ignore", category=DeprecationWarning) | |
| st.set_page_config( | |
| page_title="Date Fruit Classifier (CNN β No Transfer Learning)", | |
| layout="centered" | |
| ) | |
| # ------------------ SETTINGS ------------------ | |
| MODEL_PATH = "src/best_date_cnn.keras" | |
| IMG_SIZE = (224, 224) | |
| CLASS_NAMES = [ | |
| "Ajwa", "Galaxy", "Medjool", "Meneifi", | |
| "Nabtat Ali", "Rutab", "Shaishe", "Sokari", "Sugaey" | |
| ] | |
| # ------------------ LOAD MODEL ------------------ | |
| def load_model_cached(): | |
| return load_model(MODEL_PATH, compile=False) | |
| model = load_model_cached() | |
| # ------------------ IMAGE PREPROCESSING ------------------ | |
| def preprocess_image(image: Image.Image) -> np.ndarray: | |
| image = image.convert("RGB") | |
| image = image.resize(IMG_SIZE) | |
| img_array = np.array(image) / 255.0 # Normalization for CNN | |
| return np.expand_dims(img_array, axis=0) | |
| # ------------------ PREDICTION ------------------ | |
| def predict(image: Image.Image): | |
| processed_img = preprocess_image(image) | |
| preds = model.predict(processed_img) | |
| probs = tf.nn.softmax(preds[0]).numpy() | |
| top_idx = np.argmax(probs) | |
| return CLASS_NAMES[top_idx], probs[top_idx], probs | |
| # ------------------ STREAMLIT UI ------------------ | |
| st.title("π΄ Date Fruit Classifier (CNN β No Transfer Learning)") | |
| st.write( | |
| "This app uses a custom CNN model trained **from scratch** " | |
| "to classify date fruit images." | |
| ) | |
| st.sidebar.header("π§ Model Info") | |
| st.sidebar.write(f"**Model file:** `{MODEL_PATH}`") | |
| st.sidebar.write("**Class labels:**") | |
| for cls in CLASS_NAMES: | |
| st.sidebar.write(f"- {cls}") | |
| uploaded_file = st.file_uploader("π€ Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="πΈ Uploaded Image", use_container_width=True) | |
| if st.button("π Predict (CNN)"): | |
| with st.spinner("Analyzing..."): | |
| top_class, top_prob, probabilities = predict(image) | |
| st.subheader("π Prediction") | |
| st.write(f"**Class:** `{top_class}`") | |
| st.write(f"**Confidence:** {top_prob:.2%}") | |
| st.subheader("π Confidence per Class") | |
| for cls, prob in zip(CLASS_NAMES, probabilities): | |
| st.write(f"- **{cls}** β {prob:.2%}") | |
| else: | |
| st.info("Upload an image to start.") |