Update app.py
Browse files
app.py
CHANGED
|
@@ -2,88 +2,103 @@ import gradio as gr
|
|
| 2 |
import re
|
| 3 |
|
| 4 |
# ---------------------------
|
| 5 |
-
#
|
| 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(放大)", "
|
| 19 |
|
| 20 |
-
# Attenuate
|
| 21 |
-
attenuate_patterns = ["请尽快", "需要您", "必须", "尽快处理"]
|
| 22 |
if any(p in text for p in attenuate_patterns):
|
| 23 |
-
return "Attenuate(削弱)", "
|
| 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
|
| 38 |
if tira_type.startswith("Attenuate"):
|
| 39 |
-
return "Please review the attachment when
|
| 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 |
-
|
| 52 |
-
"Retain": "
|
| 53 |
-
"Amplify": "
|
| 54 |
-
"Attenuate": "
|
| 55 |
-
"Redirect": "
|
| 56 |
}
|
| 57 |
-
|
| 58 |
key = tira_type.split("(")[0]
|
| 59 |
-
pdi_risk =
|
| 60 |
|
| 61 |
return tira_type, analysis, pdi_risk, en_suggestion
|
| 62 |
|
| 63 |
|
| 64 |
# ---------------------------
|
| 65 |
-
#
|
| 66 |
# ---------------------------
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
gr.Markdown("""
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
""")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
tira_output = gr.Textbox(label="📌 TIRA 类型预测")
|
| 79 |
-
analysis_output = gr.Textbox(label="📘
|
| 80 |
-
risk_output = gr.Textbox(label="⚠ PDI
|
| 81 |
-
|
| 82 |
|
| 83 |
btn.click(
|
| 84 |
fn=predict_tira,
|
| 85 |
inputs=cn_input,
|
| 86 |
-
outputs=[tira_output, analysis_output, risk_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()
|