def generate_diff(old_text, new_text):
    """Generate HTML diff between two prediction texts"""
    d = Differ()
    diff = list(d.compare(str(old_text).splitlines(), str(new_text).splitlines()))
    
    html = []
    for line in diff:
        if line.startswith('+ '):
            html.append(f'<span style="color: green; background-color: #e6ffe6;">{line[2:]}</span><br>')
        elif line.startswith('- '):
            html.append(f'<span style="color: red; background-color: #ffe6e6;">{line[2:]}</span><br>')
        elif line.startswith('? '):
            continue
        else:
            html.append(f"{line[2:]}<br>")
    return "".join(html)
Generate HTML diff between two prediction texts
examples/simplelr_math_eval/visualizer.py