import numpy as np import keras from PIL import Image import gradio as gr MODEL_PATH = "small32cnn_mlbt_mmat.keras" STATS_PATH = "jet_image_scale_and_stats.npz" model = keras.models.load_model(MODEL_PATH, compile=False) stats = np.load(STATS_PATH) SCALE = float(stats["SCALE"]) MEAN = float(stats["MEAN"]) STD = float(stats["STD"]) print("Loaded SCALE/MEAN/STD:", SCALE, MEAN, STD, flush=True) CLASS_NAMES = ["MMAT", "MLBT"] # --------------------------------------------------------- # PREPROCESSING # --------------------------------------------------------- def preprocess(img: np.ndarray) -> np.ndarray: if img.ndim == 3 and img.shape[2] == 3: img_gray = np.dot(img[..., :3], [0.2989, 0.5870, 0.1140]) elif img.ndim == 3 and img.shape[2] == 1: img_gray = img[..., 0] elif img.ndim == 2: img_gray = img else: img_gray = img[..., 0] pil_img = Image.fromarray(img_gray.astype("uint8")) pil_img = pil_img.resize((32, 32), Image.BILINEAR) arr = np.array(pil_img).astype("float32") arr_unscaled = arr / SCALE arr_norm = (arr_unscaled - MEAN) / (STD + 1e-8) arr_norm = arr_norm[None, ..., None] return arr_norm # --------------------------------------------------------- # PREDICT FUNCTION # --------------------------------------------------------- def predict(img: np.ndarray): x = preprocess(img) raw = float(model.predict(x, verbose=0)[0, 0]) prob_mlbt = raw prob_mmat = 1.0 - prob_mlbt return {"MLBT": prob_mlbt, "MMAT": prob_mmat} # --------------------------------------------------------- # ABOUT PAGE — Flip-cards recreated in HTML/CSS # --------------------------------------------------------- about_html = """

Learn more about high-energy physics! ⚛️

🔍 Key Concepts

QGP click to learn more
Quark–Gluon Plasma (QGP)

A super-hot state of matter where quarks and gluons move freely.

Helps us understand conditions microseconds after the Big Bang.
Pb–Pb click to learn more
Lead–Lead Collisions

High-energy lead nuclei collisions that create QGP.

Lets us study how jets lose energy in extreme matter.
Jet click to learn more
Jets & Jet Quenching

Sprays of particles created during collisions.

Energy loss → reveals QGP properties.
αₛ click to learn more
Strong Coupling Constant (αₛ)

Measures how strongly quarks bind together.

Different αₛ values help model jet energy loss.
Q₀ click to learn more
Virtuality Scale (Q₀)

Determines how quantum or classical jet evolution is.

Controls which processes dominate in QGP.
MATTER click to learn more
MATTER Model

Describes energy loss via radiation + elastic collisions.

Represents early jet evolution.
MATTER–LBT click to learn more
MATTER–LBT Model

Hybrid model combining radiation + medium scattering.

Simulates realistic QGP interactions.
""" # --------------------------------------------------------- # FINAL GRADIO APP WITH TABS # --------------------------------------------------------- with gr.Blocks() as demo: gr.Markdown("

MLBT vs MMAT Jet Classifier

") with gr.Tabs(): with gr.Tab("🔬 Classifier"): input_component = gr.Image(type="numpy", label="Upload a jet image") output_component = gr.Label(num_top_classes=2, label="Predicted probabilities") input_component.change(predict, inputs=input_component, outputs=output_component) with gr.Tab("📘 About"): gr.HTML(about_html) if __name__ == "__main__": demo.launch()