Spaces:
Running on Zero
Running on Zero
SVG: standalone download (XML prolog, white bg, fixed dims) + Plotly first
Browse files- src/models/svg_renderer.py +13 -3
src/models/svg_renderer.py
CHANGED
|
@@ -65,16 +65,26 @@ class SVGRenderer:
|
|
| 65 |
self.tokenizer = None
|
| 66 |
|
| 67 |
def generate(self, chart_spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if self.model is not None and self.tokenizer is not None:
|
| 69 |
try:
|
| 70 |
svg = self._generate_model(chart_spec, data)
|
| 71 |
if is_renderable_svg(svg):
|
| 72 |
return apply_theme(svg)
|
| 73 |
-
logger.info("Model SVG failed validation; using Plotly fallback")
|
| 74 |
except Exception as e:
|
| 75 |
-
logger.warning(f"Model SVG generation error: {e}
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
return apply_theme(svg)
|
| 79 |
|
| 80 |
def _generate_model(self, chart_spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|
|
|
|
| 65 |
self.tokenizer = None
|
| 66 |
|
| 67 |
def generate(self, chart_spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|
| 68 |
+
"""Plotly first (reliable, consistent theming), trained model as fallback."""
|
| 69 |
+
try:
|
| 70 |
+
svg = self._plotly.render(chart_spec, data)
|
| 71 |
+
if is_renderable_svg(svg):
|
| 72 |
+
return apply_theme(svg)
|
| 73 |
+
logger.info("Plotly returned non-SVG; trying model")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.warning(f"Plotly render failed ({e}); trying model")
|
| 76 |
+
|
| 77 |
if self.model is not None and self.tokenizer is not None:
|
| 78 |
try:
|
| 79 |
svg = self._generate_model(chart_spec, data)
|
| 80 |
if is_renderable_svg(svg):
|
| 81 |
return apply_theme(svg)
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
+
logger.warning(f"Model SVG generation error: {e}")
|
| 84 |
|
| 85 |
+
# Last resort: native Python SVG (always produces something)
|
| 86 |
+
from src.visualization.plotly_fallback import PlotlyRenderer
|
| 87 |
+
svg = self._plotly._empty("Could not render chart; see Data section.")
|
| 88 |
return apply_theme(svg)
|
| 89 |
|
| 90 |
def _generate_model(self, chart_spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|