root commited on
Commit
8df070d
·
1 Parent(s): f2998c3

fix math render

Browse files
Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -7,6 +7,8 @@ import time
7
  import requests
8
  from PIL import Image
9
  import gradio as gr
 
 
10
  # =========================
11
  # Config
12
  # =========================
@@ -22,6 +24,8 @@ LATEX_DELIMS = [
22
  ]
23
  AUTH_HEADER = {"Authorization": f"bearer {TOKEN}"}
24
  JSON_HEADERS = {**AUTH_HEADER, "Content-Type": "application/json"}
 
 
25
  # =========================
26
  # Base64 and Example Loading Logic
27
  # =========================
@@ -38,6 +42,25 @@ def image_to_base64_data_url(filepath: str) -> str:
38
  print(f"Error encoding image to Base64: {e}")
39
  return ""
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def _get_examples_from_dir(dir_path: str) -> List[List[str]]:
43
  supported_exts = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}
@@ -193,7 +216,8 @@ def _process_api_response_page(result: Dict[str, Any]) -> Tuple[str, str, str]:
193
  output_html = f'<img src="{output_image_url}" alt="Detection Visualization">'
194
  else:
195
  print("Warning: No visualization image URL found in the API response.")
196
-
 
197
  return md_text or "(Empty result)", output_html, md_text
198
 
199
  # =========================
 
7
  import requests
8
  from PIL import Image
9
  import gradio as gr
10
+
11
+
12
  # =========================
13
  # Config
14
  # =========================
 
24
  ]
25
  AUTH_HEADER = {"Authorization": f"bearer {TOKEN}"}
26
  JSON_HEADERS = {**AUTH_HEADER, "Content-Type": "application/json"}
27
+
28
+
29
  # =========================
30
  # Base64 and Example Loading Logic
31
  # =========================
 
42
  print(f"Error encoding image to Base64: {e}")
43
  return ""
44
 
45
+ def _escape_inequalities_in_math(md: str) -> str:
46
+ """
47
+ Finds math blocks in a Markdown string and replaces < and > with
48
+ their LaTeX equivalents, \lt and \gt, to prevent markdown parsing errors.
49
+ """
50
+ _MATH_PATTERNS = [
51
+ re.compile(r"\$\$([\s\S]+?)\$\$"),
52
+ re.compile(r"\$([^\$]+?)\$"),
53
+ re.compile(r"\\\[([\s\S]+?)\\\]"),
54
+ re.compile(r"\\\(([\s\S]+?)\\\)"),
55
+ ]
56
+ def fix(s: str) -> str:
57
+ s = s.replace("<=", r" \le ").replace(">=", r" \ge ")
58
+ s = s.replace("≤", r" \le ").replace("≥", r" \ge ")
59
+ s = s.replace("<", r" \lt ").replace(">", r" \gt ")
60
+ return s
61
+ for pat in _MATH_PATTERNS:
62
+ md = pat.sub(lambda m: m.group(0).replace(m.group(1), fix(m.group(1))), md)
63
+ return md
64
 
65
  def _get_examples_from_dir(dir_path: str) -> List[List[str]]:
66
  supported_exts = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}
 
216
  output_html = f'<img src="{output_image_url}" alt="Detection Visualization">'
217
  else:
218
  print("Warning: No visualization image URL found in the API response.")
219
+
220
+ md_text = _escape_inequalities_in_math(md_text)
221
  return md_text or "(Empty result)", output_html, md_text
222
 
223
  # =========================