Skip to content

Streamlit#2

Merged
karsterr merged 12 commits into
mainfrom
streamlit
May 28, 2026
Merged

Streamlit#2
karsterr merged 12 commits into
mainfrom
streamlit

Conversation

@karsterr

@karsterr karsterr commented May 28, 2026

Copy link
Copy Markdown
Owner

Summary by Sourcery

Add a Streamlit-based web application for waste image classification using a TensorFlow MobileNetV2 model.

New Features:

  • Introduce a Streamlit UI that allows users to upload waste images and view predicted waste categories with confidence scores.
  • Add a reusable TensorFlow-based waste classifier abstraction with image preprocessing and prediction logic.

Enhancements:

  • Implement model path resolution and caching to efficiently load the waste classification model for the Streamlit app.

@karsterr karsterr requested a review from Copilot May 28, 2026 22:49
@sourcery-ai

sourcery-ai Bot commented May 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a Streamlit-based UI and a TensorFlow-backed classifier abstraction for an AI-powered waste classification demo, including model path resolution, caching, and image preprocessing aligned with a MobileNetV2 training pipeline.

Sequence diagram for Streamlit image classification flow

sequenceDiagram
    actor User
    participant StreamlitApp
    participant WasteClassifier
    participant TFKerasModel

    User ->> StreamlitApp: st.file_uploader
    StreamlitApp ->> StreamlitApp: load_cached_classifier
    StreamlitApp ->> WasteClassifier: predict(image)
    WasteClassifier ->> WasteClassifier: _preprocess_image(image)
    WasteClassifier ->> TFKerasModel: predict(processed_img)
    TFKerasModel -->> WasteClassifier: predictions
    WasteClassifier -->> StreamlitApp: predicted_class, confidence
    StreamlitApp ->> User: st.success / st.metric / st.info
Loading

File-Level Changes

Change Details Files
Introduce Streamlit app that loads a cached waste classifier model, accepts an image upload, runs prediction, and displays results and errors in a two-column layout.
  • Define model candidate paths and resolution logic that prefers valid .keras zip archives and falls back to existing files
  • Configure Streamlit page (title, icon, layout) and set up a cached WasteClassifier loader using st.cache_resource
  • Implement file upload handling with PIL image loading and display, then run classifier.predict within a spinner, showing predicted class, confidence metric, and user guidance
  • Add robust error handling for both model loading and prediction phases with user-facing messages
app.py
Create a TensorFlow-based classifier abstraction with a concrete WasteClassifier implementing model loading, validation, and MobileNetV2-compatible preprocessing.
  • Define an abstract BaseClassifier that stores class names, holds a keras model reference, and exposes a predict method that returns the top class and confidence percentage
  • Implement WasteClassifier that loads a MobileNetV2-based model from a .keras zip file path, validating existence and archive format and raising specific errors on failure
  • Implement image preprocessing that converts images to RGB, resizes to target size, converts to array, rescales pixel values to 0-1, and adds a batch dimension
classifier.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In resolve_model_path, returning MODEL_CANDIDATES[0] when no candidate exists can silently pass a non-existent path down to WasteClassifier; consider raising a clear exception or surfacing a user-facing error instead of defaulting to a missing file.
  • There is a slight inconsistency between resolve_model_path (which accepts any existing candidate, even if not a zip) and WasteClassifier.load_model (which requires zipfile.is_zipfile); aligning these checks will avoid confusing ValueError failures when a non-zip .keras file is selected by the resolver.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `resolve_model_path`, returning `MODEL_CANDIDATES[0]` when no candidate exists can silently pass a non-existent path down to `WasteClassifier`; consider raising a clear exception or surfacing a user-facing error instead of defaulting to a missing file.
- There is a slight inconsistency between `resolve_model_path` (which accepts any existing candidate, even if not a zip) and `WasteClassifier.load_model` (which requires `zipfile.is_zipfile`); aligning these checks will avoid confusing `ValueError` failures when a non-zip `.keras` file is selected by the resolver.

## Individual Comments

### Comment 1
<location path="classifier.py" line_range="53-54" />
<code_context>
+        model_file = Path(model_path)
+        if not model_file.is_file():
+            raise FileNotFoundError(f"Model dosyası bulunamadı: {model_file}")
+        if not zipfile.is_zipfile(model_file):
+            raise ValueError(f"Model dosyası geçersiz .keras arşivi: {model_file}")
+
+        try:
</code_context>
<issue_to_address>
**issue (bug_risk):** Model format doğrulaması `resolve_model_path` ile tutarsız görünüyor.

Burada `.keras` dosyasının mutlaka `zipfile.is_zipfile` olması şartı, `resolve_model_path` içindeki ikinci döngüyle çelişiyor; orada zip kontrolü yapılmadan da model yolu dönebiliyor. Böylece `resolve_model_path` bir dosyayı seçerken, `WasteClassifier` aynı dosyayı “format geçersiz” diyerek reddedebilir.

Bu tutarsızlığı gidermek için ya `resolve_model_path` yalnızca `zipfile.is_zipfile` olan adayları döndürmeli, ya da burada zip kontrolünü uyarı/log seviyesine çekip zip olmayan `.keras` dosyalarının da yüklenmesine izin vermelisiniz. Aksi halde geçerli bir model dosyası için kullanıcı gereksiz yere "dosya geçersiz" hatası alabilir.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread classifier.py
Comment on lines +53 to +54
if not zipfile.is_zipfile(model_file):
raise ValueError(f"Model dosyası geçersiz .keras arşivi: {model_file}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Model format doğrulaması resolve_model_path ile tutarsız görünüyor.

Burada .keras dosyasının mutlaka zipfile.is_zipfile olması şartı, resolve_model_path içindeki ikinci döngüyle çelişiyor; orada zip kontrolü yapılmadan da model yolu dönebiliyor. Böylece resolve_model_path bir dosyayı seçerken, WasteClassifier aynı dosyayı “format geçersiz” diyerek reddedebilir.

Bu tutarsızlığı gidermek için ya resolve_model_path yalnızca zipfile.is_zipfile olan adayları döndürmeli, ya da burada zip kontrolünü uyarı/log seviyesine çekip zip olmayan .keras dosyalarının da yüklenmesine izin vermelisiniz. Aksi halde geçerli bir model dosyası için kullanıcı gereksiz yere "dosya geçersiz" hatası alabilir.

@karsterr karsterr merged commit 6e30347 into main May 28, 2026
1 of 2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Streamlit-based web UI for classifying uploaded waste images using the trained MobileNetV2/Keras model, with a reusable classifier abstraction for loading the model, preprocessing images, and returning predictions.

Changes:

  • Added WasteClassifier and BaseClassifier for TensorFlow model loading, preprocessing, and prediction.
  • Added app.py Streamlit interface with model path resolution, cached classifier loading, image upload, and prediction display.
  • Maps model output classes to Turkish waste category labels.

Reviewed changes

Copilot reviewed 2 out of 4 changed files in this pull request and generated 3 comments.

File Description
classifier.py Adds reusable TensorFlow/PIL image classification logic for the waste model.
app.py Adds the Streamlit UI, model discovery/caching, upload flow, and prediction result display.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app.py
Comment on lines +31 to +32
@st.cache_resource
def load_cached_classifier():
Comment thread app.py

if uploaded_file is not None:
# Görseli RAM'e alıyoruz
image = Image.open(uploaded_file)
Comment thread app.py
Comment on lines +3 to +5
import streamlit as st
from PIL import Image
from classifier import WasteClassifier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants