import json import plotly.graph_objects as go from typing import Dict, List, Tuple from collections import defaultdict def create_distribution_plots(metrics_data: Dict[str, List[float]]) -> List[str]: """Create distribution histogram plots for each metric.""" figures = [] for metric, scores in metrics_data.items(): fig = go.Figure() fig.add_trace(go.Histogram(x=scores, name=metric, nbinsx=10, marker_color="#1f77b4")) fig.update_layout( title=f"{metric} Score Distribution", xaxis_title="Score", yaxis_title="Count", bargap=0.1, template="seaborn", ) figures.append(fig.to_html(full_html=False)) return figures def create_ci_plot(ci_results: Dict[str, Tuple[float, float, float]]) -> str: """Create confidence interval bar plot.""" fig = go.Figure() for metric, (mean_score, lower, upper) in ci_results.items(): fig.add_trace( go.Bar( x=[metric], y=[mean_score], error_y=dict( type="data", array=[upper - mean_score], arrayminus=[mean_score - lower], visible=True, ), name=metric, ) ) fig.update_layout( title="95% confidence interval for all the metrics", xaxis_title="Metric", yaxis_title="Score", template="seaborn", ) return fig.to_html(full_html=False) def generate_details_html(metrics_data: List[Dict]) -> List[str]: """Generate HTML for detailed metric information.""" details_html = [] metric_details = {} # Organize metrics by type for entry in metrics_data: for metric, values in entry["metrics"].items(): if metric not in metric_details: metric_details[metric] = [] metric_details[metric].append( { "question": entry["question"], "answer": entry["answer"], "golden_answer": entry["golden_answer"], "reason": values.get("reason", ""), "score": values["score"], } ) for metric, details in metric_details.items(): details_html.append(f"
| Question | Answer | Golden Answer | Reason | Score |
|---|---|---|---|---|
| {item['question']} | " f"{item['answer']} | " f"{item['golden_answer']} | " f"{item['reason']} | " f"{item['score']} | " f"