WENior commited on
Commit
945c653
·
verified ·
1 Parent(s): db3bed5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+ # ---- 生成固着(Entrenchment)曲线 ----
6
+ def generate_entrenchment_curve():
7
+ x = np.linspace(0, 10, 200)
8
+ y = 1 / (1 + np.exp(-1.2*(x-5)))
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
+ fig, ax = plt.subplots(figsize=(4.5, 3.2))
24
+ ax.plot(x, y, linewidth=2, color="#ff8c42")
25
+ ax.set_title("Conventionalization Curve (Community Level)")
26
+ ax.set_xlabel("Time / Spread")
27
+ ax.set_ylabel("Community Adoption")
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}」** 在个体语言使用中通过反复出现,会在大脑中逐渐固着:\n"
39
+ f"- 激活更快\n"
40
+ f"- 使用更顺\n"
41
+ f"- 成为默认表达选择\n"
42
+ )
43
+
44
+ conventionalization_text = (
45
+ f"### Conventionalization(常规化 — 社群层面)\n"
46
+ f"当越来越多的人使用 **「{expression}」** 时,它会:\n"
47
+ f"- 形成稳定的社群惯例\n"
48
+ f"- 被理解、共享、模仿\n"
49
+ f"- 成为一种“约定俗成”的表达\n"
50
+ )
51
+
52
+ ec_text = (
53
+ f"### E&C 综合分析\n"
54
+ f"根据 Schmid 的模型,表达 **「{expression}」** 的固着(E)与常规化(C)会相互强化,"
55
+ f"产生典型的 **E → C → E 循环**,最终促使语言形式在系统中稳定下来。"
56
+ )
57
+
58
+ fig_e = generate_entrenchment_curve()
59
+ fig_c = generate_conventionalization_curve()
60
+
61
+ return entrenchment_text + "\n\n" + conventionalization_text, fig_e, fig_c, ec_text
62
+
63
+
64
+
65
+ # ---- 前端 UI(升级美化版) ----
66
+ with gr.Blocks(
67
+ theme=gr.themes.Soft(
68
+ primary_hue="indigo",
69
+ secondary_hue="blue",
70
+ neutral_hue="gray",
71
+ radius_size="md",
72
+ text_size="lg",
73
+ ),
74
+ css="""
75
+ .gradio-container {background: linear-gradient(135deg, #eef2ff, #e0f2fe);}
76
+ .card {
77
+ background: rgba(255, 255, 255, 0.73);
78
+ padding: 18px;
79
+ border-radius: 12px;
80
+ box-shadow: 0 4px 12px rgba(0,0,0,0.05);
81
+ margin-bottom: 12px;
82
+ }
83
+ """
84
+ ) as demo:
85
+
86
+ gr.Markdown(
87
+ """
88
+ <div style='text-align:center; margin-bottom: 20px;'>
89
+ <h1 style='color:#334155;'>🧠 Entrenchment & Conventionalization Model</h1>
90
+ <h3 style='color:#475569;'>语言固着(E) × 社群常规化(C)的动态可视化平台</h3>
91
+ </div>
92
+ """
93
+ )
94
+
95
+ with gr.Row():
96
+ with gr.Column(scale=1):
97
+ with gr.Box(elem_classes="card"):
98
+ expression = gr.Textbox(
99
+ label="输入一个表达",
100
+ placeholder="如:kind of / 内卷 / OMG / 摆烂",
101
+ )
102
+ btn = gr.Button("生成 E&C 分析", variant="primary")
103
+
104
+ with gr.Column(scale=2):
105
+ with gr.Box(elem_classes="card"):
106
+ output_text = gr.Markdown("等待输入表达…")
107
+
108
+ with gr.Row():
109
+ with gr.Column():
110
+ with gr.Box(elem_classes="card"):
111
+ fig1 = gr.Plot(label="Entrenchment Curve")
112
+
113
+ with gr.Column():
114
+ with gr.Box(elem_classes="card"):
115
+ fig2 = gr.Plot(label="Conventionalization Curve")
116
+
117
+ with gr.Box(elem_classes="card"):
118
+ ec_summary = gr.Markdown("")
119
+
120
+ btn.click(analyze_ec, inputs=[expression],
121
+ outputs=[output_text, fig1, fig2, ec_summary])
122
+
123
+ demo.launch()