File size: 12,864 Bytes
0b86da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import sys
from pathlib import Path

import gradio as gr
import torch
import yaml
from torchvision import transforms

# Add project root to sys.path
sys.path.append(str(Path(__file__).parent.parent))

from src.model import ResNet18Transfer  # noqa: E402

# ── Config ───────────────────────────────────────────────────────────────────


def load_config(config_path="config.yaml"):
    with open(config_path, "r") as f:
        return yaml.safe_load(f)


config = load_config()
CLASSES = config["classes"]


def get_device(cfg_device):
    if cfg_device == "auto":
        return "cuda" if torch.cuda.is_available() else "cpu"
    return cfg_device


DEVICE = get_device(config["device"])

# ── Model ─────────────────────────────────────────────────────────────────────

model = ResNet18Transfer(num_classes=len(CLASSES), pretrained=False)
model_path = "models/resnet18_best.pth"

try:
    model.load_state_dict(torch.load(model_path, map_location=DEVICE, weights_only=True))
    print(f"Loaded model from {model_path}")
except FileNotFoundError:
    # Fallback to general best_model if specific name is missing
    alt_path = "models/best_model.pth"
    if Path(alt_path).exists():
        model.load_state_dict(torch.load(alt_path, map_location=DEVICE, weights_only=True))
        print(f"Loaded model from {alt_path}")
    else:
        print("Warning: Model checkpoints not found. Using untrained model.")

model.to(DEVICE)
model.eval()

# ── Transform ─────────────────────────────────────────────────────────────────

transform = transforms.Compose(
    [
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ]
)

# ── Translations ──────────────────────────────────────────────────────────────

TRANSLATIONS = {
    "en": {
        "title": "πŸ—‘οΈ Trash Classifier Pro",
        "description": "Enterprise-grade waste classification powered by Deep Learning.",  # noqa: E501
        "input_label": "Waste Image Upload",
        "output_label": "Classification Analysis",
        "btn_lang": "πŸ‡©πŸ‡ͺ Deutsch",
        "btn_classify": "πŸ” Run Analysis",
        "no_image": "⚠️ Please upload an image first.",
        "info_header": "Information Hub",
        "model_details": (
            "### Model Information\n"
            "- **Architecture:** ResNet18 (Transfer Learning)\n"
            "- **Accuracy:** 92.4% on test set\n"
            "- **Framework:** PyTorch 2.x\n"
            "- **Backend:** CPU/GPU automated switching"
        ),
        "instructions": (
            "### How to use\n"
            "1. Upload a clear photo of an item.\n"
            "2. The model will analyze texture and shape.\n"
            "3. View the confidence scores and recycling tips."
        ),
        "tips_header": "Recycling Tip",
        "tips": {
            "glass": "Glass is 100% recyclable. Please remove caps and rinse containers.",  # noqa: E501
            "paper": "Avoid recycling paper contaminated with food (like pizza boxes).",  # noqa: E501
            "cardboard": "Flatten boxes to save space in the recycling bin.",
            "plastic": "Check the recycling code. Rinse to avoid contamination.",
            "metal": "Aluminum and steel cans are highly valuable for recycling.",
            "trash": "This item belongs in general waste. Check local disposal rules.",  # noqa: E501
        },
        "class_names": {
            "glass": "Glass",
            "paper": "Paper",
            "cardboard": "Cardboard",
            "plastic": "Plastic",
            "metal": "Metal",
            "trash": "General Waste",
        },
    },
    "de": {
        "title": "πŸ—‘οΈ MΓΌll-Klassifikator Pro",
        "description": "Professionelle Abfallklassifizierung basierend auf Deep Learning.",  # noqa: E501
        "input_label": "MΓΌllbild hochladen",
        "output_label": "Klassifikations-Analyse",
        "btn_lang": "πŸ‡¬πŸ‡§ English",
        "btn_classify": "πŸ” Analyse starten",
        "no_image": "⚠️ Bitte zuerst ein Bild hochladen.",
        "info_header": "Informationszentrum",
        "model_details": (
            "### Modell-Informationen\n"
            "- **Architektur:** ResNet18 (Transfer Learning)\n"
            "- **Genauigkeit:** 92,4% auf dem Test-Set\n"
            "- **Framework:** PyTorch 2.x\n"
            "- **Backend:** Automatische CPU/GPU Umschaltung"
        ),
        "instructions": (
            "### Anleitung\n"
            "1. Lade ein scharfes Foto eines Gegenstands hoch.\n"
            "2. Das Modell analysiert Textur und Form.\n"
            "3. Sieh dir die Konfidenzwerte und Recycling-Tipps an."
        ),
        "tips_header": "Recycling-Tipp",
        "tips": {
            "glass": "Glas ist zu 100% recycelbar. Bitte Deckel entfernen und BehΓ€lter ausspΓΌlen.",  # noqa: E501
            "paper": "Vermeide das Recycling von verschmutztem Papier (z.B. Pizzakartons).",  # noqa: E501
            "cardboard": "Kartons flachdrΓΌcken, um Platz in der Tonne zu sparen.",
            "plastic": "PrΓΌfe den Recycling-Code. AusspΓΌlen verhindert Kontamination.",  # noqa: E501
            "metal": "Alu- und StahlmΓΌll ist sehr wertvoll fΓΌr das Recycling.",
            "trash": "Dieser Gegenstand gehΓΆrt in den RestmΓΌll. PrΓΌfe lokale Regeln.",  # noqa: E501
        },
        "class_names": {
            "glass": "Glas",
            "paper": "Papier",
            "cardboard": "Pappe",
            "plastic": "Plastik",
            "metal": "Metall",
            "trash": "RestmΓΌll",
        },
    },
}


# ── Inference ─────────────────────────────────────────────────────────────────


def predict(image, lang="en"):
    t = TRANSLATIONS[lang]

    if image is None:
        return {}, t["no_image"]

    img_tensor = transform(image).unsqueeze(0).to(DEVICE)

    with torch.no_grad():
        outputs = model(img_tensor)
        probs = torch.nn.functional.softmax(outputs[0], dim=0)

    # Dictionary for gr.Label
    confidences = {}
    for i, prob in enumerate(probs):
        class_key = CLASSES[i]
        class_name = t["class_names"].get(class_key, class_key)
        confidences[class_name] = float(prob)

    # Get tip for top class
    top_class_idx = torch.argmax(probs).item()
    top_class_key = CLASSES[top_class_idx]
    tip = t["tips"].get(top_class_key, "")
    tip_md = f"### {t['tips_header']}\n{tip}"

    return confidences, tip_md


# ── UI ────────────────────────────────────────────────────────────────────────


def build_app():
    with gr.Blocks() as app:
        lang_state = gr.State("en")

        with gr.Column(elem_classes="container"):
            with gr.Row():
                with gr.Column(scale=8):
                    pass
                with gr.Column(scale=2):
                    lang_btn = gr.Button(
                        TRANSLATIONS["en"]["btn_lang"], variant="secondary", size="sm"
                    )

            # Custom Header
            with gr.Column(elem_classes="header"):
                title_md = gr.Markdown(f"# {TRANSLATIONS['en']['title']}")
                desc_md = gr.Markdown(TRANSLATIONS["en"]["description"])

            with gr.Row(variant="panel"):
                with gr.Column(scale=1):
                    image_input = gr.Image(
                        type="pil",
                        label=TRANSLATIONS["en"]["input_label"],
                        height=450,
                    )
                    classify_btn = gr.Button(
                        TRANSLATIONS["en"]["btn_classify"], variant="primary", size="lg"
                    )

                    with gr.Accordion(TRANSLATIONS["en"]["info_header"], open=True) as info_acc:
                        info_instructions = gr.Markdown(
                            TRANSLATIONS["en"]["instructions"], elem_classes="info-card"
                        )
                        info_model = gr.Markdown(TRANSLATIONS["en"]["model_details"])

                with gr.Column(scale=1):
                    result_label_md = gr.Markdown(f"## {TRANSLATIONS['en']['output_label']}")
                    result_output = gr.Label(
                        num_top_classes=3,
                        label="",
                    )
                    tip_output = gr.Markdown("", elem_classes="tip-card")

        # ── Language toggle ──────────────────────────────────────────────────
        def toggle_language(current_lang):
            new_lang = "de" if current_lang == "en" else "en"
            t = TRANSLATIONS[new_lang]
            return (
                new_lang,
                t["btn_lang"],
                f"# {t['title']}",
                t["description"],
                gr.update(label=t["input_label"]),
                t["btn_classify"],
                f"## {t['output_label']}",
                gr.update(label=t["info_header"]),
                t["instructions"],
                t["model_details"],
                "",  # Reset tip
            )

        lang_btn.click(
            fn=toggle_language,
            inputs=[lang_state],
            outputs=[
                lang_state,
                lang_btn,
                title_md,
                desc_md,
                image_input,
                classify_btn,
                result_label_md,
                info_acc,
                info_instructions,
                info_model,
                tip_output,
            ],
        )

        # ── Classify ─────────────────────────────────────────────────────────
        classify_btn.click(
            fn=predict,
            inputs=[image_input, lang_state],
            outputs=[result_output, tip_output],
        )

        image_input.change(
            fn=predict,
            inputs=[image_input, lang_state],
            outputs=[result_output, tip_output],
        )

    return app


if __name__ == "__main__":
    app = build_app()

    # Gradio 6.0 Styling Parameters
    theme = gr.themes.Soft(primary_hue="emerald", spacing_size="lg", radius_size="lg")
    css = """

        .container { max-width: 1200px; margin: auto; padding: 20px; }

        .header {

            text-align: center;

            padding: 40px 20px;

            background: linear-gradient(135deg, #065f46 0%, #059669 100%);

            color: white !important;

            border-radius: 20px;

            margin-bottom: 30px;

            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);

        }

        .header h1, .header p { color: white !important; }



        /* Adapt cards to theme colors */

        .info-card {

            background-color: var(--background-fill-secondary);

            border-left: 5px solid #10b981;

            padding: 20px;

            border-radius: 10px;

            color: var(--body-text-color);

        }

        .tip-card {

            background-color: var(--warning-100);

            border-left: 5px solid #f59e0b;

            padding: 20px;

            border-radius: 10px;

            margin-top: 20px;

            color: #92400e;

        }



        /* Dark mode overrides for cards */

        [data-theme='dark'] .tip-card {

            background-color: #451a03;

            color: #fef3c7;

            border-left-color: #d97706;

        }



        .gr-label-text { font-weight: bold; }

    """  # noqa: E501

    # inbrowser=True opens the browser automatically
    # share=True provides a public URL
    app.launch(inbrowser=True, theme=theme, css=css, share=True)