app.main

Main Gradio Application Entry Point.

This module serves as the main entry point for the Garbage Classifier interactive demo. It orchestrates the creation of the Gradio interface, combining data exploration, model training, and evaluation sections into a unified web application with real-time carbon footprint tracking.

Usage

Command line: $ uv run python app/main.py

The application will launch a Gradio interface accessible via web browser, with an optional shareable link for remote access.

Notes

The application includes:

  • Data Exploration: Visualize dataset statistics and prototypes
  • Model Training: Train new models with custom hyperparameters
  • Model Evaluation: Evaluate models and make predictions
  • Carbon Tracking: Real-time display of cumulative carbon emissions
  1#!/usr/bin/env python3
  2# -*- coding: utf-8 -*-
  3"""
  4Main Gradio Application Entry Point.
  5
  6This module serves as the main entry point for the Garbage Classifier
  7interactive demo. It orchestrates the creation of the Gradio interface,
  8combining data exploration, model training, and evaluation sections into
  9a unified web application with real-time carbon footprint tracking.
 10
 11Usage
 12-----
 13Command line:
 14    $ uv run python app/main.py
 15
 16The application will launch a Gradio interface accessible via web browser,
 17with an optional shareable link for remote access.
 18
 19Notes
 20-----
 21The application includes:
 22- Data Exploration: Visualize dataset statistics and prototypes
 23- Model Training: Train new models with custom hyperparameters
 24- Model Evaluation: Evaluate models and make predictions
 25- Carbon Tracking: Real-time display of cumulative carbon emissions
 26"""
 27__docformat__ = "numpy"
 28
 29import gradio as gr
 30from app.sections.data_exploration import data_exploration_tab
 31from app.sections.model_training import model_training_tab
 32from app.sections.model_evaluation import model_evaluation_tab
 33from source.utils import config as cfg
 34from source.utils.carbon_utils import format_total_emissions_display
 35from pathlib import Path
 36
 37
 38def get_emissions_path():
 39    """
 40    Get the path to the emissions CSV file.
 41
 42    Returns
 43    -------
 44    pathlib.Path
 45        Path object pointing to the emissions.csv file in the model directory.
 46
 47    Notes
 48    -----
 49    The emissions file is located in the same directory as the trained model
 50    checkpoint, as defined in the configuration.
 51    """
 52    return Path(cfg.MODEL_PATH).parent / "emissions.csv"
 53
 54
 55def update_carbon_display():
 56    """
 57    Update the carbon footprint display with latest emissions data.
 58
 59    Returns
 60    -------
 61    str
 62        HTML-formatted string containing the total carbon emissions statistics,
 63        including CO₂ equivalent mass and car distance equivalent.
 64
 65    Notes
 66    -----
 67    This function reads the emissions CSV file and formats the cumulative
 68    carbon footprint for display in the Gradio interface header.
 69    """
 70    return format_total_emissions_display(get_emissions_path())
 71
 72
 73def main():
 74    """
 75    Launch the main Gradio application interface.
 76
 77    Creates and configures a multi-tab Gradio interface with data exploration,
 78    model training, and evaluation capabilities. Includes a persistent carbon
 79    footprint counter in the header and custom CSS styling.
 80
 81    The interface includes:
 82    - Header with project title and carbon counter
 83    - Tab 1: Data Exploration (dataset visualization and analysis)
 84    - Tab 2: Training Interface (model training with custom parameters)
 85    - Tab 3: Model Evaluation (metrics visualization and inference)
 86
 87    Notes
 88    -----
 89    The application launches with `share=True`, which creates a public URL
 90    for remote access. Set to `False` for local-only access.
 91
 92    Custom CSS is injected to style the carbon counter with a gradient
 93    background and appropriate sizing.
 94
 95    See Also
 96    --------
 97    data_exploration_tab : Data exploration UI section
 98    model_training_tab : Model training UI section
 99    model_evaluation_tab : Model evaluation UI section
100    """
101    with gr.Blocks(title="Garbage Classifier Demo") as demo:
102        # Header with carbon counter
103        with gr.Row():
104            gr.Markdown("# 🗑️♻️ Garbage Classifier Interactive Demo")
105            carbon_display = gr.HTML(
106                value=update_carbon_display(), elem_id="carbon-counter"
107            )
108
109        # Tabs
110        with gr.Tabs():
111            with gr.Tab("Data Exploration"):
112                data_exploration_tab()
113
114            with gr.Tab("Training Interface"):
115                model_training_tab(carbon_display)
116
117            with gr.Tab("Model Evaluation"):
118                model_evaluation_tab(carbon_display)
119
120        # Add custom CSS for the carbon counter
121        demo.load(
122            fn=None,
123            js="""
124            function() {
125                const style = document.createElement('style');
126                style.textContent = `
127                    #carbon-counter {
128                        color: white !important;
129                        font-size: 1.0em;
130                        font-weight: bold;
131                        padding: 12px 20px;
132                        background: \
133                            linear-gradient(135deg, #667eea 0%, #764ba2 100%);
134                        border-radius: 8px;
135                        margin-left: auto;
136                        min-width: 500px;
137                        max-width: 800px;
138                    }
139                    #carbon-counter * {
140                        color: white !important;
141                    }
142                `;
143                document.head.appendChild(style);
144            }
145            """,
146        )
147
148    demo.launch(share=True)
149
150
151if __name__ == "__main__":
152    main()
def get_emissions_path():
39def get_emissions_path():
40    """
41    Get the path to the emissions CSV file.
42
43    Returns
44    -------
45    pathlib.Path
46        Path object pointing to the emissions.csv file in the model directory.
47
48    Notes
49    -----
50    The emissions file is located in the same directory as the trained model
51    checkpoint, as defined in the configuration.
52    """
53    return Path(cfg.MODEL_PATH).parent / "emissions.csv"

Get the path to the emissions CSV file.

Returns
  • pathlib.Path: Path object pointing to the emissions.csv file in the model directory.
Notes

The emissions file is located in the same directory as the trained model checkpoint, as defined in the configuration.

def update_carbon_display():
56def update_carbon_display():
57    """
58    Update the carbon footprint display with latest emissions data.
59
60    Returns
61    -------
62    str
63        HTML-formatted string containing the total carbon emissions statistics,
64        including CO₂ equivalent mass and car distance equivalent.
65
66    Notes
67    -----
68    This function reads the emissions CSV file and formats the cumulative
69    carbon footprint for display in the Gradio interface header.
70    """
71    return format_total_emissions_display(get_emissions_path())

Update the carbon footprint display with latest emissions data.

Returns
  • str: HTML-formatted string containing the total carbon emissions statistics, including CO₂ equivalent mass and car distance equivalent.
Notes

This function reads the emissions CSV file and formats the cumulative carbon footprint for display in the Gradio interface header.

def main():
 74def main():
 75    """
 76    Launch the main Gradio application interface.
 77
 78    Creates and configures a multi-tab Gradio interface with data exploration,
 79    model training, and evaluation capabilities. Includes a persistent carbon
 80    footprint counter in the header and custom CSS styling.
 81
 82    The interface includes:
 83    - Header with project title and carbon counter
 84    - Tab 1: Data Exploration (dataset visualization and analysis)
 85    - Tab 2: Training Interface (model training with custom parameters)
 86    - Tab 3: Model Evaluation (metrics visualization and inference)
 87
 88    Notes
 89    -----
 90    The application launches with `share=True`, which creates a public URL
 91    for remote access. Set to `False` for local-only access.
 92
 93    Custom CSS is injected to style the carbon counter with a gradient
 94    background and appropriate sizing.
 95
 96    See Also
 97    --------
 98    data_exploration_tab : Data exploration UI section
 99    model_training_tab : Model training UI section
100    model_evaluation_tab : Model evaluation UI section
101    """
102    with gr.Blocks(title="Garbage Classifier Demo") as demo:
103        # Header with carbon counter
104        with gr.Row():
105            gr.Markdown("# 🗑️♻️ Garbage Classifier Interactive Demo")
106            carbon_display = gr.HTML(
107                value=update_carbon_display(), elem_id="carbon-counter"
108            )
109
110        # Tabs
111        with gr.Tabs():
112            with gr.Tab("Data Exploration"):
113                data_exploration_tab()
114
115            with gr.Tab("Training Interface"):
116                model_training_tab(carbon_display)
117
118            with gr.Tab("Model Evaluation"):
119                model_evaluation_tab(carbon_display)
120
121        # Add custom CSS for the carbon counter
122        demo.load(
123            fn=None,
124            js="""
125            function() {
126                const style = document.createElement('style');
127                style.textContent = `
128                    #carbon-counter {
129                        color: white !important;
130                        font-size: 1.0em;
131                        font-weight: bold;
132                        padding: 12px 20px;
133                        background: \
134                            linear-gradient(135deg, #667eea 0%, #764ba2 100%);
135                        border-radius: 8px;
136                        margin-left: auto;
137                        min-width: 500px;
138                        max-width: 800px;
139                    }
140                    #carbon-counter * {
141                        color: white !important;
142                    }
143                `;
144                document.head.appendChild(style);
145            }
146            """,
147        )
148
149    demo.launch(share=True)

Launch the main Gradio application interface.

Creates and configures a multi-tab Gradio interface with data exploration, model training, and evaluation capabilities. Includes a persistent carbon footprint counter in the header and custom CSS styling.

The interface includes:

  • Header with project title and carbon counter
  • Tab 1: Data Exploration (dataset visualization and analysis)
  • Tab 2: Training Interface (model training with custom parameters)
  • Tab 3: Model Evaluation (metrics visualization and inference)
Notes

The application launches with share=True, which creates a public URL for remote access. Set to False for local-only access.

Custom CSS is injected to style the carbon counter with a gradient background and appropriate sizing.

See Also

data_exploration_tab: Data exploration UI section
model_training_tab: Model training UI section
model_evaluation_tab: Model evaluation UI section