Downloads · 30 days
13
9% of all-time downloads
Mitchins/longformer-fiction-genre-13g
longformer-fiction-genre-13g is a text classification model from Mitchins. Use it when you need a label for a piece of text. It is set up for transformers. The card lists the license as mit.
This model classifies narrative semantic genres in long-form fiction. Rather than predicting marketing categories or analyzing book descriptions, it identifies narrative modes by analyzing story structure, tone, dicti…
Downloads · 30 days
13
9% of all-time downloads
All-time downloads
151
Public
Parameters
149M
1.2 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors595 MB · 99%
From the Hugging Face model README
This model classifies narrative semantic genres in long-form fiction. Rather than predicting marketing categories or analyzing book descriptions, it identifies narrative modes by analyzing story structure, tone, diction, and thematic elements in actual fiction text.
The model is trained on story text (not blurbs or metadata) and uses a Longformer architecture to handle long contexts (up to 4096 tokens). It was trained with curriculum learning, progressively moving from short scenes to full chapters. For inference on complete books, sliding windows can be used to produce genre distributions across the text.
Key differences from typical genre classifiers:
allenai/longformer-base-4096The model predicts 13 semantic narrative genres representing literary modes rather than bookstore categories:
adventure, contemporary, crime, fantasy, historical, horror, literary, mystery, romance, science_fiction, thriller, war, western
Overall Accuracy: 66.92% (87/130 stories correct)
Evaluated on 130 stories (10 per genre x 13 genres) spanning literary, indie, and blockbuster writing styles.
| Genre | Accuracy |
|---|---|
| western | 100% (10/10) |
| historical | 90% (9/10) |
| mystery | 90% (9/10) |
| horror | 80% (8/10) |
| literary | 80% (8/10) |
| science_fiction | 80% (8/10) |
| war | 80% (8/10) |
| romance | 70% (7/10) |
| adventure | 50% (5/10) |
| fantasy | 50% (5/10) |
| contemporary | 40% (4/10) |
| crime | 30% (3/10) |
| thriller | 30% (3/10) |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_name = "Mitchins/longformer-fiction-genre-13g"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=4096)
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=-1)
predicted_class = torch.argmax(probs, dim=-1).item()
predicted_genre = model.config.id2label[predicted_class]
confidence = probs[0][predicted_class].item()
print(f"Genre: {predicted_genre} ({confidence:.2%} confidence)")
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from collections import Counter
def classify_book_windowed(text, window_size=3500, stride=1750):
tokenizer = AutoTokenizer.from_pretrained("Mitchins/longformer-fiction-genre-13g")
model = AutoModelForSequenceClassification.from_pretrained("Mitchins/longformer-fiction-genre-13g")
tokens = tokenizer.encode(text, add_special_tokens=False)
genre_votes = []
for i in range(0, len(tokens), stride):
window = tokens[i:i + window_size]
if len(window) < 100:
continue
inputs = {'input_ids': torch.tensor([window])}
outputs = model(**inputs)
pred = torch.argmax(outputs.logits, dim=-1).item()
genre_votes.append(model.config.id2label[pred])
if i + window_size >= len(tokens):
break
return Counter(genre_votes)
model = AutoModelForSequenceClassification.from_pretrained(
"Mitchins/longformer-fiction-genre-13g",
output_hidden_states=True
)
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=4096)
outputs = model(**inputs)
hidden_states = outputs.hidden_states[-1]
embedding = hidden_states.mean(dim=1)
Potential extensions include training specialized subgenre classifier heads or adapting to more recent architectures like DeBERTa.
@model{longformer_fiction_genre,
title={Longformer Fiction Genre Classifier},
author={Mitchell Currie},
year={2024},
publisher={HuggingFace},
howpublished={\url{https://huggingface.co/Mitchins/longformer-fiction-genre-13g}}
}
MIT License