Spaces:
Sleeping
Sleeping
Update src/modernbert/classifier.py
Browse files- src/modernbert/classifier.py +65 -61
src/modernbert/classifier.py
CHANGED
|
@@ -1,61 +1,65 @@
|
|
| 1 |
-
# Necessary imports
|
| 2 |
-
import sys
|
| 3 |
-
from typing import Dict
|
| 4 |
-
|
| 5 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Necessary imports
|
| 2 |
+
import sys
|
| 3 |
+
from typing import Dict
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Local imports
|
| 9 |
+
from src.logger import logging
|
| 10 |
+
from src.exception import CustomExceptionHandling
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Load the zero-shot classification model
|
| 14 |
+
classifier = pipeline(
|
| 15 |
+
"zero-shot-classification",
|
| 16 |
+
model="MoritzLaurer/ModernBERT-large-zeroshot-v2.0",
|
| 17 |
+
torch_dtype=torch.bfloat16,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def ZeroShotTextClassification(
|
| 22 |
+
text_input: str, candidate_labels: str, multi_label: bool
|
| 23 |
+
) -> Dict[str, float]:
|
| 24 |
+
"""
|
| 25 |
+
Performs zero-shot classification on the given text input.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
- text_input: The input text to classify.
|
| 29 |
+
- candidate_labels: A comma-separated string of candidate labels.
|
| 30 |
+
- multi_label: A boolean indicating whether to allow the model to choose multiple classes.
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
Dictionary containing label-score pairs.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
# Check if the input and candidate labels are valid
|
| 37 |
+
if not text_input or not candidate_labels:
|
| 38 |
+
gr.Warning("Please provide valid input and candidate labels")
|
| 39 |
+
|
| 40 |
+
# Split and clean the candidate labels
|
| 41 |
+
labels = [label.strip() for label in candidate_labels.split(",")]
|
| 42 |
+
|
| 43 |
+
# Log the classification attempt
|
| 44 |
+
logging.info(f"Attempting classification with {len(labels)} labels")
|
| 45 |
+
|
| 46 |
+
# Perform zero-shot classification
|
| 47 |
+
hypothesis_template = "This text is about {}"
|
| 48 |
+
prediction = classifier(
|
| 49 |
+
text_input,
|
| 50 |
+
labels,
|
| 51 |
+
hypothesis_template=hypothesis_template,
|
| 52 |
+
multi_label=multi_label,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Return the classification results
|
| 56 |
+
logging.info("Classification completed successfully")
|
| 57 |
+
return {
|
| 58 |
+
prediction["labels"][i]: prediction["scores"][i]
|
| 59 |
+
for i in range(len(prediction["labels"]))
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Handle exceptions that may occur during the process
|
| 63 |
+
except Exception as e:
|
| 64 |
+
# Custom exception handling
|
| 65 |
+
raise CustomExceptionHandling(e, sys) from e
|