KEMBAR78
Fix UnboundLocalError in WER metric computation by prxshetty · Pull Request #40402 · huggingface/transformers · GitHub
Skip to content

Conversation

@prxshetty
Copy link
Contributor

Summary

Fixes a variable naming conflict in the compute_metrics function example in the ASR documentation that causes an UnboundLocalError.

Problem

The original code had a naming conflict where wer was used both as:

  1. The loaded evaluate metric (wer = evaluate.load("wer"))
  2. The variable to store the computed result (wer = wer.compute(...))

This caused Python to treat wer as a local variable throughout the function, leading to an UnboundLocalError when trying to access it before assignment.

Solution

  • Renamed the global metric variable from wer to wer_metric
  • Stored the result in wer_score

Changes

  • docs/source/en/tasks/asr.md: Fixed variable naming in compute_metrics example

Testing

  • Code runs without UnboundLocalError
  • WER metric computation works correctly
  • Documentation builds successfully

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Documentation update

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, LGTM. Thank you!

@Rocketknight1 Rocketknight1 enabled auto-merge (squash) August 25, 2025 11:53
Renamed wer metric variable to wer_metric to avoid naming conflict
with local variable assignment in compute_metrics function.
@Rocketknight1 Rocketknight1 merged commit d73181b into huggingface:main Aug 25, 2025
14 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants