File size: 2,113 Bytes
d10f973
 
 
 
 
 
 
 
49539a5
d10f973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49539a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10f973
49539a5
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
import gradio as gr
from PIL import Image
import torch

from uei_core.models import ModelPortfolio
from uei_core.uncertainty import UncertaintyEstimator
from uei_core.energy import EnergyProfiler
from uei_core.policy import UEIPolicy
from visuals import get_visual_ui
device = "cpu"  # M1 CPU is optimized enough; GPU optional

models = ModelPortfolio(device=device)
unc = UncertaintyEstimator()
energy = EnergyProfiler()
policy = UEIPolicy()


def uei_infer(img):

    x = models.preprocess(img)

    # Step 1: Small model
    logits_s, E_s = energy.measure(models.infer_small, x)
    U_s = unc.estimate(logits_s)

    # Step 2: Large model (to measure marginal utility)
    logits_l, E_l = energy.measure(models.infer_large, x)
    U_l = unc.estimate(logits_l)

    decision = policy.decide(U_s, U_l, E_s, E_l)

    if decision == "small":
        final_logits = logits_s
        energy_used = E_s
        unc_used = U_s
        model_name = "Low-Energy Small Model"
    else:
        final_logits = logits_l
        energy_used = E_l
        unc_used = U_l
        model_name = "High-Energy Large Model"

    probs = torch.softmax(final_logits, dim=1).squeeze()
    top_idx = torch.argmax(probs).item()
    confidence = float(probs[top_idx])

    return {
        "Predicted Class Index": top_idx,
        "Confidence": confidence,
        "Selected Model": model_name,
        "Uncertainty": float(unc_used),
        "Energy (proxy units)": energy_used
    }


demo = gr.Interface(
    fn=uei_infer,
    inputs=gr.Image(type="pil"),
    outputs=gr.JSON(),
    title="Uncertainty-Elastic Inference (UEI)",
    description="Energy-efficient inference by dynamically selecting models based on uncertainty-energy tradeoffs."
)

# ---------------------------
# NEW UI: Add Visualization Tab
# ---------------------------
visual_tab = get_visual_ui()

app = gr.TabbedInterface(
    [
        demo,            # your original UEI inference app
        visual_tab       # new visualization dashboard
    ],
    [
        "UEI Inference",
        "Visual Dashboard"
    ]
)

if __name__ == "__main__":
    app.launch()