Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 133 |
arr = np.array(H)
|
| 134 |
-
return
|
| 135 |
-
|
| 136 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|