Download src/streamlit_app.py from Sridhar027/Vegetable_Classification_Using_CNN_Model: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/Sridhar027/Vegetable_Classification_Using_CNN_Model/resolve/4b4d430a75af2bfe959284c724930b8bf012590e/src/streamlit_app.py
- Command line
-
hf download hf://spaces/Sridhar027/Vegetable_Classification_Using_CNN_Model@4b4d430a75af2bfe959284c724930b8bf012590e/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/Sridhar027/Vegetable_Classification_Using_CNN_Model/resolve/4b4d430a75af2bfe959284c724930b8bf012590e/src/streamlit_app.py
1.53 kB
| import streamlit as st | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| import base64 | |
| from tensorflow.keras.models import load_model as keras_load_model | |
| # Set Streamlit page config | |
| st.set_page_config(page_title="Vegetable Classifier", page_icon="🥦", layout="centered") | |
| # Optional: Set background color | |
| def set_bg_color(color="#f0fff0"): | |
| st.markdown(f"""<style> | |
| .stApp {{ | |
| background-color: {color}; | |
| }} | |
| </style>""", unsafe_allow_html=True) | |
| # Optional: Background image | |
| def add_bg_image(image_file): | |
| with open(image_file, "rb") as f: | |
| encoded = base64.b64encode(f.read()).decode() | |
| st.markdown(f""" | |
| <style> | |
| .stApp {{ | |
| background-image: url("data:image/png;base64,{encoded}"); | |
| background-size: cover; | |
| }} | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Class labels | |
| class_names = [ | |
| 'Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli', | |
| 'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber', | |
| 'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato' | |
| ] | |
| # Load model safely | |
| def load_model_safe(): | |
| try: | |
| model = load_model("vegetable_cnn_improved (2).h5", compile=False) | |
| return model | |
| except Exception as e: | |
| st.error(f"❌ Error loading model: {e}") | |
| return None | |
| # Load the model | |
| model = load_model_safe() | |
| # UI | |
| set_bg_color() | |
| st.markdown("<h1 style='text-align:center;'>🥦 Vegetable Image Classifier 🥕</h1>", unsafe_allow_html=True) |