Download app.py from reciouiikram/MivroOrganism_Classification_4_Classes: direct link, hf CLI and curl.
- Browser
- Download file 4.27 kB
-
https://huggingface.co/spaces/reciouiikram/MivroOrganism_Classification_4_Classes/resolve/main/app.py
- Command line
-
hf download hf://spaces/reciouiikram/MivroOrganism_Classification_4_Classes/app.py
-
curl -L -o app.py https://huggingface.co/spaces/reciouiikram/MivroOrganism_Classification_4_Classes/resolve/main/app.py
4.27 kB
| import streamlit as st | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| from PIL import Image, ImageOps | |
| import numpy as np | |
| # Set the page title and layout | |
| st.set_page_config( | |
| page_title="Microorganism Classification π¦ ", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Add custom blue-themed CSS | |
| st.markdown( | |
| """ | |
| <style> | |
| body { | |
| background-color:rgb(27, 54, 77); | |
| } | |
| .stApp { | |
| background: linear-gradient(to right,rgb(123, 166, 247),rgb(1, 103, 236)); | |
| } | |
| .stTitle { | |
| color:rgb(169, 209, 240); | |
| } | |
| .stHeader { | |
| color:rgb(155, 213, 247); | |
| } | |
| .stMarkdown { | |
| color:rgb(170, 215, 240); | |
| } | |
| .stSidebar { | |
| background-color:rgb(88, 143, 226); | |
| } | |
| .stButton > button { | |
| background-color: #0288d1; | |
| color: white; | |
| border: none; | |
| border-radius: 5px; | |
| } | |
| .stButton > button:hover { | |
| background-color: #01579b; | |
| } | |
| </style> | |
| <footer> | |
| © 2024 By: Ikram Recioui. All Rights Reserved. | |
| </footer> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| # Load the pre-trained model | |
| def load_trained_model(): | |
| return load_model(r"Micro_organisme_3_model.h5", compile=False) | |
| model = load_trained_model() | |
| # Define class labels | |
| class_labels = [ | |
| "Bacteria", | |
| "Lactobacillus", | |
| "Micrococcus", | |
| "Virus" | |
| ] | |
| # Home page content | |
| def home(): | |
| st.title("Welcome to the Microorganism Classification App π¦ π¬") | |
| st.markdown("This application helps classify microorganism images into four distinct categories using AI. π¦ π€") | |
| # Section: About Microorganisms | |
| st.subheader("What are Microorganisms? π€") | |
| st.markdown(""" | |
| Microorganisms are microscopic organisms, such as bacteria, viruses, and fungi, that are invisible to the naked eye. | |
| They play significant roles in ecosystems, health, and industries. This app assists researchers and professionals by | |
| automating the classification of microorganism images into four categories: Bacteria, Lactobacillus, Micrococcus, and Virus. π€βοΈ | |
| """) | |
| # Video | |
| st.subheader("Learn More About Microorganisms ππ‘") | |
| st.video("https://www.youtube.com/watch?v=6OQVP8jacek") | |
| # Additional description | |
| st.markdown(""" | |
| ### Features of this App β¨π: | |
| - Upload your microorganism images. | |
| - View AI predictions for 4 different categories: | |
| 1. **Bacteria**: A diverse group of prokaryotic microorganisms. | |
| 2. **Lactobacillus**: A genus of bacteria known for its role in fermentation. | |
| 3. **Micrococcus**: A genus of spherical, Gram-positive bacteria. | |
| 4. **Virus**: Submicroscopic infectious agents that replicate only inside living cells. | |
| """) | |
| # Prediction page content | |
| def upload_and_predict(): | |
| st.title("Upload and Predict π€") | |
| # File uploader for image upload | |
| uploaded_file = st.file_uploader("Choose an image of a microorganism... πΌοΈ", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Display the uploaded image | |
| st.image(uploaded_file, caption="Uploaded Image") | |
| # Load the image for prediction | |
| image = Image.open(uploaded_file) | |
| image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS) | |
| image_array = np.asarray(image) / 255.0 # Normalize the image | |
| image_array = np.expand_dims(image_array, axis=0) # Add batch dimension | |
| # Make prediction | |
| predictions = model.predict(image_array) | |
| predicted_class = class_labels[np.argmax(predictions)] | |
| confidence = np.max(predictions) * 100 | |
| # Display the prediction | |
| st.subheader("Prediction Result π:") | |
| st.markdown(f"**Predicted Class:** {predicted_class}") | |
| st.markdown(f"**Confidence:** {confidence:.2f}%") | |
| # Main function to control navigation | |
| if __name__ == "__main__": | |
| st.sidebar.title("Navigation") | |
| page = st.sidebar.radio("Go to", ["Home", "Upload and Predict"]) | |
| if page == "Home": | |
| home() | |
| elif page == "Upload and Predict": | |
| upload_and_predict() | |