yasserrmd commited on
Commit
480f256
·
verified ·
1 Parent(s): 3532724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -9,20 +9,29 @@ client = InferenceClient()
9
 
10
 
11
  def preprocess_latex(content):
12
- """Preprocess the AI response to format LaTeX properly."""
 
 
13
  lines = content.split("\n") # Split response into lines
14
  processed_lines = []
15
-
16
  for line in lines:
17
- if "[" in line and "]" in line: # Detect math expressions in brackets
18
  while "[" in line and "]" in line:
19
  start = line.index("[")
20
  end = line.index("]") + 1
21
- math_expr = line[start:end].strip("[]").strip()
22
- line = line[:start] + f"$$ {math_expr} $$" + line[end:]
 
 
 
 
 
 
 
23
  processed_lines.append(line)
24
 
25
- return "\n".join(processed_lines) # Join lines back into a single string
26
 
27
 
28
 
 
9
 
10
 
11
  def preprocess_latex(content):
12
+ """
13
+ Preprocess the AI response using sympy to properly format mathematical expressions.
14
+ """
15
  lines = content.split("\n") # Split response into lines
16
  processed_lines = []
17
+
18
  for line in lines:
19
+ if "[" in line and "]" in line: # Detect mathematical expressions in brackets
20
  while "[" in line and "]" in line:
21
  start = line.index("[")
22
  end = line.index("]") + 1
23
+ math_expr = line[start + 1 : end - 1].strip()
24
+ try:
25
+ # Parse the math expression and convert to LaTeX
26
+ sympy_expr = sympify(math_expr)
27
+ latex_expr = latex(sympy_expr)
28
+ line = line[:start] + f"$$ {latex_expr} $$" + line[end:]
29
+ except Exception:
30
+ # Fallback to raw math if parsing fails
31
+ line = line[:start] + f"$$ {math_expr} $$" + line[end:]
32
  processed_lines.append(line)
33
 
34
+ return "\n".join(processed_lines)
35
 
36
 
37