Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from amr-moussa/Garbage_Clf2: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/amr-moussa/Garbage_Clf2/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/amr-moussa/Garbage_Clf2/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/amr-moussa/Garbage_Clf2/resolve/main/src/streamlit_app.py
2.48 kB
| import streamlit as st | |
| import numpy as np | |
| import tensorflow as tf | |
| import pandas as pd | |
| from PIL import Image | |
| from io import StringIO | |
| # ========== Configuration ========== | |
| st.set_page_config(page_title="Garbage Classifier", layout="centered") | |
| # ========== Load Model ========== | |
| def load_model() -> tf.keras.Model: | |
| """Loads and caches the trained Keras model.""" | |
| return tf.keras.models.load_model("model.keras") | |
| model = load_model() | |
| # ========== Class Names ========== | |
| class_names = [ | |
| 'battery', 'biological', 'cardboard', 'clothes', 'glass', | |
| 'metal', 'paper', 'plastic', 'shoes', 'trash' | |
| ] | |
| # ========== Sidebar ========== | |
| st.sidebar.title("βοΈ Options") | |
| st.sidebar.markdown(""" | |
| Upload an image for classification. | |
| - Formats: JPG, PNG, JPEG | |
| - Images will be resized to 224Γ224 before prediction. | |
| """) | |
| if st.sidebar.button("Show Model Summary"): | |
| buffer = StringIO() | |
| model.summary(print_fn=lambda line: buffer.write(line + "\n")) | |
| st.sidebar.subheader("π Model Architecture") | |
| st.sidebar.text(buffer.getvalue()) | |
| # ========== Main Interface ========== | |
| st.title("π§ Garbage Classifier App") | |
| st.markdown("Upload an image and the model will predict the type of garbage.") | |
| uploaded_file = st.file_uploader("π€ Upload Image", type=["jpg", "jpeg", "png"]) | |
| # ========== Preprocessing ========== | |
| def preprocess_image(img: Image.Image, target_size=(224, 224)) -> np.ndarray: | |
| """Resizes and normalizes the image.""" | |
| img = img.resize(target_size) | |
| img_array = tf.keras.preprocessing.image.img_to_array(img) | |
| img_array = img_array / 255.0 | |
| return np.expand_dims(img_array, axis=0) | |
| # ========== Prediction ========== | |
| if uploaded_file: | |
| image = Image.open(uploaded_file).convert("RGB") | |
| st.image(image, caption="π· Uploaded Image", use_column_width=True) | |
| # Preprocess & predict | |
| input_tensor = preprocess_image(image) | |
| predictions = model.predict(input_tensor)[0] | |
| predicted_index = np.argmax(predictions) | |
| predicted_class = class_names[predicted_index] | |
| confidence = predictions[predicted_index] * 100 | |
| st.markdown(f"### π― Prediction: `{predicted_class}`") | |
| st.markdown(f"**Confidence:** `{confidence:.2f}%`") | |
| # Show all class probabilities | |
| st.subheader("π Class Probabilities") | |
| prob_df = pd.DataFrame({ | |
| "Class": class_names, | |
| "Probability": [f"{p:.4f}" for p in predictions] | |
| }) | |
| st.dataframe(prob_df, use_container_width=True) | |