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 a summarization task, these metrics are used to evaluate how well the generated summary captures the information and structure of the source text. Here are the mathematical formulations for each metric:

---

### 1. **ROUGE-1 and ROUGE-2**  
ROUGE-1 and ROUGE-2 measure the overlap of unigrams (ROUGE-1) and bigrams (ROUGE-2) between the generated summary and the reference summary, focusing on precision and recall.

For ROUGE-N, where \(N\) is 1 or 2, the formula is:
\[
\text{ROUGE-N (Precision)} = \frac{\text{Number of matching N-grams}}{\text{Total N-grams in generated summary}}
\]
\[
\text{ROUGE-N (Recall)} = \frac{\text{Number of matching N-grams}}{\text{Total N-grams in reference summary}}
\]
\[
\text{ROUGE-N (F1-score)} = \frac{(1 + \beta^2) \times \text{ROUGE-N Precision} \times \text{ROUGE-N Recall}}{\beta^2 \times \text{ROUGE-N Precision} + \text{ROUGE-N Recall}}
\]
where \(\beta\) is typically set to 1 for a balanced F-score.

---

### 2. **ROUGE-L**  
ROUGE-L measures the longest common subsequence (LCS) overlap, which helps capture the longest sequence of words that appear in both the generated and reference summaries in the same order. This is useful for summarization because it measures structural similarity.

The formula for ROUGE-L is:
\[
\text{ROUGE-L (Precision)} = \frac{\text{LCS length}}{\text{Length of generated summary}}
\]
\[
\text{ROUGE-L (Recall)} = \frac{\text{LCS length}}{\text{Length of reference summary}}
\]
\[
\text{ROUGE-L (F1-score)} = \frac{(1 + \beta^2) \times \text{ROUGE-L Precision} \times \text{ROUGE-L Recall}}{\beta^2 \times \text{ROUGE-L Precision} + \text{ROUGE-L Recall}}
\]
where LCS length is the length of the longest common subsequence between the generated and reference summaries.

---

### 3. **Content Fidelity**  
Content Fidelity measures how accurately the content of the summary reflects the content of the source text. While there are various ways to compute fidelity, one common approach is through cosine similarity between the embeddings of the source text and generated summary:

\[
\text{Content Fidelity} = \frac{\text{Emb(Source Text)} \cdot \text{Emb(Summary)}}{\|\text{Emb(Source Text)}\| \times \|\text{Emb(Summary)}\|}
\]
where:
- \(\text{Emb(Source Text)}\) and \(\text{Emb(Summary)}\) are the vector embeddings of the source text and summary, respectively.
- \(\|\text{Emb(Source Text)}\|\) and \(\|\text{Emb(Summary)}\|\) represent the magnitudes (norms) of the embeddings.

Alternatively, Jaccard similarity can also be used:
\[
\text{Content Fidelity (Jaccard)} = \frac{|S \cap R|}{|S \cup R|}
\]
where \(S\) is the set of unique terms in the summary and \(R\) is the set of unique terms in the source text.

---

These formulations allow evaluation of both word-based and semantic similarity between generated summaries and their source texts, with ROUGE metrics capturing word overlap and Content Fidelity ensuring alignment in content.