helen573 commited on
Commit
24e9376
·
verified ·
1 Parent(s): 198dd89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -57
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
- # Replace single $ with double $$ if not already doubled
97
- response = response.replace('$ ', '$$').replace(' $', '$$')
98
-
99
- # Format log subscripts correctly
100
  response = response.replace('log_', '\\log_')
 
 
 
 
 
101
 
102
- # Ensure all numbers in math contexts are wrapped in $$
103
- response = response.replace(' $log', ' $$\\log')
104
-
105
- # Clean up line breaks
106
- response = '\n\n'.join(line.strip() for line in response.split('\n') if line.strip())
107
-
108
- in_math = False
109
- formatted_lines = []
110
- current_line = []
111
 
112
- for char in response:
113
- if char == '$' and (len(current_line) == 0 or current_line[-1] != '\\'):
114
- in_math = not in_math
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
- if current_line:
123
- formatted_lines.append(''.join(current_line))
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
- - 運算:$$+$$, $$-$$, $$\times$$, $$\div$$
136
- - 對數:$$\log_b(x)$$
137
  - 不要使用單個 $ 符號
138
- - LaTeX中的下標使_ ,例如:$$\log_b(x)$$ 而不是 $$log_b(x)$$
139
- - 所有數字都要用 $$...$$ 包覆,例如:$$1$$, $$2$$, $$3$$
140
 
141
- 2. 用 <br> 標記分隔不同段落。
 
 
 
 
142
 
143
- 請依照此格式提供
144
- 1. 英文翻譯:[列出數學相關的英文翻譯]<br>
145
- 2. 中文解釋:[連貫中文解釋]<br>
146
- 3. English Explanation:[連貫英文解釋]<br>
147
- 4. 數學使用場景:[數學使用場景說明]<br>
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
- - 運算:$$+$$, $$-$$, $$\times$$, $$\div$$
177
- - 對數:$$\log_b(x)$$
178
  - 不要使用單個 $ 符號
179
- - LaTeX中的下標使_ ,例如:$$\log_b(x)$$ 而不是 $$log_b(x)$$
180
- - 所有數字都要用 $$...$$ 包覆,例如:$$1$$, $$2$$, $$3$$
181
 
182
- 2. 用 <br> 標記分隔不同段落。
 
 
 
 
183
 
184
- 回應格式:
185
- 1. 英文例句:[寫出一個數學相關的英文例句]<br>
186
- 2. 中文翻譯:[該例句中文���譯]<br>
187
- 3. 句子解釋:[用中文解釋這個例句中的數學概念]<br>
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
- - 運算:$$+$$, $$-$$, $$\times$$, $$\div$$
217
- - 對數:$$\log_b(x)$$
218
  - 不要使用單個 $ 符號
219
- - LaTeX中的下標使_ ,例如:$$\log_b(x)$$ 而不是 $$log_b(x)$$
220
- - 所有數字都要用 $$...$$ 包覆,例如:$$1$$, $$2$$, $$3$$
 
 
 
 
 
221
 
222
- 2. 用 <br> 標記分隔不同段落。
223
- 3. 數學式必須與文字在一行。
224
- 4. 所有步驟的說明文字和數學式都要在同一行。"""
 
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