File size: 10,026 Bytes
707db97
 
 
 
 
c6c2112
674515d
707db97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674515d
 
 
 
 
 
 
 
 
 
707db97
 
 
674515d
 
 
707db97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674515d
707db97
 
 
 
 
674515d
 
 
 
 
 
 
 
 
707db97
 
 
 
674515d
 
707db97
 
 
 
 
674515d
 
 
707db97
 
674515d
 
707db97
674515d
 
 
 
 
 
 
 
 
 
707db97
 
674515d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707db97
674515d
 
 
 
 
 
 
 
 
 
707db97
 
674515d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707db97
 
674515d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
import pandas as pd
import math
from plotly import graph_objects as go

# Load model and tokenizer
model_ids = {
    "ERNIE-4.5-PT": "baidu/ERNIE-4.5-0.3B-PT",
    "ERNIE-4.5-Base-PT": "baidu/ERNIE-4.5-0.3B-Base-PT"
}

tokenizers = {
    name: AutoTokenizer.from_pretrained(path)
    for name, path in model_ids.items()
}

models = {
    name: AutoModelForCausalLM.from_pretrained(path).eval()
    for name, path in model_ids.items()
}

# Helper function to format probability
def format_prob(prob):
    """Format probability as percentage with 1 decimal place"""
    return f"{prob*100:.1f}%"

# Helper function to format log probability
def format_log_prob(log_prob):
    """Format log probability with color coding"""
    return f"{log_prob:.3f}"

# Main function: compute token-wise log probabilities and top-k predictions
@torch.no_grad()
def compare_models(text, top_k=5):
    if not text.strip():
        return None, "⚠️ Please enter some text to analyze"
    
    results = {}

    for model_name in model_ids:
        tokenizer = tokenizers[model_name]
        model = models[model_name]

        # Tokenize input
        inputs = tokenizer(text, return_tensors="pt")
        input_ids = inputs["input_ids"]

        # Get model output logits
        outputs = model(**inputs)
        shift_logits = outputs.logits[:, :-1, :]          # Align prediction with target
        shift_labels = input_ids[:, 1:]                   # Shift labels to match predictions

        # Compute log probabilities
        log_probs = F.log_softmax(shift_logits, dim=-1)
        token_log_probs = log_probs.gather(2, shift_labels.unsqueeze(-1)).squeeze(-1)

        total_log_prob = token_log_probs.sum().item()
        tokens = tokenizer.convert_ids_to_tokens(input_ids[0])[1:]  # Skip BOS token

        # Generate top-k predictions for each position (up to first 20 tokens)
        topk_list = []
        confidence_list = []
        for i in range(min(20, shift_logits.shape[1])):
            topk = torch.topk(log_probs[0, i], k=top_k)
            topk_ids = topk.indices.tolist()
            topk_scores = topk.values.tolist()
            topk_tokens = tokenizer.convert_ids_to_tokens(topk_ids)
            topk_probs = [math.exp(s) for s in topk_scores]
            
            # Format top-k predictions with probabilities
            topk_formatted = [f"{tok} ({format_prob(p)})" for tok, p in zip(topk_tokens, topk_probs)]
            topk_list.append(", ".join(topk_formatted))
            
            # Calculate confidence (probability of actual token)
            actual_token_prob = math.exp(token_log_probs[0, i].item())
            confidence_list.append(actual_token_prob)

        # Prepare dataframe for display
        df = pd.DataFrame({
            "Token": tokens[:20],
            "LogProb": [format_log_prob(float(x)) for x in token_log_probs[0][:20]],
            "Confidence": [format_prob(x) for x in confidence_list[:20]],
            f"Top-{top_k} Predictions": topk_list
        })

        results[model_name] = {
            "df": df,
            "total_log_prob": total_log_prob,
            "tokens": tokens[:20],
            "confidences": confidence_list[:20]
        }

    # Create comparison dataframe
    comparison_df = pd.DataFrame({
        "Token": results["ERNIE-4.5-PT"]["df"]["Token"],
        "ERNIE-4.5-PT": {
            "LogProb": results["ERNIE-4.5-PT"]["df"]["LogProb"],
            "Confidence": results["ERNIE-4.5-PT"]["df"]["Confidence"],
            "Top-k": results["ERNIE-4.5-PT"]["df"][f"Top-{top_k} Predictions"]
        },
        "ERNIE-4.5-Base-PT": {
            "LogProb": results["ERNIE-4.5-Base-PT"]["df"]["LogProb"],
            "Confidence": results["ERNIE-4.5-Base-PT"]["df"]["Confidence"],
            "Top-k": results["ERNIE-4.5-Base-PT"]["df"][f"Top-{top_k} Predictions"]
        }
    })

    # Create visualization
    fig = go.Figure()
    
    # Add confidence bars for both models
    fig.add_trace(go.Bar(
        name='ERNIE-4.5-PT',
        x=results["ERNIE-4.5-PT"]["tokens"],
        y=results["ERNIE-4.5-PT"]["confidences"],
        marker_color='royalblue'
    ))
    
    fig.add_trace(go.Bar(
        name='ERNIE-4.5-Base-PT',
        x=results["ERNIE-4.5-Base-PT"]["tokens"],
        y=results["ERNIE-4.5-Base-PT"]["confidences"],
        marker_color='lightseagreen'
    ))
    
    fig.update_layout(
        title='Model Confidence Comparison',
        xaxis_title='Token',
        yaxis_title='Confidence (Probability)',
        barmode='group',
        yaxis=dict(tickformat='.0%', range=[0, 1]),
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=1
        )
    )

    # Create summary
    pt_logprob = results['ERNIE-4.5-PT']['total_log_prob']
    base_logprob = results['ERNIE-4.5-Base-PT']['total_log_prob']
    
    # Determine which model has higher confidence
    if pt_logprob > base_logprob:
        better_model = "ERNIE-4.5-PT"
        difference = pt_logprob - base_logprob
    else:
        better_model = "ERNIE-4.5-Base-PT"
        difference = base_logprob - pt_logprob
    
    summary = (
        f"πŸ“Š **Model Comparison Summary**\n\n"
        f"**Total Log Probability**:\n"
        f"- ERNIE-4.5-PT: {pt_logprob:.3f}\n"
        f"- ERNIE-4.5-Base-PT: {base_logprob:.3f}\n\n"
        f"πŸ† **Higher Confidence Model**: {better_model}\n"
        f"Difference: {difference:.3f} ({'+' if better_model == 'ERNIE-4.5-PT' else '-'}{difference:.3f})\n\n"
        f"**What this means**:\n"
        f"- Log probability closer to 0 (less negative) indicates higher model confidence\n"
        f"- The {better_model} model is more confident in predicting your input text\n"
        f"- Confidence per token is shown in the table and chart below"
    )

    return comparison_df, summary, fig

# Create custom CSS for better styling
css = """
.main-container {
    max-width: 1200px;
    margin: 0 auto;
}
.dataframe-container {
    margin: 20px 0;
}
.confidence-chart {
    margin: 20px 0;
    height: 400px;
}
.summary-box {
    background-color: #f8f9fa;
    border-left: 4px solid #4285f4;
    padding: 15px;
    border-radius: 4px;
    margin: 20px 0;
}
.model-header {
    font-weight: bold;
    color: #1a73e8;
    margin-top: 10px;
}
.token-cell {
    font-family: monospace;
    background-color: #f1f3f4;
    padding: 4px 8px;
    border-radius: 3px;
}
.confidence-high {
    color: #0f9d58;
    font-weight: bold;
}
.confidence-medium {
    color: #f4b400;
}
.confidence-low {
    color: #db4437;
}
"""

# Gradio interface with improved layout
with gr.Blocks(css=css, title="ERNIE Model Comparison Tool") as demo:
    gr.Markdown(
        """
        # πŸ” ERNIE 4.5 Model Comparison Tool
        
        Compare how different ERNIE models process your text with detailed token-level analysis.
        
        ## What this tool shows:
        - **Token Log Probability**: How confident the model is in predicting each token (closer to 0 is better)
        - **Confidence**: Probability percentage for each token prediction
        - **Top-k Predictions**: What other tokens the model considered likely
        - **Visual Comparison**: Bar chart showing confidence differences between models
        """
    )
    
    with gr.Row():
        with gr.Column(scale=3):
            input_text = gr.Textbox(
                lines=3, 
                placeholder="Enter text to analyze (e.g., 'Hello, World!')", 
                label="Input Text",
                value="Hello, World!"
            )
        with gr.Column(scale=1):
            top_k = gr.Slider(
                minimum=1, 
                maximum=10, 
                value=3, 
                step=1, 
                label="Top-k Predictions"
            )
    
    with gr.Row():
        compare_btn = gr.Button("Compare Models", variant="primary")
    
    with gr.Row():
        with gr.Column():
            summary_box = gr.Markdown(
                elem_classes=["summary-box"],
                label="Model Comparison Summary"
            )
    
    with gr.Row():
        with gr.Column():
            comparison_table = gr.Dataframe(
                label="Token-Level Analysis",
                elem_classes=["dataframe-container"],
                interactive=False,
                wrap=True
            )
    
    with gr.Row():
        with gr.Column():
            confidence_chart = gr.Plot(
                label="Model Confidence Comparison",
                elem_classes=["confidence-chart"]
            )
    
    # Examples section
    gr.Examples(
        examples=[
            ["Hello, World!", 3],
            ["The quick brown fox jumps over the lazy dog.", 5],
            ["Artificial intelligence will transform our society.", 3],
            ["What is the meaning of life?", 4]
        ],
        inputs=[input_text, top_k],
        label="Try these examples:"
    )
    
    # Footer with explanation
    gr.Markdown(
        """
        ## How to Interpret Results
        
        1. **Log Probability**: Negative values where closer to 0 means higher model confidence
        2. **Confidence**: Percentage showing how certain the model was about each token
        3. **Top-k Predictions**: Alternative tokens the model considered likely
        4. **Visual Chart**: Bar heights represent model confidence for each token
        
        **Model Differences**:
        - **ERNIE-4.5-PT**: Instruction-tuned model, better at following complex instructions
        - **ERNIE-4.5-Base-PT**: Base model, better at general language patterns
        """
    )
    
    # Set up event handler
    compare_btn.click(
        fn=compare_models,
        inputs=[input_text, top_k],
        outputs=[comparison_table, summary_box, confidence_chart]
    )

if __name__ == "__main__":
    demo.launch()