Edit model card

Fine-Tuned Topic Classification Model - SK_Morph_BLM (Topic News)

Model Overview

This model is a fine-tuned version of the SK_Morph_BLM model for topic classification. For this task, we used the Slovak Categorized News Corpus, which contains news articles divided into six categories: Economy and Business, Culture, News, World, Sports, and Healthcare. The corpus provides text files with detailed annotations, including token and sentence boundary identification, stop words, morphological analysis, named entity recognition, and lemmatization.

Topic Labels

Each record in the dataset is labeled with one of the following topics:

  • Healthcare (0): 2,564 records
  • News (1): 4,174 records
  • Sports (2): 2,759 records
  • World (3): 1,660 records
  • Economy and Business (4): 4,199 records
  • Culture (5): 137 records

Dataset Details

The original corpus did not contain continuous text, requiring significant preprocessing. The process involved:

  1. Reconstruction: We reconstructed coherent text from individual annotated files, resulting in over 86,000 sentences.
  2. Combining Sentences: Sentences from each file were combined into single records, with a maximum length of 600 characters (approximately 200 tokens). The final dataset comprises a total of 15,493 records, each labeled according to the categories listed above.

For more information about the dataset, please visit this link.

Fine-Tuning Hyperparameters

The following hyperparameters were used during the fine-tuning process:

  • Learning Rate: 1e-05
  • Training Batch Size: 64
  • Evaluation Batch Size: 64
  • Seed: 42
  • Optimizer: Adam (default)
  • Number of Epochs: 10

Model Performance

The model was evaluated using stratified 10-fold cross-validation, achieving a weighted F1-score with a median value of 0.968 across the folds.

Model Usage

This model is suitable for topic classification in Slovak text, particularly for news articles across various categories. It is specifically designed for applications requiring topic categorization of news content and may not generalize well to other types of text.

Example Usage

Below is an example of how to use the fine-tuned SK_Morph_BLM-topic-news model in a Python script:

import torch
from transformers import RobertaForSequenceClassification, RobertaTokenizerFast
from huggingface_hub import snapshot_download

class TopicClassifier:
    def __init__(self, tokenizer, model):
        self.model = RobertaForSequenceClassification.from_pretrained(model, num_labels=6)
        
        repo_path = snapshot_download(repo_id = tokenizer)
        sys.path.append(repo_path)

        # Import the custom tokenizer from the downloaded repository
        from SKMT_lib_v2.SKMT_BPE import SKMorfoTokenizer
        self.tokenizer = SKMorfoTokenizer()

    def tokenize_text(self, text):
        encoded_text = self.tokenizer.tokenize(text.lower(), max_length=256, return_tensors='pt', return_subword=False)
        return encoded_text

    def classify_text(self, encoded_text):
        with torch.no_grad():
            output = self.model(**encoded_text)
            logits = output.logits
            predicted_class = torch.argmax(logits, dim=1).item()
            probabilities = torch.softmax(logits, dim=1)
            class_probabilities = probabilities[0].tolist()
            predicted_class_text = self.model.config.id2label[predicted_class]
        return predicted_class, predicted_class_text, class_probabilities

# Instantiate the topic classifier with the specified tokenizer and model
classifier = TopicClassifier(tokenizer="daviddrzik/SK_Morph_BLM", model="daviddrzik/SK_Morph_BLM-topic-news")

# Example text to classify topic
text_to_classify = "Tento dôležitý zápas medzi Českou republikou a Švajčiarskom sa po troch tretinách skončil 2:0."
print("Text to classify: " + text_to_classify + "\n")

# Tokenize the input text
encoded_text = classifier.tokenize_text(text_to_classify)

# Classify the topic of the tokenized text
predicted_class, predicted_class_text, logits = classifier.classify_text(encoded_text)

# Print the predicted class label and index
print(f"Predicted class: {predicted_class_text} ({predicted_class})")
# Print the probabilities for each class
print(f"Class probabilities: {logits}")

Example Output Here is the output when running the above example:

Text to classify: Tento dôležitý zápas medzi Českou republikou a Švajčiarskom sa po troch tretinách skončil 2:0.

Predicted class: Sport (2)
Class probabilities: [0.00015074180555529892, 0.000343791936757043, 0.9958429932594299, 0.0015455043176189065, 0.0013796273851767182, 0.000737304100766778]
Downloads last month
2
Safetensors
Model size
58.7M params
Tensor type
F32
·
Inference Examples
This model does not have enough activity to be deployed to Inference API (serverless) yet. Increase its social visibility and check back later, or deploy to Inference Endpoints (dedicated) instead.

Model tree for daviddrzik/SK_Morph_BLM-topic-news

Finetuned
(7)
this model