Will Phoenix commited on
Commit
bbfb9f6
·
1 Parent(s): 1c883aa

Add demo model selection to Gradio app

Browse files
Files changed (1) hide show
  1. mithridatium/gradio_app.py +54 -12
mithridatium/gradio_app.py CHANGED
@@ -27,6 +27,12 @@ except PackageNotFoundError:
27
  HIDDEN_DATASET_CHOICES = {"cifar10_for_imagenet", "fake_imagenet"}
28
  DATASET_CHOICES = sorted(set(utils.DATASET_CONFIGS) - HIDDEN_DATASET_CHOICES)
29
  UI_DEFENSE_CHOICES = sorted((set(DEFENSES) | {"freeeagle"}) - {"aeva"})
 
 
 
 
 
 
30
  UI_CSS = """
31
  .gradio-container .run-detection-btn,
32
  .gradio-container .run-detection-btn button,
@@ -66,6 +72,8 @@ def _write_temp_json(payload: dict[str, Any]) -> str:
66
 
67
 
68
  def _run_detection_from_ui(
 
 
69
  model_file: str | None,
70
  dataset: str,
71
  defense: str,
@@ -78,12 +86,23 @@ def _run_detection_from_ui(
78
  logs.append(msg)
79
 
80
  try:
81
- selected_model = (model_file or "").strip()
82
- if not selected_model:
83
- raise ValueError("Upload a local model checkpoint (.pth or .pt).")
84
-
85
- logs.append("[ui] provider: local upload")
86
- logs.append(f"[ui] model source: {selected_model}")
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  detection = run_detection(
89
  model=selected_model,
@@ -126,7 +145,7 @@ def build_app() -> gr.Blocks:
126
  with gr.Blocks(title="Mithridatium UI", css=UI_CSS) as demo:
127
  gr.Markdown(
128
  "## Mithridatium Detection UI\n"
129
- "Upload a local checkpoint and run a defense."
130
  )
131
 
132
  defense = gr.Dropdown(
@@ -135,11 +154,32 @@ def build_app() -> gr.Blocks:
135
  label="Defense",
136
  )
137
 
138
- model_file = gr.File(
139
- label="Upload Local Model Checkpoint (.pth or .pt)",
140
- file_count="single",
141
- file_types=[".pth", ".pt"],
142
- type="filepath",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  )
144
 
145
  dataset = gr.Dropdown(
@@ -174,6 +214,8 @@ def build_app() -> gr.Blocks:
174
  run_btn.click(
175
  fn=_run_detection_from_ui,
176
  inputs=[
 
 
177
  model_file,
178
  dataset,
179
  defense,
 
27
  HIDDEN_DATASET_CHOICES = {"cifar10_for_imagenet", "fake_imagenet"}
28
  DATASET_CHOICES = sorted(set(utils.DATASET_CONFIGS) - HIDDEN_DATASET_CHOICES)
29
  UI_DEFENSE_CHOICES = sorted((set(DEFENSES) | {"freeeagle"}) - {"aeva"})
30
+ MODEL_SOURCE_DEMO = "demo"
31
+ MODEL_SOURCE_UPLOAD = "upload"
32
+ DEMO_MODELS = {
33
+ "clean_resnet18_cifar10": "models/resnet18_cifar10.pt",
34
+ "backdoored_resnet18_cifar10": "models/resnet18_poison.pt",
35
+ }
36
  UI_CSS = """
37
  .gradio-container .run-detection-btn,
38
  .gradio-container .run-detection-btn button,
 
72
 
73
 
74
  def _run_detection_from_ui(
75
+ model_source: str,
76
+ demo_model: str,
77
  model_file: str | None,
78
  dataset: str,
79
  defense: str,
 
86
  logs.append(msg)
87
 
88
  try:
89
+ source_key = str(model_source or MODEL_SOURCE_DEMO).strip().lower()
90
+ if source_key == MODEL_SOURCE_DEMO:
91
+ demo_name = str(demo_model or "").strip()
92
+ selected_model = DEMO_MODELS.get(demo_name, "")
93
+ if not selected_model:
94
+ supported = ", ".join(DEMO_MODELS)
95
+ raise ValueError(f"Choose a demo model. Supported demo models: {supported}.")
96
+ if not Path(selected_model).is_file():
97
+ raise ValueError(f"Demo model is not available in this Space: {selected_model}")
98
+ logs.append("[ui] model source: demo model")
99
+ logs.append(f"[ui] demo model: {demo_name}")
100
+ else:
101
+ selected_model = (model_file or "").strip()
102
+ if not selected_model:
103
+ raise ValueError("Upload a local model checkpoint (.pth or .pt).")
104
+ logs.append("[ui] model source: uploaded file")
105
+ logs.append(f"[ui] uploaded model: {selected_model}")
106
 
107
  detection = run_detection(
108
  model=selected_model,
 
145
  with gr.Blocks(title="Mithridatium UI", css=UI_CSS) as demo:
146
  gr.Markdown(
147
  "## Mithridatium Detection UI\n"
148
+ "Choose a demo checkpoint or upload a local checkpoint, then run a defense."
149
  )
150
 
151
  defense = gr.Dropdown(
 
154
  label="Defense",
155
  )
156
 
157
+ model_source = gr.State(MODEL_SOURCE_DEMO)
158
+
159
+ with gr.Tabs():
160
+ with gr.Tab("Demo Model") as demo_model_tab:
161
+ demo_model = gr.Radio(
162
+ choices=list(DEMO_MODELS),
163
+ value="clean_resnet18_cifar10",
164
+ label="Demo Model",
165
+ )
166
+ with gr.Tab("Upload Model") as upload_model_tab:
167
+ model_file = gr.File(
168
+ label="Upload Local Model Checkpoint (.pth or .pt)",
169
+ file_count="single",
170
+ file_types=[".pth", ".pt"],
171
+ type="filepath",
172
+ )
173
+
174
+ demo_model_tab.select(
175
+ fn=lambda: MODEL_SOURCE_DEMO,
176
+ inputs=None,
177
+ outputs=model_source,
178
+ )
179
+ upload_model_tab.select(
180
+ fn=lambda: MODEL_SOURCE_UPLOAD,
181
+ inputs=None,
182
+ outputs=model_source,
183
  )
184
 
185
  dataset = gr.Dropdown(
 
214
  run_btn.click(
215
  fn=_run_detection_from_ui,
216
  inputs=[
217
+ model_source,
218
+ demo_model,
219
  model_file,
220
  dataset,
221
  defense,