Jintonic92 commited on
Commit
0ae7414
ยท
verified ยท
1 Parent(s): 565499c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -79
app.py CHANGED
@@ -5,6 +5,8 @@ from src.SecondModule.module2 import SimilarQuestionGenerator
5
  from src.ThirdModule.module3 import AnswerVerifier
6
  import logging
7
  from typing import Optional, Tuple
 
 
8
  logging.basicConfig(level=logging.DEBUG)
9
 
10
 
@@ -183,90 +185,18 @@ def handle_answer(answer, current_q):
183
  if st.session_state.current_question_index >= 10:
184
  st.session_state.current_step = 'review'
185
 
186
- def format_question(question: str) -> str:
187
- """๋ฌธ์ œ ํ…์ŠคํŠธ๋ฅผ LaTeX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜"""
188
- # ์ƒ‰์ƒ์ด ์žˆ๋Š” ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ
189
- colored_text_pattern = r'\\textcolor{([^}]+)}{([^}]+)}'
190
-
191
- def process_colored_text(match):
192
- color = match.group(1)
193
- text = match.group(2)
194
- return f'\\textcolor{{{color}}}{{{format_latex_expression(text)}}}'
195
-
196
- question = re.sub(colored_text_pattern, process_colored_text, question)
197
- return format_latex_expression(question)
198
-
199
- def format_answer_choice(choice: str) -> str:
200
- """์„ ํƒ์ง€ ํ…์ŠคํŠธ๋ฅผ LaTeX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜"""
201
- # ๋‹ฌ๋Ÿฌ ๊ธฐํ˜ธ๊ฐ€ ํฌํ•จ๋œ ์„ ํƒ์ง€ ํŠน๋ณ„ ์ฒ˜๋ฆฌ
202
- if '$' in choice:
203
- # ๋‹ฌ๋Ÿฌ ๊ธฐํ˜ธ๋ฅผ LaTeX ๋ช…๋ น์–ด๋กœ ๋ณ€ํ™˜
204
- choice = choice.replace('$', '\\$')
205
- return format_latex_expression(choice)
206
 
207
  def display_math_content(content: str):
208
  """์ˆ˜ํ•™ ๋‚ด์šฉ์„ ํ™”๋ฉด์— ํ‘œ์‹œ"""
209
- formatted_content = format_latex_expression(content)
210
- # LaTeX ๋ Œ๋”๋ง์„ ์œ„ํ•œ ์ถ”๊ฐ€ ์„ค์ •
211
  st.markdown(formatted_content, unsafe_allow_html=True)
212
 
213
- def format_latex_expression(text: str) -> str:
214
- """๋ณต์žกํ•œ LaTeX ์ˆ˜์‹๊ณผ ํŠน์ˆ˜ ๊ธฐํ˜ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜"""
215
- import re
216
-
217
- # LaTeX ํŠน์ˆ˜ ๋ช…๋ น์–ด ๋งคํ•‘
218
- latex_commands = {
219
- r'\left': r'\\left',
220
- r'\right': r'\\right',
221
- r'\bigcirc': r'\\bigcirc',
222
- r'\square': r'\\square',
223
- r'\quad': r'\\quad'
224
- }
225
-
226
- # 1. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” LaTeX ์ˆ˜์‹ ๋ณด์กด
227
- latex_parts = []
228
- def save_latex(match):
229
- latex_parts.append(match.group(0))
230
- return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
231
-
232
- text = re.sub(r'\$\$.*?\$\$', save_latex, text)
233
-
234
- # 2. ํŠน์ˆ˜ ๋ช…๋ น์–ด ์ฒ˜๋ฆฌ
235
- for cmd, latex_cmd in latex_commands.items():
236
- text = text.replace(cmd, latex_cmd)
237
-
238
- # 3. ๋ถ™์–ด์žˆ๋Š” ๋‹จ์–ด ๋ถ„๋ฆฌ
239
- text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
240
- text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
241
- text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
242
-
243
- # 4. ๋ฌธ์žฅ ๋ถ„์„
244
- sentences = text.split('$$')
245
- formatted_sentences = []
246
-
247
- for i, sentence in enumerate(sentences):
248
- if i % 2 == 0: # ์ผ๋ฐ˜ ํ…์ŠคํŠธ
249
- # ์ˆ˜์‹์ด ์•„๋‹Œ ๋ถ€๋ถ„์˜ ํŠน์ˆ˜ ๋ฌธ์ž ์ฒ˜๋ฆฌ
250
- for cmd, latex_cmd in latex_commands.items():
251
- if cmd in sentence:
252
- sentence = f"$${sentence}$$"
253
- break
254
- formatted_sentences.append(sentence)
255
- else: # ์ˆ˜์‹
256
- formatted_sentences.append(f"$${sentence}$$")
257
-
258
- text = ''.join(formatted_sentences)
259
-
260
- # 5. LaTeX ์ˆ˜์‹ ๋ณต์›
261
- for i, latex in enumerate(latex_parts):
262
- text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
263
-
264
- # 6. ๋งˆ์ง€๋ง‰ ์ •๋ฆฌ
265
- text = text.replace('\\\\', '\\') # ์ค‘๋ณต๋œ ๋ฐฑ์Šฌ๋ž˜์‹œ ์ œ๊ฑฐ
266
- text = re.sub(r'\s+', ' ', text) # ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜๋กœ
267
-
268
- return text
269
-
270
  def main():
271
  """๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง"""
272
  st.title("MisconcepTutor")
 
5
  from src.ThirdModule.module3 import AnswerVerifier
6
  import logging
7
  from typing import Optional, Tuple
8
+ from latex_formatter import LatexFormatter # LaTeX ํฌ๋งทํ„ฐ import
9
+
10
  logging.basicConfig(level=logging.DEBUG)
11
 
12
 
 
185
  if st.session_state.current_question_index >= 10:
186
  st.session_state.current_step = 'review'
187
 
188
+ # ์ „์—ญ LaTeX ํฌ๋งทํ„ฐ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
189
+ latex_formatter = LatexFormatter()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  def display_math_content(content: str):
192
  """์ˆ˜ํ•™ ๋‚ด์šฉ์„ ํ™”๋ฉด์— ํ‘œ์‹œ"""
193
+ formatted_content = latex_formatter.format_expression(content)
 
194
  st.markdown(formatted_content, unsafe_allow_html=True)
195
 
196
+ def format_answer_choice(choice: str) -> str:
197
+ """์„ ํƒ์ง€ LaTeX ํฌ๋งทํŒ…"""
198
+ return latex_formatter.format_expression(choice)
199
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  def main():
201
  """๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง"""
202
  st.title("MisconcepTutor")