import io import base64 import matplotlib.pyplot as plt def figure_to_png_bytes(fig): """ Convert a matplotlib figure to PNG bytes. Useful for exporting images in Gradio. """ buf = io.BytesIO() fig.savefig(buf, format="png", dpi=150, bbox_inches="tight") buf.seek(0) return buf.getvalue() def safe_get(d, key, default=None): """ Helper to safely get values from dictionaries. """ if d is None: return default return d.get(key, default) def dict_to_text(d): """ Simple helper to display a dictionary as multi-line text. Useful for showing the trend summary in the Insights tab. """ if not isinstance(d, dict) or len(d) == 0: return "No insights available." lines = [] for k, v in d.items(): lines.append(f"{k}: {v}") return "\n".join(lines)