Download src/streamlit_app1.py from BOLLO-DIENG/Projet_Deep_Learning_BOLLO_DIENG: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/BOLLO-DIENG/Projet_Deep_Learning_BOLLO_DIENG/resolve/main/src/streamlit_app1.py
- Command line
-
hf download hf://spaces/BOLLO-DIENG/Projet_Deep_Learning_BOLLO_DIENG/src/streamlit_app1.py
-
curl -L -o streamlit_app1.py https://huggingface.co/spaces/BOLLO-DIENG/Projet_Deep_Learning_BOLLO_DIENG/resolve/main/src/streamlit_app1.py
2.08 kB
| import streamlit as st | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| # Chargement des modèles avec message | |
| with st.spinner("Chargement des modèles..."): | |
| try: | |
| model_infect = tf.keras.models.load_model("src/exo1.keras") | |
| model_animals = tf.keras.models.load_model("src/exo2.keras") | |
| st.success("Modèles chargés avec succès ") | |
| except Exception as e: | |
| st.error(f"Erreur lors du chargement des modèles : {e}") | |
| st.stop() | |
| # Fonction de prédiction | |
| def predict(model, img_array, classes): | |
| prediction = model.predict(img_array) | |
| index = np.argmax(prediction) | |
| return classes[index], float(prediction[0][index]) | |
| # Interface utilisateur | |
| st.title("Classification d’images") | |
| # Choix du modèle | |
| option = st.selectbox("Choisissez le modèle :", ("Infecté / Non Infecté", "Chat / Chien")) | |
| # Upload d'image | |
| uploaded_file = st.file_uploader("Uploader une image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file: | |
| try: | |
| image = Image.open(uploaded_file).convert("RGB") | |
| st.image(image, caption="Image chargée", use_container_width=True) | |
| # Bouton pour déclencher la prédiction | |
| if st.button("Prédire"): | |
| with st.spinner("Prétraitement de l’image..."): | |
| if option == "Infecté / Non Infecté": | |
| image = image.resize((50, 50)) | |
| model = model_infect | |
| classes = ["Non Infecté", "Infecté"] | |
| else: | |
| image = image.resize((128, 128)) | |
| model = model_animals | |
| classes = ["Chat", "Chien"] | |
| img_array = np.array(image) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| with st.spinner("Prédiction en cours..."): | |
| label, confidence = predict(model, img_array, classes) | |
| st.success(f"✅ Classe prédite : **{label}** avec une confiance de **{confidence:.2f}**") | |
| except Exception as e: | |
| st.error(f"❌ Erreur lors du traitement : {e}") | |