Commit
·
6f30057
1
Parent(s):
886bdd9
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
|
6 |
+
sample_text = [
|
7 |
+
[
|
8 |
+
"Poems on various subjects. Whereto is prefixed a short essay on the structure of English verse"
|
9 |
+
],
|
10 |
+
[
|
11 |
+
"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.]"
|
12 |
+
],
|
13 |
+
["The Adventures of Oliver Twist. [With plates.]"],
|
14 |
+
["['The Adventures of Sherlock Holmes', 'Single Works']"],
|
15 |
+
[
|
16 |
+
"['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']"
|
17 |
+
],
|
18 |
+
[
|
19 |
+
"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'"
|
20 |
+
],
|
21 |
+
[
|
22 |
+
"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"
|
23 |
+
],
|
24 |
+
[
|
25 |
+
"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"
|
26 |
+
],
|
27 |
+
["""A Christmas Carol"""],
|
28 |
+
]
|
29 |
+
|
30 |
+
description = """
|
31 |
+
British Library Books genre detection model
|
32 |
+
"""
|
33 |
+
|
34 |
+
tokenizer = AutoTokenizer.from_pretrained("BritishLibraryLabs/bl-books-genre")
|
35 |
+
|
36 |
+
model = AutoModelForSequenceClassification.from_pretrained("BritishLibraryLabs/bl-books-genre")
|
37 |
+
classifier = pipeline('text-classification',model=model, tokenizer=tokenizer, return_all_scores=True)
|
38 |
+
|
39 |
+
|
40 |
+
def predict(text):
|
41 |
+
predictions = classifier(text)
|
42 |
+
return {pred['label']: pred['score'] for pred in predictions[0]}
|
43 |
+
|
44 |
+
gr.Interface(predict,
|
45 |
+
inputs=gr.inputs.Textbox(label="Book title"),
|
46 |
+
outputs=gr.outputs.Label(label="Predicted genre"),
|
47 |
+
interpretation='shap',
|
48 |
+
examples=sample_text,description=description,
|
49 |
+
).launch(enable_queue=True)
|
50 |
+
|