Khoi1234210 commited on
Commit
8c6ac41
·
verified ·
1 Parent(s): 30b060a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -109,31 +109,21 @@ def create_math_system_message():
109
  Always be precise, educational, and encourage mathematical thinking."""
110
 
111
  def render_latex(text):
112
- """Enhanced LaTeX rendering with better error handling and formatting"""
113
- if not text or len(text) < 5:
114
  return text
115
 
116
  try:
117
- # Ensure proper LaTeX delimiters
118
- text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text) # Inline math
119
- text = re.sub(r'\$\$([^\$]+)\$\$', r'$$\1$$', text) # Display math
120
- text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text) # Display math alternative
121
- text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text) # Inline math alternative
122
 
123
- # Fix common LaTeX commands
124
- 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)
125
-
126
- # Ensure boxed answers and fractions render correctly
127
- text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
128
- text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
129
-
130
- # Clean extra spaces and escape issues
131
- text = re.sub(r'\s+([\$\\])\s+', r'\1', text)
132
- text = text.replace(r'frac', r'\frac') # Catch any unescaped frac
133
 
134
  except Exception as e:
135
- print(f"⚠️ LaTeX formatting error: {e}")
136
- return text # Return original text if formatting fails
137
 
138
  return text
139
 
 
109
  Always be precise, educational, and encourage mathematical thinking."""
110
 
111
  def render_latex(text):
112
+ """Minimal LaTeX cleanup - let the model do the work"""
113
+ if not text:
114
  return text
115
 
116
  try:
117
+ # Convert LaTeX bracket notation to dollar signs
118
+ text = re.sub(r'\\\[(.*?)\\\]', r'$$\1$$', text, flags=re.DOTALL)
119
+ text = re.sub(r'\\\((.*?)\\\)', r'$\1$', text, flags=re.DOTALL)
 
 
120
 
121
+ # Fix boxed answers ONLY if not already in math mode
122
+ if '\\boxed' in text and not re.search(r'\$.*\\boxed.*\$', text):
123
+ text = re.sub(r'\\boxed\{([^}]+)\}', r'$\\boxed{\1}$', text)
 
 
 
 
 
 
 
124
 
125
  except Exception as e:
126
+ print(f"⚠️ LaTeX error: {e}")
 
127
 
128
  return text
129