WENior commited on
Commit
b80369c
·
verified ·
1 Parent(s): 454a628

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -37
app.py CHANGED
@@ -2,88 +2,103 @@ import gradio as gr
2
  import re
3
 
4
  # ---------------------------
5
- # 简易规则版 TIRA 分类器(可升级)
6
  # ---------------------------
7
  def classify_tira(cn_text):
8
  text = cn_text.strip()
9
 
10
- # Retain: 常用礼貌问候、称呼
11
- retain_patterns = ["您好", "敬请", "麻烦您", "请查收"]
12
  if any(p in text for p in retain_patterns):
13
- return "Retain(保留)", "礼貌用语多为直译可保持原意,PDI 变化较小。"
14
 
15
- # Amplify: 请求+模糊词,如“是否方便”“能否请您”
16
- amplify_patterns = ["能否", "是否方便", "请您帮忙", "抽空"]
17
  if any(p in text for p in amplify_patterns):
18
- return "Amplify(放大)", "英文可能出现更显性礼貌(please / possibly),使 PDI 上移。"
19
 
20
- # Attenuate: 直接请求、没有客套
21
- attenuate_patterns = ["请尽快", "需要您", "必须", "尽快处理"]
22
  if any(p in text for p in attenuate_patterns):
23
- return "Attenuate(削弱)", "英文可能变得更直接,在目标语减弱礼貌,PDI 下移。"
24
 
25
- # Redirect: 情感性、文化特定表达
26
- redirect_patterns = ["给您添麻烦", "不好意思打扰", "不胜感激"]
27
  if any(p in text for p in redirect_patterns):
28
- return "Redirect(转向)", "中文特有的情感敬辞,英文常转为名词化/被动式,方向不定。"
 
 
 
29
 
30
- return "Retain(保留)", "未检测到明显礼貌偏移特征,默认保持。"
31
 
32
  # ---------------------------
33
- # 建议英文生成(模板版)
34
  # ---------------------------
35
  def suggest_english(cn_text, tira_type):
36
  if tira_type.startswith("Amplify"):
37
- return "Could you please take a moment to look at the attached file?"
38
  if tira_type.startswith("Attenuate"):
39
- return "Please review the attachment when you have time."
40
  if tira_type.startswith("Redirect"):
41
  return "I apologize if this may cause any inconvenience. Please find the attachment below."
42
  return "Dear Professor, could you please take a look at the attached document?"
43
 
 
44
  # ---------------------------
45
- # 主函数
46
  # ---------------------------
47
  def predict_tira(cn_text):
48
  tira_type, analysis = classify_tira(cn_text)
49
  en_suggestion = suggest_english(cn_text, tira_type)
50
 
51
- risk = {
52
- "Retain": "低风险(PDI 基本保持)",
53
- "Amplify": "中风险(可能让关系显得更疏远)",
54
- "Attenuate": "中偏高风险(可能显得更直接)",
55
- "Redirect": "不稳定(可能造成理解偏差)"
56
  }
57
-
58
  key = tira_type.split("(")[0]
59
- pdi_risk = risk.get(key, "低风险")
60
 
61
  return tira_type, analysis, pdi_risk, en_suggestion
62
 
63
 
64
  # ---------------------------
65
- # Gradio UI
66
  # ---------------------------
67
- with gr.Blocks(title="TIRA 预测工具") as demo:
 
 
 
 
 
 
68
  gr.Markdown("""
69
- # **TIRA 学术邮件翻译预测工具**
70
- ### Translation-Induced Re-Alignment (保留 / 放大 / 削弱 / 转向)
71
- 输入中文学术邮件 → 获得 TIRA 类型预测 + 翻译风险 + 建议英文表达。
 
 
72
  """)
73
 
74
- cn_input = gr.Textbox(label="✉ 输入中文学术邮件内容", lines=5, placeholder="例如:能否请您帮我看一下附件?")
75
-
76
- btn = gr.Button("🔍 预测 TIRA 类型")
 
 
 
 
 
77
 
78
  tira_output = gr.Textbox(label="📌 TIRA 类型预测")
79
- analysis_output = gr.Textbox(label="📘 解释")
80
- risk_output = gr.Textbox(label="⚠ PDI 风险")
81
- en_suggestion_output = gr.Textbox(label="✏ 建议英文译文")
82
 
83
  btn.click(
84
  fn=predict_tira,
85
  inputs=cn_input,
86
- outputs=[tira_output, analysis_output, risk_output, en_suggestion_output]
87
  )
88
 
89
  demo.launch()
 
2
  import re
3
 
4
  # ---------------------------
5
+ # TIRA 规则分类器
6
  # ---------------------------
7
  def classify_tira(cn_text):
8
  text = cn_text.strip()
9
 
10
+ # Retain 保留
11
+ retain_patterns = ["您好", "敬请", "麻烦您", "请查收", "拜托您"]
12
  if any(p in text for p in retain_patterns):
13
+ return "Retain(保留)", "常规礼貌标记沿用 → 英译时多保持原本关系强度。"
14
 
15
+ # Amplify 放大
16
+ amplify_patterns = ["能否", "是否方便", "请您帮忙", "抽空", "劳驾", "麻烦帮我"]
17
  if any(p in text for p in amplify_patterns):
18
+ return "Amplify(放大)", "英文可能加入 please / possibly → 增加礼貌显性度 → PDI 上移。"
19
 
20
+ # Attenuate 削弱
21
+ attenuate_patterns = ["请尽快", "需要您", "必须", "麻烦尽快", "尽快处理"]
22
  if any(p in text for p in attenuate_patterns):
23
+ return "Attenuate(削弱)", "英文可能变为更直接表达 → 礼貌减弱 → PDI 下移。"
24
 
25
+ # Redirect 转向
26
+ redirect_patterns = ["给您添麻烦", "不好意思打扰", "不胜感激", "心里很过意不去"]
27
  if any(p in text for p in redirect_patterns):
28
+ return "Redirect(转向)", "中文特有情感敬辞 → 英文常被被动化/名词化 → 方向不定。"
29
+
30
+ # 默认 → 按保留处理
31
+ return "Retain(保留)", "未出现特殊礼貌偏移 → 默认为保留型。"
32
 
 
33
 
34
  # ---------------------------
35
+ # 建议英文(模板版,可替换成 GPT 输出)
36
  # ---------------------------
37
  def suggest_english(cn_text, tira_type):
38
  if tira_type.startswith("Amplify"):
39
+ return "Could you please take a moment to review the attached file?"
40
  if tira_type.startswith("Attenuate"):
41
+ return "Please review the attachment when possible."
42
  if tira_type.startswith("Redirect"):
43
  return "I apologize if this may cause any inconvenience. Please find the attachment below."
44
  return "Dear Professor, could you please take a look at the attached document?"
45
 
46
+
47
  # ---------------------------
48
+ # 主预测函数
49
  # ---------------------------
50
  def predict_tira(cn_text):
51
  tira_type, analysis = classify_tira(cn_text)
52
  en_suggestion = suggest_english(cn_text, tira_type)
53
 
54
+ risk_map = {
55
+ "Retain": "低风险(关系保持稳定)",
56
+ "Amplify": "中风险(英文更显远礼貌)",
57
+ "Attenuate": "中偏高(可能显得更直接)",
58
+ "Redirect": "不稳定(误解风险较高)"
59
  }
 
60
  key = tira_type.split("(")[0]
61
+ pdi_risk = risk_map.get(key, "低风险")
62
 
63
  return tira_type, analysis, pdi_risk, en_suggestion
64
 
65
 
66
  # ---------------------------
67
+ # 深蓝学术风 UI
68
  # ---------------------------
69
+ css = """
70
+ h1 {text-align: center; color: #1B3B6F;}
71
+ .gradio-container {background-color: #F7F9FC;}
72
+ label {font-weight: bold;}
73
+ """
74
+
75
+ with gr.Blocks(css=css, title="TIRA 学术邮件翻译分析工具") as demo:
76
  gr.Markdown("""
77
+ # **📘 TIRA Academic Email Translation Tool**
78
+ ### *Translation-Induced Re-Alignment (Retain / Amplify / Attenuate / Redirect)*
79
+ ---
80
+
81
+ 输入一段中文��术邮件 → 系统将预测 TIRA 类型、给出解释、评估 PDI 风险,并提供建议英文表达。
82
  """)
83
 
84
+ with gr.Row():
85
+ cn_input = gr.Textbox(
86
+ label=" 输入中文邮件内容",
87
+ lines=6,
88
+ placeholder="例如:能否请您帮我看一下附件?"
89
+ )
90
+
91
+ btn = gr.Button("🔍 开始预测", variant="primary")
92
 
93
  tira_output = gr.Textbox(label="📌 TIRA 类型预测")
94
+ analysis_output = gr.Textbox(label="📘 解释说明")
95
+ risk_output = gr.Textbox(label="⚠ PDI 风险评估")
96
+ en_output = gr.Textbox(label="✏ 建议英文表达")
97
 
98
  btn.click(
99
  fn=predict_tira,
100
  inputs=cn_input,
101
+ outputs=[tira_output, analysis_output, risk_output, en_output]
102
  )
103
 
104
  demo.launch()