Update app.py
Browse files
app.py
CHANGED
|
@@ -93,36 +93,34 @@ def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
|
| 93 |
print(f"Error logging to Notion: {e}")
|
| 94 |
|
| 95 |
def format_math_response(response):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
response = response.replace('log_', '\\log_')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
current_line.append(char)
|
| 116 |
-
elif char == '\n' and not in_math:
|
| 117 |
-
formatted_lines.append(''.join(current_line))
|
| 118 |
-
current_line = []
|
| 119 |
-
else:
|
| 120 |
-
current_line.append(char)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
return '<br>'.join(formatted_lines)
|
| 126 |
|
| 127 |
def translate_to_english(name, word):
|
| 128 |
messages = [
|
|
@@ -130,22 +128,26 @@ def translate_to_english(name, word):
|
|
| 130 |
"role": "system",
|
| 131 |
"content": """你是一個數學翻譯專家。請用以下規則提供翻譯:
|
| 132 |
|
| 133 |
-
1.
|
|
|
|
|
|
|
| 134 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 135 |
-
-
|
| 136 |
-
- 對數:$$\log_b(x)$$
|
| 137 |
- 不要使用單個 $ 符號
|
| 138 |
-
-
|
| 139 |
-
- 所有數字都要用 $$...$$ 包覆,例如:$$1$$, $$2$$, $$3$$
|
| 140 |
|
| 141 |
-
2.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
1. 英文翻譯:[
|
| 145 |
-
2. 中文解釋:[
|
| 146 |
-
3. English Explanation:[
|
| 147 |
-
4. 數學使用場景:[數學
|
| 148 |
-
5. Mathematical Context:[
|
| 149 |
},
|
| 150 |
{
|
| 151 |
"role": "user",
|
|
@@ -171,21 +173,25 @@ def generate_example(name, word):
|
|
| 171 |
"role": "system",
|
| 172 |
"content": """你是一個數學教師。請用以下規則提供例句:
|
| 173 |
|
| 174 |
-
1.
|
|
|
|
|
|
|
| 175 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 176 |
-
-
|
| 177 |
-
- 對數:$$\log_b(x)$$
|
| 178 |
- 不要使用單個 $ 符號
|
| 179 |
-
-
|
| 180 |
-
- 所有數字都要用 $$...$$ 包覆,例如:$$1$$, $$2$$, $$3$$
|
| 181 |
|
| 182 |
-
2.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
回應格式:
|
| 185 |
-
1. 英文例句:[
|
| 186 |
-
2. 中文翻譯:[
|
| 187 |
-
3. 句子解釋:[
|
| 188 |
-
4. Sentence Explanation:[
|
| 189 |
},
|
| 190 |
{
|
| 191 |
"role": "user",
|
|
@@ -211,17 +217,24 @@ def chat_response(name, message, chat_history):
|
|
| 211 |
"role": "system",
|
| 212 |
"content": """你是一個高中數學老師,使用英文回應。請確保:
|
| 213 |
|
| 214 |
-
1.
|
|
|
|
|
|
|
| 215 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 216 |
-
-
|
| 217 |
-
- 對數:$$\log_b(x)$$
|
| 218 |
- 不要使用單個 $ 符號
|
| 219 |
-
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
| 225 |
}
|
| 226 |
]
|
| 227 |
|
|
|
|
| 93 |
print(f"Error logging to Notion: {e}")
|
| 94 |
|
| 95 |
def format_math_response(response):
|
| 96 |
+
"""Format response to ensure proper LaTeX formatting"""
|
| 97 |
+
# Replace incorrect LaTeX formatting with correct one
|
| 98 |
+
response = response.replace(r'$\log_b(x)', r'$$\log_b(x)$$')
|
| 99 |
+
response = response.replace(r'$\log_', r'$$\log_')
|
| 100 |
response = response.replace('log_', '\\log_')
|
| 101 |
+
response = response.replace('$x', '$$x$$')
|
| 102 |
+
response = response.replace('$b$', '$$b$$')
|
| 103 |
+
response = response.replace('$1', '$$1$$')
|
| 104 |
+
response = response.replace('$(', '$$(')
|
| 105 |
+
response = response.replace(')$', ')$$')
|
| 106 |
|
| 107 |
+
# Clean up any remaining single dollar signs
|
| 108 |
+
parts = response.split('$')
|
| 109 |
+
formatted_parts = []
|
| 110 |
+
for i, part in enumerate(parts):
|
| 111 |
+
if i % 2 == 1: # Inside math expression
|
| 112 |
+
formatted_parts.append(f'$${part}$$')
|
| 113 |
+
else: # Outside math expression
|
| 114 |
+
formatted_parts.append(part)
|
| 115 |
+
response = ''.join(formatted_parts)
|
| 116 |
|
| 117 |
+
# Fix common spacing issues
|
| 118 |
+
response = response.replace('$$$$', '$$')
|
| 119 |
+
response = response.replace(' ', ' ')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
# Format line breaks
|
| 122 |
+
lines = [line.strip() for line in response.split('\n') if line.strip()]
|
| 123 |
+
return '<br>'.join(lines)
|
|
|
|
| 124 |
|
| 125 |
def translate_to_english(name, word):
|
| 126 |
messages = [
|
|
|
|
| 128 |
"role": "system",
|
| 129 |
"content": """你是一個數學翻譯專家。請用以下規則提供翻譯:
|
| 130 |
|
| 131 |
+
1. 數學式格式規範:
|
| 132 |
+
- 所有數學式都必須使用 $$...$$
|
| 133 |
+
- 對數表示:$$\\log_b(x)$$
|
| 134 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 135 |
+
- 數字:$$1$$, $$2$$, $$3$$
|
|
|
|
| 136 |
- 不要使用單個 $ 符號
|
| 137 |
+
- 所有數學符號都要用 LaTeX 格式
|
|
|
|
| 138 |
|
| 139 |
+
2. 對數的特別注意事項:
|
| 140 |
+
- 正確格式:$$\\log_b(x)$$
|
| 141 |
+
- 底數是 b 時:$$\\log_b$$
|
| 142 |
+
- 底數是 2 時:$$\\log_2$$
|
| 143 |
+
- 底數是 e 時:$$\\ln$$
|
| 144 |
|
| 145 |
+
3. 回應格式(每項使用 <br> 分隔):
|
| 146 |
+
1. 英文翻譯:[包含完整的數學符號]<br>
|
| 147 |
+
2. 中文解釋:[包含完整的數學符號]<br>
|
| 148 |
+
3. English Explanation:[包含完整的數學符號]<br>
|
| 149 |
+
4. 數學使用場景:[包含完整的數學符號]<br>
|
| 150 |
+
5. Mathematical Context:[包含完整的數學符號]<br>"""
|
| 151 |
},
|
| 152 |
{
|
| 153 |
"role": "user",
|
|
|
|
| 173 |
"role": "system",
|
| 174 |
"content": """你是一個數學教師。請用以下規則提供例句:
|
| 175 |
|
| 176 |
+
1. 數學式格式規範:
|
| 177 |
+
- 所有數學式都必須使用 $$...$$
|
| 178 |
+
- 對數表示:$$\\log_b(x)$$
|
| 179 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 180 |
+
- 數字:$$1$$, $$2$$, $$3$$
|
|
|
|
| 181 |
- 不要使用單個 $ 符號
|
| 182 |
+
- 所有數學符號都要用 LaTeX 格式
|
|
|
|
| 183 |
|
| 184 |
+
2. 對數的特別注意事項:
|
| 185 |
+
- 正確格式:$$\\log_b(x)$$
|
| 186 |
+
- 底數是 b 時:$$\\log_b$$
|
| 187 |
+
- 底數是 2 時:$$\\log_2$$
|
| 188 |
+
- 底數是 e 時:$$\\ln$$
|
| 189 |
|
| 190 |
+
3. 回應格式(每項使用 <br> 分隔):
|
| 191 |
+
1. 英文例句:[包含完整的數學符號]<br>
|
| 192 |
+
2. 中文翻譯:[包含完整的數學符號]<br>
|
| 193 |
+
3. 句子解釋:[包含完整的數學符號]<br>
|
| 194 |
+
4. Sentence Explanation:[包含完整的數學符號]<br>"""
|
| 195 |
},
|
| 196 |
{
|
| 197 |
"role": "user",
|
|
|
|
| 217 |
"role": "system",
|
| 218 |
"content": """你是一個高中數學老師,使用英文回應。請確保:
|
| 219 |
|
| 220 |
+
1. 數學式格式規範:
|
| 221 |
+
- 所有數學式都必須使用 $$...$$
|
| 222 |
+
- 對數表示:$$\\log_b(x)$$
|
| 223 |
- 變數:$$x$$, $$y$$, $$a$$, $$b$$
|
| 224 |
+
- 數字:$$1$$, $$2$$, $$3$$
|
|
|
|
| 225 |
- 不要使用單個 $ 符號
|
| 226 |
+
- 所有數學符號都要用 LaTeX 格式
|
| 227 |
+
|
| 228 |
+
2. 對數的特別注意事項:
|
| 229 |
+
- 正確格式:$$\\log_b(x)$$
|
| 230 |
+
- 底數是 b 時:$$\\log_b$$
|
| 231 |
+
- 底數是 2 時:$$\\log_2$$
|
| 232 |
+
- 底數是 e 時:$$\\ln$$
|
| 233 |
|
| 234 |
+
3. 格式要求:
|
| 235 |
+
- 用 <br> 分隔不同段落
|
| 236 |
+
- 數學式必須與文字在同一行
|
| 237 |
+
- 每個步驟的說明文字和數學式都要在同一行"""
|
| 238 |
}
|
| 239 |
]
|
| 240 |
|