import streamlit as st import requests from PIL import Image import io import numpy as np import os import zipfile # ───────────────────────────────────────────── # PAGE CONFIG # ───────────────────────────────────────────── st.set_page_config( page_title="MNIST Digit Classifier", page_icon="🔢", layout="wide", initial_sidebar_state="expanded", ) # ───────────────────────────────────────────── # CUSTOM CSS – Dark / Professional Theme # ───────────────────────────────────────────── st.markdown(""" """, unsafe_allow_html=True) # ───────────────────────────────────────────── # CONSTANTS # ───────────────────────────────────────────── API_URL = "http://127.0.0.1:8000/predict/" SAMPLE_DIR = "sample_digits" # ───────────────────────────────────────────── # HELPERS # ───────────────────────────────────────────── def call_api(image_bytes: bytes, filename: str) -> dict | None: try: resp = requests.post( API_URL, files={"file": (filename, image_bytes, "image/png")}, timeout=10, ) resp.raise_for_status() return resp.json() except Exception as e: return {"error": str(e)} def make_zip_of_samples() -> bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: for i in range(10): path = os.path.join(SAMPLE_DIR, f"digit_{i}.png") if os.path.exists(path): zf.write(path, f"digit_{i}.png") return buf.getvalue() def confidence_html(conf: float) -> str: pct = round(conf * 100, 1) color = "#4ade80" if conf > 0.90 else "#facc15" if conf > 0.70 else "#f87171" return f"""
{pct}% confidence
""" # ───────────────────────────────────────────── # HERO # ───────────────────────────────────────────── st.markdown("""Upload a handwritten digit image and get instant AI-powered prediction
Muhammad Taha Sattar Arain
Founder & CEO — Alpha Orbit
SMIT Batch 10 — AI & Data Science
📧 taha@alphaorbit.site
🔗 linkedin.com/in/taha-arain
🐙 github.com/funterpie
🌐 alphaorbit.site
📝 tahatradz.online
Assignment SMIT AI&DS — Deep Learning
MNIST Classification via FastAPI
File: {fname}
Download sample MNIST-style images to test the classifier.
", unsafe_allow_html=True, ) # Download all as ZIP if os.path.exists(SAMPLE_DIR): zip_bytes = make_zip_of_samples() st.download_button( label="📦 Download All Samples (ZIP)", data=zip_bytes, file_name="mnist_sample_digits.zip", mime="application/zip", use_container_width=True, ) st.markdown("**Or download individually:**") # Display in 5-column grid rows = [range(0, 5), range(5, 10)] for row in rows: cols = st.columns(5) for i, digit in enumerate(row): path = os.path.join(SAMPLE_DIR, f"digit_{digit}.png") if os.path.exists(path): with cols[i]: img_sample = Image.open(path) # Scale up for display display_img = img_sample.resize((80, 80), Image.NEAREST) st.image(display_img, caption=f"Digit {digit}", use_container_width=False) with open(path, "rb") as f: st.download_button( label=f"↓ {digit}", data=f.read(), file_name=f"digit_{digit}.png", mime="image/png", key=f"dl_{digit}", use_container_width=True, ) else: st.warning("Sample images not found. Make sure `sample_digits/` folder is in the same directory as `app.py`.") # ───────────────────────────────────────────── # FOOTER # ───────────────────────────────────────────── st.markdown("---") st.markdown("""