Update app.py
Browse files
app.py
CHANGED
|
@@ -41,28 +41,77 @@ def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
|
| 41 |
|
| 42 |
def translate_to_english(name, word):
|
| 43 |
"""Generate math-related English translation with bilingual explanation"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# Log to Notion
|
| 50 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 51 |
return response
|
| 52 |
|
| 53 |
def generate_example(name, word):
|
| 54 |
"""Generate math-related example sentence for Chinese word"""
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Log to Notion
|
| 60 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 61 |
return response
|
| 62 |
|
| 63 |
def respond(name, message, history):
|
| 64 |
"""Process chatbot response and update history"""
|
| 65 |
-
response = f"You asked: {message}. Here's a sample response with math: \( a^2 + b^2 = c^2 \)."
|
| 66 |
history.append((message, response))
|
| 67 |
return "", history
|
| 68 |
|
|
@@ -116,8 +165,4 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 116 |
show_label=False,
|
| 117 |
elem_classes=["input-box"]
|
| 118 |
)
|
| 119 |
-
clear = gr.ClearButton([msg, chatbot], value="
|
| 120 |
-
msg.submit(respond, [name_input, msg, chatbot], [msg, chatbot])
|
| 121 |
-
|
| 122 |
-
if __name__ == "__main__":
|
| 123 |
-
demo.launch()
|
|
|
|
| 41 |
|
| 42 |
def translate_to_english(name, word):
|
| 43 |
"""Generate math-related English translation with bilingual explanation"""
|
| 44 |
+
translations = {
|
| 45 |
+
"向量": {
|
| 46 |
+
"english": "Vector",
|
| 47 |
+
"chinese_explanation": "向量是一種具有大小與方向的數學實體。",
|
| 48 |
+
"english_explanation": "A vector is a mathematical entity characterized by magnitude and direction.",
|
| 49 |
+
"math_context": "向量廣泛用於物理學和工程學中的力和位移表示。",
|
| 50 |
+
"english_context": "Vectors are widely used in physics and engineering to represent forces and displacements."
|
| 51 |
+
},
|
| 52 |
+
"函數": {
|
| 53 |
+
"english": "Function",
|
| 54 |
+
"chinese_explanation": "函數描述變量之間的依賴關係。",
|
| 55 |
+
"english_explanation": "A function describes the dependency relationship between variables.",
|
| 56 |
+
"math_context": "函數是數學分析中的核心概念,常用於表示輸入與輸出之間的關係。",
|
| 57 |
+
"english_context": "Functions are a core concept in mathematical analysis, often used to represent input-output relationships."
|
| 58 |
+
},
|
| 59 |
+
"極限": {
|
| 60 |
+
"english": "Limit",
|
| 61 |
+
"chinese_explanation": "極限用於描述函數趨於某一特定值的行為。",
|
| 62 |
+
"english_explanation": "The limit describes the behavior of a function as it approaches a specific value.",
|
| 63 |
+
"math_context": "極限是微積分中的基礎概念,用於定義導數和積分。",
|
| 64 |
+
"english_context": "Limits are a foundational concept in calculus, used to define derivatives and integrals."
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
if word in translations:
|
| 68 |
+
t = translations[word]
|
| 69 |
+
response = f"""1. 英文翻譯:{t['english']}
|
| 70 |
+
2. 中文解釋:{t['chinese_explanation']}
|
| 71 |
+
3. English Explanation: {t['english_explanation']}
|
| 72 |
+
4. 數學使用場景:{t['math_context']}
|
| 73 |
+
5. Mathematical Context: {t['english_context']}"""
|
| 74 |
+
else:
|
| 75 |
+
response = f"抱歉,未找到 '{word}' 的對應翻譯與解釋。請嘗試其他數學詞彙!"
|
| 76 |
+
|
| 77 |
# Log to Notion
|
| 78 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 79 |
return response
|
| 80 |
|
| 81 |
def generate_example(name, word):
|
| 82 |
"""Generate math-related example sentence for Chinese word"""
|
| 83 |
+
examples = {
|
| 84 |
+
"向量": {
|
| 85 |
+
"example": "The vector \\( \\vec{v} = (3, 4) \\) represents a point in 2D space.",
|
| 86 |
+
"translation": "向量 \\( \\vec{v} = (3, 4) \\) 表示二維空間中的一個點。",
|
| 87 |
+
"explanation": "此例句中的向量表示從原點到座標 (3, 4) 的方向和距離。"
|
| 88 |
+
},
|
| 89 |
+
"函數": {
|
| 90 |
+
"example": "The function \\( f(x) = x^2 \\) squares the input value.",
|
| 91 |
+
"translation": "函數 \\( f(x) = x^2 \\) 將輸入值平方。",
|
| 92 |
+
"explanation": "此例句中的函數描述了輸入值與輸出值之間的關係。"
|
| 93 |
+
},
|
| 94 |
+
"極限": {
|
| 95 |
+
"example": "The limit of \\( f(x) = 1/x \\) as x approaches infinity is 0.",
|
| 96 |
+
"translation": "當 x 趨於無窮大時,\\( f(x) = 1/x \\) 的極限為 0。",
|
| 97 |
+
"explanation": "此例句中的極限描述了函數在某一趨勢下的行為。"
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
if word in examples:
|
| 101 |
+
e = examples[word]
|
| 102 |
+
response = f"""1. 英文例句:{e['example']}
|
| 103 |
+
2. 中文翻譯:{e['translation']}
|
| 104 |
+
3. 句子解釋:{e['explanation']}"""
|
| 105 |
+
else:
|
| 106 |
+
response = f"抱歉,未找到 '{word}' 的例句。請嘗試其他數學詞彙!"
|
| 107 |
+
|
| 108 |
# Log to Notion
|
| 109 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 110 |
return response
|
| 111 |
|
| 112 |
def respond(name, message, history):
|
| 113 |
"""Process chatbot response and update history"""
|
| 114 |
+
response = f"You asked: {message}. Here's a sample response with math: \\( a^2 + b^2 = c^2 \\)."
|
| 115 |
history.append((message, response))
|
| 116 |
return "", history
|
| 117 |
|
|
|
|
| 165 |
show_label=False,
|
| 166 |
elem_classes=["input-box"]
|
| 167 |
)
|
| 168 |
+
clear = gr.ClearButton([msg, chatbot], value="
|
|
|
|
|
|
|
|
|
|
|
|