import gradio as gr from transformers import pipeline from transformers import AutoTokenizer, AutoModelForSequenceClassification sample_text = [ [ "Poems on various subjects. Whereto is prefixed a short essay on the structure of English verse" ], [ "Journal of a Residence in China and the neighbouring countries from 1830 to 1833. With an introductory essay by the Hon. and Rev. Baptist Wriothesley Noel. [With a map.]" ], ["The Adventures of Oliver Twist. [With plates.]"], ["['The Adventures of Sherlock Holmes', 'Single Works']"], [ "['Coal, Iron, and Oil; or, the Practical American miner. A plain and popular work on our mines and mineral resources ... With numerous maps and engravings, etc']" ], [ "Summer Travelling in Iceland; being the narrative of two journeys across the island ... With a chapter on Askja by E. Delmar Morgan ... Containing also a literal translation of three sagas. Maps, etc'" ], [ "History of the Monument. With a brief account of the Great Fire of London, which it commemorates. By Charles Welch. (With illustrations and a map of Old London.)", ], ["The history and antiquities of Newbury and its environs [By E. W. Gray.]"], ["""A Christmas Carol"""], ] description = """ V2 of a British Library Books genre detection model. The interpretation interface helps show what words the model is using to make its predictions. Words highlighted in red contributed to the model being more confident about a prediction. The intensity of colour corresponds to the importance of that part of the input. The words that decrease the label confidence are highlighted in blue.""" article = """

British Library Books genre detection demo

This demo allows you to play with a 'genre' detection model which has been trained to predict, from the title of a book, whether it is 'fiction' or 'non-fiction'. The [model](https://huggingface.co/TheBritishLibrary/bl-books-genre) was trained on training data drawn from [digitised books](https://www.bl.uk/collection-guides/digitised-printed-books) at the British Library. These Books are mainly from the 19th Century. The demo also shows you which parts of the input the model is using most to make its prediction. The examples include titles from the BL books collection. You may notice that the model makes mistakes on short titles in particular, this can partly be explained by the title format in the original data. For example the novel *'Vanity Fair'* by William Makepeace Thackeray is found in the training data as: ``` Vanity Fair. A novel without a hero ... With all the original illustrations by the author, etc ``` You can see that the model gets a bit of help with the genre here 😉. Since the model was trained for a very particular dataset and task it might not work well on titles that don't match this original corpus. ## Background This model was developed as part of work by the [Living with Machines](https://livingwithmachines.ac.uk/). The process of training the model and working with the data is documented in a [tutorial](github.com/living-with-machines/genre-classification). ## Model description This model is intended to predict, from the title of a book, whether it is 'fiction' or 'non-fiction'. This model was trained on data created from the [Digitised printed books (18th-19th Century)](https://www.bl.uk/collection-guides/digitised-printed-books) book collection. This dataset is dominated by English language books though it includes books in several other languages in much smaller numbers. You can find more information about the model [here](https://huggingface.co/BritishLibraryLabs/bl-books-genre) ## Training data The model is trained on a particular collection of books digitised by the British Library. As a result, the model may do less well on titles that look different to this data. In particular, the training data, was mostly English, and mostly from the 19th Century. The model is likely to do less well with non-English languages and book titles which fall outside of the 19th Century. Since the data was derived from books catalogued by the British Library it is also possible the model will perform less well for books held by other institutions if, for example, they catalogue book titles in different ways, or have different biases in the types of books they hold. Some of the data was generated using weak supervision. You can learn more about how this was done [here](https://living-with-machines.github.io/genre-classification/04_snorkel.html) ### Credits >This work was partly supported by [Living with Machines](https://livingwithmachines.ac.uk/). This project, funded by the UK Research and Innovation (UKRI) Strategic Priority Fund, is a multidisciplinary collaboration delivered by the Arts and Humanities Research Council (AHRC), with The Alan Turing Institute, the British Library and the Universities of Cambridge, East Anglia, Exeter, and Queen Mary University of London. """ tokenizer = AutoTokenizer.from_pretrained("TheBritishLibrary/bl-books-genre") model = AutoModelForSequenceClassification.from_pretrained( "TheBritishLibrary/bl-books-genre" ) classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=10) def predict(text): predictions = classifier(text) return {pred["label"]: float(pred["score"]) for pred in predictions} gr.Interface( predict, inputs=gr.Textbox(label="Book title"), outputs="label", interpretation="shap", num_shap=10.0, theme="huggingface", examples=sample_text, description=description, article=article, ).launch(enable_queue=True)