Update app.py
Browse files
app.py
CHANGED
|
@@ -2,171 +2,52 @@ import gradio as gr
|
|
| 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 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
# E 曲线
|
| 76 |
-
xA, yA = entrenchment_curve(strength1, shift1)
|
| 77 |
-
xB, yB = entrenchment_curve(strength2, shift2)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 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 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
**2. 常规化(Conventionalization)对比**
|
| 118 |
-
- 若 **{expr1}** 的社区扩散曲线更早上升 → 更容易成为社群惯例。
|
| 119 |
-
- 若 **{expr2}** 上升滞后 → 社群采���速度较慢。
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
- 若接近浅色 → 两者在固着层面更相似
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
"""
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ========== Mock 词频函数(保证 HuggingFace 100% 可运行) ==========
|
| 7 |
+
# 你之后可以换成真实语料版本
|
| 8 |
+
def get_freq(expr):
|
| 9 |
+
expr = expr.lower().replace(" ", "_")
|
| 10 |
+
return (abs(hash(expr)) % 5000) + 50 # 50–5000 之间的“伪词频”
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# ========== E 曲线:固着(Entrenchment) ==========
|
| 14 |
+
def entrenchment_real(freq_val):
|
| 15 |
+
x = np.linspace(0, 10, 200)
|
| 16 |
+
# 高频 → 更陡
|
| 17 |
+
strength = min(2.0, 0.5 + np.log(freq_val + 1) / 4)
|
| 18 |
+
shift = 5
|
| 19 |
+
y = 1 / (1 + np.exp(-strength * (x - shift)))
|
| 20 |
+
return x, y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ========== C 曲线:常规化(Conventionalization) ==========
|
| 24 |
+
def conventionalization_real(freq_val):
|
| 25 |
+
x = np.linspace(0, 10, 200)
|
| 26 |
+
# 高频 → 扩散更快
|
| 27 |
+
spread = min(2.0, 0.4 + np.log(freq_val + 1) / 5)
|
| 28 |
+
shift = 4
|
| 29 |
+
y = 1 / (1 + np.exp(-spread * (x - shift)))
|
| 30 |
+
return x, y
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# ===================== 单表达分析 =====================
|
| 34 |
+
def analyze_ec(expr):
|
| 35 |
+
f = get_freq(expr)
|
|
|
|
| 36 |
|
| 37 |
+
entrenchment_text = f"""
|
| 38 |
+
### Entrenchment(固着)
|
| 39 |
+
表达 **「{expr}」** 在模拟语料中的出现频率:**{f} 次**
|
| 40 |
+
词频越高 → 个体越容易固着 → 曲线越陡。
|
| 41 |
"""
|
| 42 |
|
| 43 |
+
conventionalization_text = f"""
|
| 44 |
+
### Conventionalization(常规化)
|
| 45 |
+
出现频率越高 → 在社群中越容易被接受 → 扩散速度越快。
|
| 46 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
summary = f"""
|
| 49 |
+
### 综合解释
|
| 50 |
+
真实使用频率影响固着(E)与常规化(C):
|
| 51 |
+
- 高频表达 → 更易自动化 / 固着
|
| 52 |
+
- 高频表达 → 社群采用速度更快
|
| 53 |
+
这
|