Anon commited on
Commit
ee645d7
·
1 Parent(s): e16ad82

update requirements.

Browse files
Files changed (2) hide show
  1. app.py +208 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # --- Placeholder image paths (local files in same repo) ---
4
+ CAT_IMG_URL = "https://placekitten.com/256/256" # swap with local cat if you want
5
+ UNICORN_WITH_HORN_PATH = "unicorn_with_horn.png"
6
+ UNICORN_NO_HORN_PATH = "unicorn_without_horn.png"
7
+
8
+ # --- Dummy callbacks ---
9
+
10
+ def steer_spectacles(strength: float):
11
+ """
12
+ Placeholder: given a steering strength, return (left_img, right_img).
13
+ Later you can replace this with your actual spectacles-mapping logic.
14
+ """
15
+ return CAT_IMG_URL, CAT_IMG_URL
16
+
17
+ def run_ablation(layer_name: str, strength: float):
18
+ """
19
+ Placeholder ablation function: currently just returns None.
20
+ Replace with your actual ablation pipeline and return an image.
21
+ """
22
+ # TODO: hook layer_name + strength into your UNet / transformer ablation
23
+ return None
24
+
25
+
26
+ with gr.Blocks() as demo:
27
+ # Inject CSS via HTML so it works on older Gradio versions
28
+ gr.HTML("""
29
+ <style>
30
+ .tooltip-bubble {
31
+ position: relative;
32
+ display: inline-block;
33
+ cursor: help;
34
+ padding: 4px 8px;
35
+ border-radius: 6px;
36
+ border: 1px solid #ccc;
37
+ font-size: 0.85rem;
38
+ margin: 4px;
39
+ }
40
+ .tooltip-bubble .tooltip-text {
41
+ visibility: hidden;
42
+ width: 260px;
43
+ background-color: #111;
44
+ color: #fff;
45
+ text-align: left;
46
+ border-radius: 8px;
47
+ padding: 8px 10px;
48
+ position: absolute;
49
+ z-index: 10;
50
+ bottom: 125%;
51
+ left: 50%;
52
+ transform: translateX(-50%);
53
+ opacity: 0;
54
+ transition: opacity 0.2s ease-in-out;
55
+ }
56
+ .tooltip-bubble .tooltip-text::after {
57
+ content: "";
58
+ position: absolute;
59
+ top: 100%;
60
+ left: 50%;
61
+ margin-left: -5px;
62
+ border-width: 5px;
63
+ border-style: solid;
64
+ border-color: #111 transparent transparent transparent;
65
+ }
66
+ .tooltip-bubble:hover .tooltip-text {
67
+ visibility: visible;
68
+ opacity: 1;
69
+ }
70
+ </style>
71
+ """)
72
+
73
+ gr.Markdown("# Steering & Ablation UI Skeleton")
74
+
75
+ # ----------------- 1. Cat spectacles steering -----------------
76
+ with gr.Group():
77
+ gr.Markdown("## Steer: Add Spectacles via Mapping")
78
+
79
+ with gr.Row():
80
+ cat_left = gr.Image(
81
+ value=CAT_IMG_URL,
82
+ label="Original cat",
83
+ interactive=False,
84
+ show_label=True
85
+ )
86
+ cat_right = gr.Image(
87
+ value=CAT_IMG_URL,
88
+ label="Steered cat (with spectacles)",
89
+ interactive=False,
90
+ show_label=True
91
+ )
92
+
93
+ steer_slider = gr.Slider(
94
+ minimum=0.0,
95
+ maximum=1.0,
96
+ value=0.0,
97
+ step=0.05,
98
+ label="Steer (add spectacles via mapping)",
99
+ info="Placeholder: hook into your spectacles-mapping call."
100
+ )
101
+
102
+ steer_slider.change(
103
+ fn=steer_spectacles,
104
+ inputs=steer_slider,
105
+ outputs=[cat_left, cat_right]
106
+ )
107
+
108
+ # ----------------- 2. Unicorns with / without horns -----------------
109
+ with gr.Group():
110
+ gr.Markdown("## Unicorns: With vs Without Horn")
111
+
112
+ with gr.Row():
113
+ unicorn_with = gr.Image(
114
+ value=UNICORN_WITH_HORN_PATH,
115
+ label="Unicorn with horn",
116
+ interactive=False,
117
+ show_label=True
118
+ )
119
+ unicorn_without = gr.Image(
120
+ value=UNICORN_NO_HORN_PATH,
121
+ label="Unicorn without horn",
122
+ interactive=False,
123
+ show_label=True
124
+ )
125
+
126
+ # ----------------- 3. Ablation experiment -----------------
127
+ with gr.Group():
128
+ gr.Markdown("## Ablation Experiment")
129
+
130
+ with gr.Row():
131
+ # Controls
132
+ with gr.Column(scale=1):
133
+ layer_dropdown = gr.Dropdown(
134
+ choices=[
135
+ "UNet.block_1",
136
+ "UNet.block_2",
137
+ "UNet.attn_mid",
138
+ "TextEncoder.layer_5",
139
+ ],
140
+ value="UNet.block_1",
141
+ label="Layer / module to ablate",
142
+ info="Populate with your actual layer names."
143
+ )
144
+
145
+ ablation_strength = gr.Slider(
146
+ minimum=0.0,
147
+ maximum=1.0,
148
+ value=0.0,
149
+ step=0.05,
150
+ label="Ablation strength",
151
+ info="0 = no ablation, 1 = full ablation."
152
+ )
153
+
154
+ # Output + explanation
155
+ with gr.Column(scale=1):
156
+ ablation_output = gr.Image(
157
+ value=None,
158
+ label="Ablated output (placeholder)",
159
+ interactive=False
160
+ )
161
+
162
+ gr.HTML("""
163
+ <div>
164
+ <p>
165
+ This panel is for an <strong>ablation experiment</strong>:
166
+ you remove or zero-out parts of the network and see how the
167
+ generated image changes.
168
+ </p>
169
+ <div class="tooltip-bubble">
170
+ Baseline vs ablated
171
+ <span class="tooltip-text">
172
+ In the real experiment, you would compare the original image
173
+ to this ablated one to see which visual attributes the
174
+ selected layer controls.
175
+ </span>
176
+ </div>
177
+ <div class="tooltip-bubble">
178
+ Layer selection
179
+ <span class="tooltip-text">
180
+ Different layers control different levels of abstraction:
181
+ early blocks affect global layout, mid blocks affect object
182
+ structure, and late blocks refine texture and style.
183
+ </span>
184
+ </div>
185
+ <div class="tooltip-bubble">
186
+ Ablation strength
187
+ <span class="tooltip-text">
188
+ Ablation strength could interpolate from no ablation (0.0),
189
+ to partial noise / zeroing (0.5), to fully removing the
190
+ layer's contribution (1.0).
191
+ </span>
192
+ </div>
193
+ </div>
194
+ """)
195
+
196
+ # Wire up the (dummy) ablation callback
197
+ layer_dropdown.change(
198
+ fn=run_ablation,
199
+ inputs=[layer_dropdown, ablation_strength],
200
+ outputs=ablation_output,
201
+ )
202
+ ablation_strength.change(
203
+ fn=run_ablation,
204
+ inputs=[layer_dropdown, ablation_strength],
205
+ outputs=ablation_output,
206
+ )
207
+
208
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio==4.44.1