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'" ], [ "Histoire de France au moyen aÃÇge, depuis Philippe-Auguste jusqu'aÃÄ la fin du reÃÄgne de Louis XI. 1223-1483. Troisieme eÃÅdition" ], [ "Two Centuries of Soho: its institutions, firms, and amusements. By the Clergy of St. Anne's, Soho, J. H. Cardwell ... H. B. Freeman ... G. C. Wilton ... assisted by other contributors, etc" ], ["""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/BritishLibraryLabs/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("BritishLibraryLabs/bl-books-genre") model = AutoModelForSequenceClassification.from_pretrained("BritishLibraryLabs/bl-books-genre") classifier = pipeline('text-classification',model=model, tokenizer=tokenizer, return_all_scores=True) def predict(text): predictions = classifier(text) return {pred['label']: pred['score'] for pred in predictions[0]} gr.Interface(predict, inputs=gr.inputs.Textbox(label="Book title"), outputs=gr.outputs.Label(label="Predicted genre"), interpretation='shap', num_shap=10, theme="peach", examples=sample_text,description=description,article=article, ).launch(enable_queue=True)