Khoi1234210 commited on
Commit
47e7b60
·
verified ·
1 Parent(s): 225572b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -108,29 +108,31 @@ def create_math_system_message():
108
  Always be precise, educational, and encourage mathematical thinking."""
109
 
110
  def render_latex(text):
111
- """Enhanced LaTeX rendering - fixes raw code output"""
112
  if not text or len(text) < 5:
113
  return text
114
 
115
  try:
116
- # Fix common LaTeX patterns from Qwen
117
- text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text)
118
- text = re.sub(r'\$\$([^\$]+)\$$', r'$$\1$$', text)
119
- text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text)
120
- text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text)
121
 
122
- # Fix escaped LaTeX commands
123
- text = re.sub(r'\\(lim|frac|sqrt|int|sum|prod|partial|nabla|infty|to|le|ge|neq|approx|cdot|times|div|deg|prime|log|ln|sin|cos|tan|cot|sec|csc|arcsin|arccos|arctan|sinh|cosh)', r'\1', text)
124
 
125
- # Ensure boxed answers render
126
  text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
127
  text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
128
 
129
- # Clean up extra spaces
130
- text = re.sub(r'\s*([\$\\])\s*', r'\1', text)
 
131
 
132
  except Exception as e:
133
  print(f"⚠️ LaTeX formatting error: {e}")
 
134
 
135
  return text
136
 
 
108
  Always be precise, educational, and encourage mathematical thinking."""
109
 
110
  def render_latex(text):
111
+ """Enhanced LaTeX rendering with better error handling and formatting"""
112
  if not text or len(text) < 5:
113
  return text
114
 
115
  try:
116
+ # Ensure proper LaTeX delimiters
117
+ text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text) # Inline math
118
+ text = re.sub(r'\$\$([^\$]+)\$\$', r'$$\1$$', text) # Display math
119
+ text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text) # Display math alternative
120
+ text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text) # Inline math alternative
121
 
122
+ # Fix common LaTeX commands
123
+ text = re.sub(r'\\(lim|frac|sqrt|int|sum|prod|partial|nabla|infty|to|le|ge|neq|approx|cdot|times|div|deg|prime|log|ln|sin|cos|tan|cot|sec|csc|arcsin|arccos|arctan|sinh|cosh)', r'\\\1', text)
124
 
125
+ # Ensure boxed answers and fractions render correctly
126
  text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
127
  text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
128
 
129
+ # Clean extra spaces and escape issues
130
+ text = re.sub(r'\s+([\$\\])\s+', r'\1', text)
131
+ text = text.replace(r'frac', r'\frac') # Catch any unescaped frac
132
 
133
  except Exception as e:
134
  print(f"⚠️ LaTeX formatting error: {e}")
135
+ return text # Return original text if formatting fails
136
 
137
  return text
138