Following this instruction :
1. Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
2. Use appropriate modules like `typing` for type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively.
3. Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
4. Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
5. Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.

TASK : Generalised robustness metrics Pipeline (logits or predictions or loss values)

NOTE : using the only torch module we have implement below scratch 

Problem Statement:

For speech recognition tasks, these metrics evaluate the accuracy of transcriptions by comparing them to reference transcriptions at the word, character, or phoneme level. Here are the equations:

---

### 1. **Word Error Rate (WER)**  
Word Error Rate (WER) measures the ratio of errors (insertions, deletions, and substitutions) to the total number of words in the reference transcription.

WER is calculated as:
\[
\text{WER} = \frac{S + D + I}{N}
\]
where:
- \(S\) is the number of substitutions (words in the transcription that differ from the reference).
- \(D\) is the number of deletions (words present in the reference but missing in the transcription).
- \(I\) is the number of insertions (extra words in the transcription not in the reference).
- \(N\) is the total number of words in the reference transcription.

WER is often calculated using the Levenshtein distance to count the minimum number of edits needed.

---

### 2. **Character Error Rate (CER)**  
Character Error Rate (CER) measures errors at the character level, suitable for languages or applications where character-level accuracy is critical.

CER is calculated similarly to WER:
\[
\text{CER} = \frac{S + D + I}{C}
\]
where:
- \(S\), \(D\), and \(I\) are the counts of substitutions, deletions, and insertions, respectively, at the character level.
- \(C\) is the total number of characters in the reference transcription.

Like WER, CER uses the Levenshtein distance to compute the minimum number of character edits needed.

---

### 3. **Phoneme Error Rate (PER)**  
Phoneme Error Rate (PER) measures errors at the phoneme level, useful in evaluating phoneme recognition systems or phonetic accuracy in speech recognition.

The formula for PER is:
\[
\text{PER} = \frac{S + D + I}{P}
\]
where:
- \(S\), \(D\), and \(I\) are the number of substitutions, deletions, and insertions, respectively, counted at the phoneme level.
- \(P\) is the total number of phonemes in the reference transcription.

PER also typically uses the Levenshtein distance at the phoneme level to compute the required edits.

---

### 4. **BLEU (Speech)**  
BLEU (Bilingual Evaluation Understudy) score is used to evaluate the overlap of n-grams between the transcription and the reference. For speech recognition, BLEU measures how precisely the transcribed words match the reference at the n-gram level.

For an n-gram order \(N\), BLEU is calculated as:
\[
\text{BLEU} = \exp \left( \sum_{n=1}^N w_n \log \text{Precision}_n \right) \times \text{BP}
\]
where:
- \(w_n\) are weights for each n-gram order (commonly uniform, \(w_n = \frac{1}{N}\)).
- \(\text{Precision}_n\) is the modified precision for each n-gram order \(n\), i.e., the number of matching n-grams divided by the number of n-grams in the transcription.
- \(\text{BP}\) is the brevity penalty:
  \[
  \text{BP} = 
  \begin{cases} 
      1 & \text{if } c > r \\ 
      \exp\left(1 - \frac{r}{c}\right) & \text{if } c \leq r 
  \end{cases}
  \]
  where \(c\) is the length of the transcription and \(r\) is the length of the reference transcription.

---

These metrics provide a comprehensive assessment of speech transcription accuracy, covering different linguistic units (words, characters, phonemes) and capturing both overall and n-gram matching accuracy.