Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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()
|