oracat commited on
Commit
429523a
β€’
1 Parent(s): 374a9ea

Initial commit

Browse files
Files changed (3) hide show
  1. README.md +2 -3
  2. app.py +108 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
  title: PaperClassifierArxiv
3
- emoji: 🏒
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: streamlit
7
- sdk_version: 1.17.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: PaperClassifierArxiv
3
+ emoji: πŸ“
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: streamlit
7
+ sdk_version: 1.20.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
  ---
12
 
 
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
2
+ import streamlit as st
3
+
4
+
5
+ @st.cache_data
6
+ def prepare_model():
7
+ """
8
+ Prepare the tokenizer and the model for classification.
9
+ """
10
+ tokenizer = AutoTokenizer.from_pretrained("oracat/bert-paper-classifier-arxiv")
11
+ model = AutoModelForSequenceClassification.from_pretrained(
12
+ "oracat/bert-paper-classifier-arxiv"
13
+ )
14
+ return (tokenizer, model)
15
+
16
+
17
+ def process(text):
18
+ """
19
+ Translate incoming text to tokens and classify it
20
+ """
21
+ pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
22
+ result = pipe(text)[0]
23
+ return result["label"]
24
+
25
+
26
+ tokenizer, model = prepare_model()
27
+
28
+
29
+ # State managements
30
+ #
31
+ # The state in the app is the title and the abstract.
32
+ # State management is used here in order to pre-fill
33
+ # input fields with values for demos.
34
+
35
+ if "title" not in st.session_state:
36
+ st.session_state["title"] = ""
37
+
38
+ if "abstract" not in st.session_state:
39
+ st.session_state["abstract"] = ""
40
+
41
+ if "output" not in st.session_state:
42
+ st.session_state["output"] = ""
43
+
44
+
45
+ # Simple streamlit interface
46
+
47
+ st.markdown("### Hello, paper classifier!")
48
+
49
+
50
+ ## Demo buttons and their callbacks
51
+
52
+
53
+ def demo_cl_callback():
54
+ """
55
+ Use https://ai.facebook.com/blog/large-language-model-llama-meta-ai/ for demo
56
+ """
57
+ paper_title = (
58
+ "Introducing LLaMA: A foundational, 65-billion-parameter large language model"
59
+ )
60
+ paper_abstract = "Over the last year, large language models β€” natural language processing (NLP) systems with billions of parameters β€” have shown new capabilities to generate creative text, solve mathematical theorems, predict protein structures, answer reading comprehension questions, and more. They are one of the clearest cases of the substantial potential benefits AI can offer at scale to billions of people. Smaller models trained on more tokens β€” which are pieces of words β€” are easier to retrain and fine-tune for specific potential product use cases. We trained LLaMA 65B and LLaMA 33B on 1.4 trillion tokens. Our smallest model, LLaMA 7B, is trained on one trillion tokens. Like other large language models, LLaMA works by taking a sequence of words as an input and predicts a next word to recursively generate text. To train our model, we chose text from the 20 languages with the most speakers, focusing on those with Latin and Cyrillic alphabets."
61
+ st.session_state["title"] = paper_title
62
+ st.session_state["abstract"] = paper_abstract
63
+
64
+
65
+ def demo_cv_callback():
66
+ """
67
+ Use https://arxiv.org/abs/2010.11929 for demo
68
+ """
69
+ paper_title = (
70
+ "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale"
71
+ )
72
+ paper_abstract = "While the Transformer architecture has become the de-facto standard for natural language processing tasks, its applications to computer vision remain limited. In vision, attention is either applied in conjunction with convolutional networks, or used to replace certain components of convolutional networks while keeping their overall structure in place. We show that this reliance on CNNs is not necessary and a pure transformer applied directly to sequences of image patches can perform very well on image classification tasks. When pre-trained on large amounts of data and transferred to multiple mid-sized or small image recognition benchmarks (ImageNet, CIFAR-100, VTAB, etc.), Vision Transformer (ViT) attains excellent results compared to state-of-the-art convolutional networks while requiring substantially fewer computational resources to train."
73
+ st.session_state["title"] = paper_title
74
+ st.session_state["abstract"] = paper_abstract
75
+
76
+
77
+ def clear_callback():
78
+ """
79
+ Clear input fields
80
+ """
81
+ st.session_state["title"] = ""
82
+ st.session_state["abstract"] = ""
83
+ st.session_state["output"] = ""
84
+
85
+
86
+ col1, col2, col3 = st.columns([1, 1, 1])
87
+ with col1:
88
+ st.button("Demo: LLaMA paper", on_click=demo_cl_callback)
89
+ with col2:
90
+ st.button("Demo: ViT paper", on_click=demo_cv_callback)
91
+ with col3:
92
+ st.button("Clear fields", on_click=clear_callback)
93
+
94
+ ## Input fields
95
+
96
+ placeholder = st.empty()
97
+
98
+ title = st.text_input("Enter the title:", key="title")
99
+ abstract = st.text_area(
100
+ "... and maybe the abstract of the paper you want to classify:", key="abstract"
101
+ )
102
+
103
+ text = "\n".join([title, abstract])
104
+
105
+ ## Output
106
+
107
+ if len(text.strip()) > 0:
108
+ st.markdown(f"<h4>Predicted class: {process(text)}</h4>", unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch