UTKojima commited on
Commit
232f9db
ยท
verified ยท
1 Parent(s): 3c1334a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -7,6 +7,7 @@ import numpy as np
7
  import matplotlib.pyplot as plt
8
  from PIL import Image
9
  import gradio as gr
 
10
 
11
  # โ”€โ”€ 0. ๅฎ‰ๅ…จใช .to() ใƒ‘ใƒƒใƒ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
12
  from transformers.modeling_utils import PreTrainedModel
@@ -25,7 +26,6 @@ def _safe_to(self, *args, **kwargs):
25
 
26
  torch.nn.Module.to = _safe_to
27
  PreTrainedModel.to = _safe_to
28
- print("๐Ÿฉน Applied safe .to() patch: skip only bitsandbytes modules")
29
 
30
  # โ”€โ”€ ใ‚ฐใƒญใƒผใƒใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
31
  ฮ”t, ฮฒ = 1e-2, 1e-4 # ใ‚ทใƒณใƒ—ใƒฌใ‚ฏใƒ†ใ‚ฃใƒƒใ‚ฏ่ฃœๆญฃ็”จใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใจๅผทๅบฆ
@@ -128,13 +128,16 @@ def compare_generate(prompt: str):
128
  model_s, tok_s = load_quantized_lora_model()
129
  register_symp_hook(model_s)
130
  gen_s, H_s = generate_and_drift(model_s, tok_s, prompt)
131
- # ใ‚จใƒใƒซใ‚ฎใƒผใƒ‰ใƒชใƒ•ใƒˆ็ตฑ่จˆ้‡่จˆ็ฎ—
132
- def stats(H):
133
  arr = np.array(H)
134
- return f"min={arr.min():.1f}, max={arr.max():.1f}, mean={arr.mean():.1f}, median={np.median(arr):.1f}, std={arr.std():.1f}"
135
- stat_b = stats(H_b)
136
- stat_s = stats(H_s)
137
- # ใƒ‰ใƒชใƒ•ใƒˆๆฏ”่ผƒใƒ—ใƒญใƒƒใƒˆ
 
 
 
138
  buf = io.BytesIO()
139
  plt.figure()
140
  plt.plot(H_b, label="Baseline")
@@ -148,7 +151,11 @@ def compare_generate(prompt: str):
148
  plt.close()
149
  buf.seek(0)
150
  img = Image.open(buf)
151
- return gen_b, gen_s, stat_b, stat_s, img
 
 
 
 
152
 
153
  # โ”€โ”€ 7. Gradio UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
154
  iface = gr.Interface(
@@ -159,10 +166,11 @@ iface = gr.Interface(
159
  gr.Textbox(label="Symplectic Generation"),
160
  gr.Textbox(label="Baseline Stats"),
161
  gr.Textbox(label="Symplectic Stats"),
 
162
  gr.Image(label="Energy Drift Comparison")
163
  ],
164
  title="Baseline vs Symplectic Control Demo",
165
- description="Compare generation, statistics & energy drift: baseline vs symplectic correction"
166
  )
167
 
168
  if __name__ == "__main__":
 
7
  import matplotlib.pyplot as plt
8
  from PIL import Image
9
  import gradio as gr
10
+ import scipy.stats as stats
11
 
12
  # โ”€โ”€ 0. ๅฎ‰ๅ…จใช .to() ใƒ‘ใƒƒใƒ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
13
  from transformers.modeling_utils import PreTrainedModel
 
26
 
27
  torch.nn.Module.to = _safe_to
28
  PreTrainedModel.to = _safe_to
 
29
 
30
  # โ”€โ”€ ใ‚ฐใƒญใƒผใƒใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
31
  ฮ”t, ฮฒ = 1e-2, 1e-4 # ใ‚ทใƒณใƒ—ใƒฌใ‚ฏใƒ†ใ‚ฃใƒƒใ‚ฏ่ฃœๆญฃ็”จใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใจๅผทๅบฆ
 
128
  model_s, tok_s = load_quantized_lora_model()
129
  register_symp_hook(model_s)
130
  gen_s, H_s = generate_and_drift(model_s, tok_s, prompt)
131
+ # ็ตฑ่จˆ้‡
132
+ def compute_stats(H):
133
  arr = np.array(H)
134
+ return arr.min(), arr.max(), arr.mean(), np.median(arr), arr.std()
135
+ stats_b = compute_stats(H_b)
136
+ stats_s = compute_stats(H_s)
137
+ # ๆœ‰ๆ„ๅทฎๆคœๅฎš
138
+ t_stat, p_val = stats.ttest_ind(H_b, H_s, equal_var=False)
139
+ lev_stat, lev_p = stats.levene(H_b, H_s)
140
+ # ใƒ—ใƒญใƒƒใƒˆ
141
  buf = io.BytesIO()
142
  plt.figure()
143
  plt.plot(H_b, label="Baseline")
 
151
  plt.close()
152
  buf.seek(0)
153
  img = Image.open(buf)
154
+ # ใƒ†ใ‚ญใ‚นใƒˆๅ‡บๅŠ›็”จ
155
+ stat_text_b = f"Baseline: min={stats_b[0]:.1f}, max={stats_b[1]:.1f}, mean={stats_b[2]:.1f}, median={stats_b[3]:.1f}, std={stats_b[4]:.1f}"
156
+ stat_text_s = f"Symplectic: min={stats_s[0]:.1f}, max={stats_s[1]:.1f}, mean={stats_s[2]:.1f}, median={stats_s[3]:.1f}, std={stats_s[4]:.1f}"
157
+ test_text = f"t-test p-value={p_val:.3f}, Levene p-value={lev_p:.3f}"
158
+ return gen_b, gen_s, stat_text_b, stat_text_s, test_text, img
159
 
160
  # โ”€โ”€ 7. Gradio UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
161
  iface = gr.Interface(
 
166
  gr.Textbox(label="Symplectic Generation"),
167
  gr.Textbox(label="Baseline Stats"),
168
  gr.Textbox(label="Symplectic Stats"),
169
+ gr.Textbox(label="Significance Tests"),
170
  gr.Image(label="Energy Drift Comparison")
171
  ],
172
  title="Baseline vs Symplectic Control Demo",
173
+ description="Compare generation, statistics & energy drift: baseline vs symplectic correction with significance tests"
174
  )
175
 
176
  if __name__ == "__main__":