Update app.py
Browse files
app.py
CHANGED
|
@@ -2,136 +2,205 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def
|
| 7 |
x = np.linspace(0, 10, 200)
|
| 8 |
-
y = 1 / (1 + np.exp(-
|
| 9 |
-
|
| 10 |
-
fig, ax = plt.subplots(figsize=(4.5, 3.2))
|
| 11 |
-
ax.plot(x, y, linewidth=2)
|
| 12 |
-
ax.set_title("Entrenchment Curve (Individual Level)")
|
| 13 |
-
ax.set_xlabel("Usage Frequency")
|
| 14 |
-
ax.set_ylabel("Cognitive Strength")
|
| 15 |
-
fig.tight_layout()
|
| 16 |
-
return fig
|
| 17 |
-
|
| 18 |
-
# ---- 生成常规化(Conventionalization)曲线 ----
|
| 19 |
-
def generate_conventionalization_curve():
|
| 20 |
-
x = np.linspace(0, 10, 200)
|
| 21 |
-
y = 1 / (1 + np.exp(-0.9*(x-4)))
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
fig.tight_layout()
|
| 29 |
-
return fig
|
| 30 |
|
| 31 |
-
#
|
| 32 |
def analyze_ec(expression):
|
| 33 |
if not expression.strip():
|
| 34 |
return "请输入一个表达。", None, None, ""
|
| 35 |
|
| 36 |
entrenchment_text = (
|
| 37 |
f"### Entrenchment(固着 — 个体层面)\n"
|
| 38 |
-
f"表达 **「{expression}」**
|
| 39 |
-
f"-
|
| 40 |
-
f"-
|
| 41 |
-
f"-
|
| 42 |
)
|
| 43 |
|
| 44 |
conventionalization_text = (
|
| 45 |
f"### Conventionalization(常规化 — 社群层面)\n"
|
| 46 |
-
f"
|
| 47 |
-
f"-
|
| 48 |
-
f"-
|
| 49 |
-
f"-
|
| 50 |
)
|
| 51 |
|
| 52 |
ec_text = (
|
| 53 |
-
f"###
|
| 54 |
-
f"
|
| 55 |
-
f"
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
return entrenchment_text + "\n\n" + conventionalization_text, fig_e, fig_c, ec_text
|
| 62 |
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Blocks(
|
| 67 |
css="""
|
| 68 |
-
.card {
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
border-radius: 12px;
|
| 72 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
|
| 73 |
-
margin-bottom: 12px;
|
| 74 |
-
}
|
| 75 |
-
.title-area {
|
| 76 |
-
text-align:center;
|
| 77 |
-
margin-bottom: 20px;
|
| 78 |
-
}
|
| 79 |
-
.title-area h1 {
|
| 80 |
-
color:#334155;
|
| 81 |
-
margin-bottom:5px;
|
| 82 |
-
}
|
| 83 |
-
.title-area h3 {
|
| 84 |
-
color:#475569;
|
| 85 |
-
font-weight: normal;
|
| 86 |
-
}
|
| 87 |
-
body {
|
| 88 |
-
background: linear-gradient(135deg, #eef2ff, #e0f2fe);
|
| 89 |
-
}
|
| 90 |
"""
|
| 91 |
) as demo:
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
gr.
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
| 5 |
+
# 生成 E 曲线
|
| 6 |
+
def entrenchment_curve(strength=1.2, shift=5):
|
| 7 |
x = np.linspace(0, 10, 200)
|
| 8 |
+
y = 1 / (1 + np.exp(-strength*(x-shift)))
|
| 9 |
+
return x, y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# 生成 C 曲线
|
| 12 |
+
def conventionalization_curve(strength=0.9, shift=4):
|
| 13 |
+
x = np.linspace(0, 10, 200)
|
| 14 |
+
y = 1 / (1 + np.exp(-strength*(x-shift)))
|
| 15 |
+
return x, y
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# 单表达分析
|
| 18 |
def analyze_ec(expression):
|
| 19 |
if not expression.strip():
|
| 20 |
return "请输入一个表达。", None, None, ""
|
| 21 |
|
| 22 |
entrenchment_text = (
|
| 23 |
f"### Entrenchment(固着 — 个体层面)\n"
|
| 24 |
+
f"表达 **「{expression}」** 在个体语言使用中反复出现,会导致:\n"
|
| 25 |
+
f"- 激活速度增强\n"
|
| 26 |
+
f"- 心理熟悉度提高\n"
|
| 27 |
+
f"- 逐渐成为最易脱口而出的默认结构\n"
|
| 28 |
)
|
| 29 |
|
| 30 |
conventionalization_text = (
|
| 31 |
f"### Conventionalization(常规化 — 社群层面)\n"
|
| 32 |
+
f"当社群中越来越多人使用 **「{expression}」** 时,它会:\n"
|
| 33 |
+
f"- 形成共享表达\n"
|
| 34 |
+
f"- 变成约定俗成的社群惯例\n"
|
| 35 |
+
f"- 在语言系统中获得更高稳定度\n"
|
| 36 |
)
|
| 37 |
|
| 38 |
ec_text = (
|
| 39 |
+
f"### 综合分析\n"
|
| 40 |
+
f"表达 **「{expression}」** 的固着(E)和常规化(C)相互强化,"
|
| 41 |
+
f"语言形式会进入典型的 **E → C → E 循环**,从而在语言系统中逐渐稳定下来。"
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# 画图
|
| 45 |
+
x1, y1 = entrenchment_curve()
|
| 46 |
+
fig_e = plt.figure(figsize=(4.5,3.2))
|
| 47 |
+
plt.plot(x1, y1, linewidth=2)
|
| 48 |
+
plt.title("Entrenchment Curve")
|
| 49 |
+
plt.xlabel("Usage Frequency")
|
| 50 |
+
plt.ylabel("Cognitive Strength")
|
| 51 |
+
plt.tight_layout()
|
| 52 |
+
|
| 53 |
+
x2, y2 = conventionalization_curve()
|
| 54 |
+
fig_c = plt.figure(figsize=(4.5,3.2))
|
| 55 |
+
plt.plot(x2, y2, color="#ff8c42", linewidth=2)
|
| 56 |
+
plt.title("Conventionalization Curve")
|
| 57 |
+
plt.xlabel("Time / Spread")
|
| 58 |
+
plt.ylabel("Community Adoption")
|
| 59 |
+
plt.tight_layout()
|
| 60 |
|
| 61 |
return entrenchment_text + "\n\n" + conventionalization_text, fig_e, fig_c, ec_text
|
| 62 |
|
| 63 |
|
| 64 |
+
# 🔥 双表达对比
|
| 65 |
+
def compare_two(expr1, expr2):
|
| 66 |
+
if not expr1.strip() or not expr2.strip():
|
| 67 |
+
return "请输入两个表达进行对比。", None, None, None, ""
|
| 68 |
+
|
| 69 |
+
# 模拟差异(这里只是演示,真实可以绑定语料库)
|
| 70 |
+
strength1 = 1.2 + np.random.uniform(-0.2, 0.2)
|
| 71 |
+
strength2 = 1.2 + np.random.uniform(-0.2, 0.2)
|
| 72 |
+
shift1 = 5 + np.random.uniform(-0.4, 0.4)
|
| 73 |
+
shift2 = 5 + np.random.uniform(-0.4, 0.4)
|
| 74 |
+
|
| 75 |
+
# E 曲线
|
| 76 |
+
xA, yA = entrenchment_curve(strength1, shift1)
|
| 77 |
+
xB, yB = entrenchment_curve(strength2, shift2)
|
| 78 |
+
|
| 79 |
+
fig_e = plt.figure(figsize=(5,3.5))
|
| 80 |
+
plt.plot(xA, yA, linewidth=2, label=f"{expr1}")
|
| 81 |
+
plt.plot(xB, yB, linewidth=2, label=f"{expr2}")
|
| 82 |
+
plt.legend()
|
| 83 |
+
plt.title("Entrenchment Comparison")
|
| 84 |
+
plt.xlabel("Usage Frequency")
|
| 85 |
+
plt.ylabel("Cognitive Strength")
|
| 86 |
+
plt.tight_layout()
|
| 87 |
+
|
| 88 |
+
# C 曲线
|
| 89 |
+
xC1, yC1 = conventionalization_curve(strength1, shift1)
|
| 90 |
+
xC2, yC2 = conventionalization_curve(strength2, shift2)
|
| 91 |
+
|
| 92 |
+
fig_c = plt.figure(figsize=(5,3.5))
|
| 93 |
+
plt.plot(xC1, yC1, linewidth=2, label=f"{expr1}")
|
| 94 |
+
plt.plot(xC2, yC2, linewidth=2, label=f"{expr2}")
|
| 95 |
+
plt.legend()
|
| 96 |
+
plt.title("Conventionalization Comparison")
|
| 97 |
+
plt.xlabel("Time / Spread")
|
| 98 |
+
plt.ylabel("Community Adoption")
|
| 99 |
+
plt.tight_layout()
|
| 100 |
+
|
| 101 |
+
# 热力图(模拟差异矩阵)
|
| 102 |
+
diff = np.abs(yA - yB)[:50].reshape(10, 5)
|
| 103 |
+
fig_h = plt.figure(figsize=(4,3))
|
| 104 |
+
plt.imshow(diff, cmap="Reds", aspect="auto")
|
| 105 |
+
plt.colorbar(label="Difference Intensity")
|
| 106 |
+
plt.title("E Difference Heatmap")
|
| 107 |
+
plt.tight_layout()
|
| 108 |
+
|
| 109 |
+
# 自动解释
|
| 110 |
+
explanation = f"""
|
| 111 |
+
### 🔍 双表达对比解释
|
| 112 |
+
|
| 113 |
+
**1. 固着(Entrenchment)对比**
|
| 114 |
+
- 若 **{expr1}** 曲线更陡峭 → 固着速度更快(更容易在大脑中自动化)。
|
| 115 |
+
- 若 **{expr2}** 曲线更平缓 → 固着程度较弱。
|
| 116 |
+
|
| 117 |
+
**2. 常规化(Conventionalization)对比**
|
| 118 |
+
- 若 **{expr1}** 的社区扩散曲线更早上升 → 更容易成为社群惯例。
|
| 119 |
+
- 若 **{expr2}** 上升滞后 → 社群采用速度较慢。
|
| 120 |
+
|
| 121 |
+
**3. 热力图解释**
|
| 122 |
+
- 红色越深 → 两个表达的固着差异越显著
|
| 123 |
+
- 若图像整体偏红 → 两表达的心理加工方式差异大
|
| 124 |
+
- 若接近浅色 → 两者在固着层面更相似
|
| 125 |
+
|
| 126 |
+
**4. 综合判断**
|
| 127 |
+
从整体曲线趋势与热力图可看出:
|
| 128 |
+
- 若 {expr1} 曲线始终高于 {expr2} → 它在 E 和 C 两方面均占优势,语言更容易稳定化
|
| 129 |
+
- 若两者交叉,多半说明两词属于“不同语用功能/语域”
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
return "", fig_e, fig_c, fig_h, explanation
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# ========== UI ==========
|
| 136 |
with gr.Blocks(
|
| 137 |
css="""
|
| 138 |
+
.card {background:white; padding:15px; border-radius:10px;
|
| 139 |
+
box-shadow:0 3px 10px rgba(0,0,0,0.08); margin-bottom:12px;}
|
| 140 |
+
body {background: linear-gradient(135deg,#eef2ff,#e0f2fe);}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"""
|
| 142 |
) as demo:
|
| 143 |
|
| 144 |
+
gr.HTML("<h1 style='text-align:center'>🧠 E&C 模型互动平台(升级版)</h1>")
|
| 145 |
+
|
| 146 |
+
with gr.Tab("单表达分析"):
|
| 147 |
+
with gr.Row():
|
| 148 |
+
with gr.Column(scale=1):
|
| 149 |
+
gr.HTML("<div class='card'>")
|
| 150 |
+
expr = gr.Textbox(label="输入一个表达")
|
| 151 |
+
btn1 = gr.Button("生成单表达分析")
|
| 152 |
+
gr.HTML("</div>")
|
| 153 |
+
with gr.Column(scale=2):
|
| 154 |
+
gr.HTML("<div class='card'>")
|
| 155 |
+
out_txt = gr.Markdown()
|
| 156 |
+
gr.HTML("</div>")
|
| 157 |
+
with gr.Row():
|
| 158 |
+
with gr.Column():
|
| 159 |
+
gr.HTML("<div class='card'>")
|
| 160 |
+
out_e = gr.Plot()
|
| 161 |
+
gr.HTML("</div>")
|
| 162 |
+
with gr.Column():
|
| 163 |
+
gr.HTML("<div class='card'>")
|
| 164 |
+
out_c = gr.Plot()
|
| 165 |
+
gr.HTML("</div>")
|
| 166 |
+
gr.HTML("<div class='card'>")
|
| 167 |
+
out_sum = gr.Markdown()
|
| 168 |
+
gr.HTML("</div>")
|
| 169 |
+
|
| 170 |
+
btn1.click(analyze_ec, inputs=[expr],
|
| 171 |
+
outputs=[out_txt, out_e, out_c, out_sum])
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
with gr.Tab("双表达对比分析"):
|
| 175 |
+
with gr.Row():
|
| 176 |
+
with gr.Column(scale=1):
|
| 177 |
+
gr.HTML("<div class='card'>")
|
| 178 |
+
expr1 = gr.Textbox(label="表达 A")
|
| 179 |
+
expr2 = gr.Textbox(label="表达 B")
|
| 180 |
+
btn2 = gr.Button("开始对比")
|
| 181 |
+
gr.HTML("</div>")
|
| 182 |
+
|
| 183 |
+
with gr.Column(scale=2):
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
gr.HTML("<div class='card'>")
|
| 187 |
+
cmp_e = gr.Plot()
|
| 188 |
+
gr.HTML("</div>")
|
| 189 |
+
|
| 190 |
+
with gr.Column():
|
| 191 |
+
gr.HTML("<div class='card'>")
|
| 192 |
+
cmp_c = gr.Plot()
|
| 193 |
+
gr.HTML("</div>")
|
| 194 |
+
|
| 195 |
+
gr.HTML("<div class='card'>")
|
| 196 |
+
heatmap = gr.Plot()
|
| 197 |
+
gr.HTML("</div>")
|
| 198 |
+
|
| 199 |
+
gr.HTML("<div class='card'>")
|
| 200 |
+
cmp_text = gr.Markdown()
|
| 201 |
+
gr.HTML("</div>")
|
| 202 |
+
|
| 203 |
+
btn2.click(compare_two, inputs=[expr1, expr2],
|
| 204 |
+
outputs=["", cmp_e, cmp_c, heatmap, cmp_text])
|
| 205 |
|
| 206 |
demo.launch()
|