Download src/streamlit_app.py from ESMATUGBA/Fruit-Veg-Classifier: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/spaces/ESMATUGBA/Fruit-Veg-Classifier/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/ESMATUGBA/Fruit-Veg-Classifier/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/ESMATUGBA/Fruit-Veg-Classifier/resolve/main/src/streamlit_app.py
4.34 kB
| import streamlit as st | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| import os | |
| # --- SAYFA AYARLARI / PAGE CONFIG --- | |
| st.set_page_config(page_title="Fruit & Veg Classifier", layout="wide", page_icon="🍎") | |
| # --- MODEL YÜKLEME / LOAD MODEL (Titremeyi ve tekrar yüklemeyi önler) --- | |
| def load_my_model(): | |
| model_path = "cnn_model.h5" | |
| if not os.path.exists(model_path): | |
| return None | |
| # Model yüklenirken hata oluşursa sessizce yönetir | |
| try: | |
| model = tf.keras.models.load_model(model_path) | |
| return model | |
| except: | |
| return None | |
| model = load_my_model() | |
| # --- SINIF İSİMLERİ (Notebook Sıralaması İle Birebir Aynı) --- | |
| # İngilizce (Klasör isimleri) | |
| class_names = [ | |
| 'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', | |
| 'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', | |
| 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', | |
| 'mango', 'onion', 'orange', 'paprika', 'pear', 'peas', 'pineapple', | |
| 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn', | |
| 'sweetpotato', 'tomato', 'turnip', 'watermelon' | |
| ] | |
| # Türkçe Karşılıkları | |
| class_tr = [ | |
| 'Elma', 'Muz', 'Pancar', 'Dolmalık Biber', 'Lahana', 'Dolma Biber (Capsicum)', | |
| 'Havuç', 'Karnabahar', 'Acı Biber', 'Mısır', 'Salatalık', 'Patlıcan', | |
| 'Sarımsak', 'Zencefil', 'Üzüm', 'Jalapeno Biberi', 'Kivi', 'Limon', 'Marul', | |
| 'Mango', 'Soğan', 'Portakal', 'Kırmızı Toz Biber', 'Armut', 'Bezelye', 'Ananas', | |
| 'Nar', 'Patates', 'Turp', 'Soya Fasulyesi', 'Ispanak', 'Tatlı Mısır', | |
| 'Tatlı Patates', 'Domates', 'Şalgam', 'Karpuz' | |
| ] | |
| # --- ARAYÜZ / UI --- | |
| st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı") | |
| st.write("36 different species / 36 farklı tür") | |
| st.divider() | |
| # Yan Panel / Sidebar | |
| with st.sidebar: | |
| st.header("Project Info / Proje Bilgisi") | |
| st.info("Architecture: Custom 5-Layer CNN\n\nMimari: Özel 5 Katmanlı CNN") | |
| st.subheader("Species List / Tür Listesi") | |
| # Liste görünümü (İngilizce / Türkçe) | |
| for en, tr in zip(class_names, class_tr): | |
| st.write(f"• {en.capitalize()} / {tr}") | |
| # Ana İçerik Alanı | |
| if model is None: | |
| st.error("Model file 'cnn_model.h5' not found! Please upload the model file. / 'cnn_model.h5' dosyası bulunamadı!") | |
| else: | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| st.subheader("Upload / Yükle 📤") | |
| uploaded_file = st.file_uploader("Choose a fruit/veg photo...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True) | |
| with col2: | |
| st.subheader("Analysis / Analiz 🔍") | |
| if uploaded_file is not None: | |
| if st.button("Predict / Tahmin Et"): | |
| with st.spinner("Analyzing... / Analiz ediliyor..."): | |
| # 1. Ön İşleme (Preprocessing) | |
| img = image.resize((128, 128)) | |
| img_array = np.array(img).astype('float32') | |
| # 2. Normalizasyon (Hep aynı meyvenin çıkmasını engelleyen kritik adım) | |
| img_array /= 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # 3. Tahmin (Prediction) | |
| preds = model.predict(img_array, verbose=0) | |
| class_idx = np.argmax(preds[0]) | |
| confidence = np.max(preds[0]) * 100 | |
| # 4. Sonuçları Göster | |
| en_result = class_names[class_idx].capitalize() | |
| tr_result = class_tr[class_idx] | |
| st.success(f"### Result / Sonuç: {en_result} / {tr_result}") | |
| st.write(f"**Confidence / Güven:** %{confidence:.2f}") | |
| st.progress(int(confidence)) | |
| # Kutlama | |
| st.balloons() | |
| else: | |
| st.info("Waiting for an image to analyze... / Analiz için resim bekleniyor...") | |
| st.divider() | |
| st.caption("Deep Learning Project - Fruit & Vegetable Detection") |