liaoch commited on
Commit
127229f
·
1 Parent(s): d01fcae

add error handling

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +30 -1
  3. requirements.txt +3 -1
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ venv
2
+ __pycache__
app.py CHANGED
@@ -112,7 +112,36 @@ with gr.Blocks(title="Mermaid Diagram Renderer", theme=gr.themes.Soft()) as demo
112
  else:
113
  preview_path = None
114
 
115
- return preview_path, output_path, f"Successfully rendered {output_format.upper()} with {theme} theme"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  except Exception as e:
118
  logging.error(f"Rendering failed: {e}")
 
112
  else:
113
  preview_path = None
114
 
115
+ # Check for Mermaid syntax errors in SVG or PNG output
116
+ status_message = f"Successfully rendered {output_format.upper()} with {theme} theme"
117
+ if output_format == "svg" and output_path and os.path.exists(output_path):
118
+ try:
119
+ with open(output_path, "r", encoding="utf-8") as f:
120
+ svg_content = f.read()
121
+ # Look for common error indicators in the SVG output
122
+ if "SyntaxError" in svg_content or "Parse error" in svg_content or "Error:" in svg_content:
123
+ # Extract the error message (simple heuristic)
124
+ import re
125
+ error_lines = re.findall(r'>([^<>]*Error[^<>]*)<', svg_content)
126
+ error_text = error_lines[0] if error_lines else "Mermaid syntax error"
127
+ status_message = f"Error: {error_text}"
128
+ except Exception as svg_err:
129
+ logging.warning(f"Could not check SVG for errors: {svg_err}")
130
+ elif output_format == "png" and output_path and os.path.exists(output_path):
131
+ try:
132
+ from PIL import Image
133
+ import pytesseract
134
+ img = Image.open(output_path)
135
+ ocr_text = pytesseract.image_to_string(img)
136
+ if "Syntax error" in ocr_text or "Parse error" in ocr_text or "Error" in ocr_text:
137
+ # Extract the error message (simple heuristic)
138
+ lines = [line for line in ocr_text.splitlines() if "error" in line.lower()]
139
+ error_text = lines[0] if lines else "Mermaid syntax error"
140
+ status_message = f"Error: {error_text}"
141
+ except Exception as png_err:
142
+ logging.warning(f"Could not OCR PNG for errors: {png_err}")
143
+
144
+ return preview_path, output_path, status_message
145
 
146
  except Exception as e:
147
  logging.error(f"Rendering failed: {e}")
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- gradio>=5.0.0
 
 
 
1
+ gradio>=5.0.0
2
+ pytesseract
3
+ Pillow