Spaces:
Sleeping
Sleeping
| """ | |
| Visualizador de Semantic Highlighting para Agent Pruning Lab | |
| """ | |
| import plotly.graph_objects as go | |
| import numpy as np | |
| class SemanticHighlighter: | |
| """Gera visualizações interativas de relevância semântica""" | |
| def render_semantic_highlighting(self, sentences, scores): | |
| """ | |
| Renderiza texto com highlighting semântico baseado em scores | |
| """ | |
| html_parts = ['<div style="line-height: 2; font-size: 16px; padding: 20px; background: #fafafa; border-radius: 10px;">'] | |
| for sent, score in zip(sentences, scores): | |
| # Mapeia score para intensidade de cor | |
| intensity = int(score * 255) | |
| if score >= 0.7: | |
| color = f"rgba(255, 135, 135, {score * 0.3})" # Vermelho suave | |
| border = "2px solid #ff8787" | |
| elif score >= 0.4: | |
| color = f"rgba(255, 216, 168, {score * 0.3})" # Laranja suave | |
| border = "2px solid #ffd8a8" | |
| else: | |
| color = f"rgba(255, 243, 191, {score * 0.3})" # Amarelo suave | |
| border = "1px solid #ffe066" | |
| if score < 0.2: | |
| # Texto de baixa relevância - riscado e opaco | |
| html_parts.append( | |
| f'<span style="text-decoration: line-through; color: #adb5bd; opacity: 0.6; padding: 2px 4px;">' | |
| f'{sent}</span> ' | |
| ) | |
| else: | |
| html_parts.append( | |
| f'<span style="background-color: {color}; border: {border}; ' | |
| f'border-radius: 4px; padding: 2px 6px; margin: 1px; ' | |
| f'transition: all 0.3s;" title="Relevância: {score:.2f}">' | |
| f'{sent}</span> ' | |
| ) | |
| html_parts.append('</div>') | |
| return "".join(html_parts) | |
| def render_heatmap(self, sentences, scores): | |
| """ | |
| Cria heatmap Plotly das sentenças e seus scores | |
| """ | |
| fig = go.Figure(data=go.Heatmap( | |
| z=[scores], | |
| x=[f"S{i+1}" for i in range(len(sentences))], | |
| y=["Relevância"], | |
| colorscale=[ | |
| [0, "#fff3bf"], | |
| [0.5, "#ffd8a8"], | |
| [1, "#ff8787"] | |
| ], | |
| showscale=True, | |
| text=[[f"{s[:30]}..." for s in sentences]], | |
| hovertemplate='Sentença: %{text}<br>Score: %{z:.2f}<extra></extra>' | |
| )) | |
| fig.update_layout( | |
| title="Mapa de Calor de Relevância Semântica", | |
| xaxis_title="Sentenças", | |
| yaxis_title="", | |
| height=200, | |
| template="plotly_white" | |
| ) | |
| return fig |