Skip to content

Commit ba31482

Browse files
committed
feat: wrapping long text in plots
1 parent c3c92b7 commit ba31482

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/rai_bench/rai_bench/results_processing/visualise.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ def display_task_complexity_performance(model_results: ModelResults):
258258
st.plotly_chart(fig_complexity_calls, use_container_width=True) # type: ignore
259259

260260

261+
def wrap_text(text: str, max_width: int = 50):
262+
"""Wrap text to multiple lines if it's too long"""
263+
words = text.split()
264+
lines: List[str] = []
265+
current_line = []
266+
current_length = 0
267+
268+
for word in words:
269+
if current_length + len(word) + len(current_line) <= max_width:
270+
current_line.append(word)
271+
current_length += len(word)
272+
else:
273+
lines.append(" ".join(current_line))
274+
current_line = [word]
275+
current_length = len(word)
276+
277+
if current_line:
278+
lines.append(" ".join(current_line))
279+
280+
return "<br>".join(lines)
281+
282+
261283
def display_detailed_task_type_analysis(
262284
model_results: ModelResults, selected_type: str
263285
):
@@ -295,6 +317,9 @@ def display_detailed_task_type_analysis(
295317

296318
# Replace zero scores with a small value to enable hover
297319
task_stats = task_stats.replace(0, 0.01) # type: ignore
320+
321+
# wrapped text column for hover, so that text is multiline
322+
task_stats["wrapped_prompt"] = task_stats["task_prompt"].apply(wrap_text) # type: ignore
298323
task_stats["score"] = task_stats["score"].round(2)
299324
task_stats["total_time"] = task_stats["total_time"].round(2)
300325
# Create short labels for x-axis
@@ -311,7 +336,7 @@ def display_detailed_task_type_analysis(
311336
title=f"Avg Score for '{selected_type}' Tasks",
312337
x_label="Task",
313338
y_label="Avg Score",
314-
custom_data=["task_prompt", "score"],
339+
custom_data=["wrapped_prompt", "score"],
315340
hover_template="<b>Task:</b> %{customdata[0]}\n <b>Score:</b> %{customdata[1]}",
316341
y_range=(0.0, 1.0),
317342
x_tickvals=task_stats["task_prompt"].tolist(),
@@ -326,7 +351,7 @@ def display_detailed_task_type_analysis(
326351
title=f"Avg Time for '{selected_type}' Tasks",
327352
x_label="Task",
328353
y_label="Avg Time (s)",
329-
custom_data=["task_prompt", "total_time"],
354+
custom_data=["wrapped_prompt", "total_time"],
330355
hover_template="<b>Task:</b> %{customdata[0]}\n <b>Time:</b> %{customdata[1]}",
331356
x_tickvals=task_stats["task_prompt"].tolist(),
332357
x_ticktext=short_labels,

0 commit comments

Comments
 (0)