Spaces:
Sleeping
Sleeping
Add BertGram model
Browse files- .gitignore +0 -1
- extract.py +5 -5
- files/js/detection.js +4 -0
- files/js/grammar.js +4 -0
- files/js/summarizer.js +4 -0
- inference.py +11 -6
- main.py +3 -3
- models/mbert-gram/config.json +32 -0
- models/mbert-gram/pytorch_model.bin +3 -0
- models/mbert-gram/special_tokens_map.json +7 -0
- models/mbert-gram/tokenizer_config.json +15 -0
- models/mbert-gram/vocab.txt +0 -0
.gitignore
CHANGED
@@ -1,2 +1 @@
|
|
1 |
__pycache__/
|
2 |
-
models/mbert-gram/
|
|
|
1 |
__pycache__/
|
|
extract.py
CHANGED
@@ -55,8 +55,8 @@ def get_pagerank(importance, top_k):
|
|
55 |
|
56 |
def summarize_text(text: str) -> str:
|
57 |
return 'ΡΡΡ'
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
55 |
|
56 |
def summarize_text(text: str) -> str:
|
57 |
return 'ΡΡΡ'
|
58 |
+
sentences = sent_tokenize(text)[:2000]
|
59 |
+
top_k = max(20, int(len(sentences) ** .5))
|
60 |
+
mat = build_matrix(sentences)
|
61 |
+
idx = get_pagerank(mat, top_k)
|
62 |
+
return '\n'.join(sentences[i] for i in idx)
|
files/js/detection.js
CHANGED
@@ -119,6 +119,8 @@ function _show_summary() {
|
|
119 |
|
120 |
summary.classList.remove('hidden');
|
121 |
original.classList.add('hidden');
|
|
|
|
|
122 |
}
|
123 |
|
124 |
function _show_original() {
|
@@ -127,6 +129,8 @@ function _show_original() {
|
|
127 |
|
128 |
original.classList.remove('hidden');
|
129 |
summary.classList.add('hidden');
|
|
|
|
|
130 |
}
|
131 |
|
132 |
document.addEventListener('DOMContentLoaded', function () {
|
|
|
119 |
|
120 |
summary.classList.remove('hidden');
|
121 |
original.classList.add('hidden');
|
122 |
+
|
123 |
+
summaryText.focus({ preventScroll: true });
|
124 |
}
|
125 |
|
126 |
function _show_original() {
|
|
|
129 |
|
130 |
original.classList.remove('hidden');
|
131 |
summary.classList.add('hidden');
|
132 |
+
|
133 |
+
sumTextInput.focus({ preventScroll: true });
|
134 |
}
|
135 |
|
136 |
document.addEventListener('DOMContentLoaded', function () {
|
files/js/grammar.js
CHANGED
@@ -119,6 +119,8 @@ function _show_summary() {
|
|
119 |
|
120 |
summary.classList.remove('hidden');
|
121 |
original.classList.add('hidden');
|
|
|
|
|
122 |
}
|
123 |
|
124 |
function _show_original() {
|
@@ -127,6 +129,8 @@ function _show_original() {
|
|
127 |
|
128 |
original.classList.remove('hidden');
|
129 |
summary.classList.add('hidden');
|
|
|
|
|
130 |
}
|
131 |
|
132 |
document.addEventListener('DOMContentLoaded', function () {
|
|
|
119 |
|
120 |
summary.classList.remove('hidden');
|
121 |
original.classList.add('hidden');
|
122 |
+
|
123 |
+
summaryText.focus({ preventScroll: true });
|
124 |
}
|
125 |
|
126 |
function _show_original() {
|
|
|
129 |
|
130 |
original.classList.remove('hidden');
|
131 |
summary.classList.add('hidden');
|
132 |
+
|
133 |
+
sumTextInput.focus({ preventScroll: true });
|
134 |
}
|
135 |
|
136 |
document.addEventListener('DOMContentLoaded', function () {
|
files/js/summarizer.js
CHANGED
@@ -164,6 +164,8 @@ function _show_summary() {
|
|
164 |
|
165 |
summary.classList.remove('hidden');
|
166 |
original.classList.add('hidden');
|
|
|
|
|
167 |
}
|
168 |
|
169 |
function _show_original() {
|
@@ -172,6 +174,8 @@ function _show_original() {
|
|
172 |
|
173 |
original.classList.remove('hidden');
|
174 |
summary.classList.add('hidden');
|
|
|
|
|
175 |
}
|
176 |
|
177 |
|
|
|
164 |
|
165 |
summary.classList.remove('hidden');
|
166 |
original.classList.add('hidden');
|
167 |
+
|
168 |
+
summaryText.focus({ preventScroll: true });
|
169 |
}
|
170 |
|
171 |
function _show_original() {
|
|
|
174 |
|
175 |
original.classList.remove('hidden');
|
176 |
summary.classList.add('hidden');
|
177 |
+
|
178 |
+
sumTextInput.focus({ preventScroll: true });
|
179 |
}
|
180 |
|
181 |
|
inference.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import torch
|
2 |
from transformers import BertForSequenceClassification, AutoTokenizer
|
3 |
|
4 |
-
|
5 |
|
6 |
# path_emo = 'Djacon/rubert-tiny2-russian-emotion-detection'
|
7 |
path_emo = './models/emotion_detection/'
|
@@ -27,12 +27,17 @@ def predict_emotions(text: str) -> str:
|
|
27 |
return '\n'.join(f"{k}: {v}%" for k, v in sorted(emotions_list.items(),
|
28 |
key=lambda x: -x[1]))
|
29 |
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
|
38 |
def test():
|
@@ -42,7 +47,7 @@ def test():
|
|
42 |
predict_summarization('I am so happy now!')
|
43 |
print('>>> Extractive Summarizer successfully initialized! <<<')
|
44 |
|
45 |
-
|
46 |
print('>>> Grammar Checker successfully initialized! <<<\n')
|
47 |
|
48 |
|
|
|
1 |
import torch
|
2 |
from transformers import BertForSequenceClassification, AutoTokenizer
|
3 |
|
4 |
+
from extract import summarize_text as predict_summarization
|
5 |
|
6 |
# path_emo = 'Djacon/rubert-tiny2-russian-emotion-detection'
|
7 |
path_emo = './models/emotion_detection/'
|
|
|
27 |
return '\n'.join(f"{k}: {v}%" for k, v in sorted(emotions_list.items(),
|
28 |
key=lambda x: -x[1]))
|
29 |
|
30 |
+
path_gram = './models/mbert-gram/'
|
31 |
+
model_gram = BertForSequenceClassification.from_pretrained(path_gram)
|
32 |
+
tokenizer_gram = AutoTokenizer.from_pretrained(path_gram)
|
33 |
|
34 |
|
35 |
+
@torch.no_grad()
|
36 |
+
def predict_acceptance(text: str) -> str:
|
37 |
+
inputs = tokenizer_gram(text, truncation=True, return_tensors='pt')
|
38 |
+
output = model_gram(**inputs).logits[0]
|
39 |
+
pred = torch.nn.functional.softmax(output, dim=0)[1]
|
40 |
+
return f'Acceptance: {100 * pred.item():.2f}%'
|
41 |
|
42 |
|
43 |
def test():
|
|
|
47 |
predict_summarization('I am so happy now!')
|
48 |
print('>>> Extractive Summarizer successfully initialized! <<<')
|
49 |
|
50 |
+
predict_acceptance('I am so happy now!')
|
51 |
print('>>> Grammar Checker successfully initialized! <<<\n')
|
52 |
|
53 |
|
main.py
CHANGED
@@ -6,11 +6,11 @@ from fastapi.staticfiles import StaticFiles
|
|
6 |
from fastapi.templating import Jinja2Templates
|
7 |
|
8 |
from youtube import get_youtube_caption
|
9 |
-
from inference import predict_emotions, predict_summarization,
|
10 |
|
11 |
MAX_ITER_SIZE = 3000
|
12 |
|
13 |
-
app = FastAPI()
|
14 |
app.mount("/files", StaticFiles(directory="files"), name="files")
|
15 |
templates = Jinja2Templates(directory="static")
|
16 |
|
@@ -61,4 +61,4 @@ async def predict_sum(request: EmotionRequest):
|
|
61 |
|
62 |
@app.post('/predict_grammar')
|
63 |
async def predict_gram(request: EmotionRequest):
|
64 |
-
return
|
|
|
6 |
from fastapi.templating import Jinja2Templates
|
7 |
|
8 |
from youtube import get_youtube_caption
|
9 |
+
from inference import predict_emotions, predict_summarization, predict_acceptance
|
10 |
|
11 |
MAX_ITER_SIZE = 3000
|
12 |
|
13 |
+
app = FastAPI(docs_url=None, redoc_url=None)
|
14 |
app.mount("/files", StaticFiles(directory="files"), name="files")
|
15 |
templates = Jinja2Templates(directory="static")
|
16 |
|
|
|
61 |
|
62 |
@app.post('/predict_grammar')
|
63 |
async def predict_gram(request: EmotionRequest):
|
64 |
+
return predict_acceptance(request.text)
|
models/mbert-gram/config.json
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "bert-base-multilingual-cased",
|
3 |
+
"architectures": [
|
4 |
+
"BertForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"classifier_dropout": null,
|
8 |
+
"directionality": "bidi",
|
9 |
+
"hidden_act": "gelu",
|
10 |
+
"hidden_dropout_prob": 0.1,
|
11 |
+
"hidden_size": 768,
|
12 |
+
"initializer_range": 0.02,
|
13 |
+
"intermediate_size": 3072,
|
14 |
+
"layer_norm_eps": 1e-12,
|
15 |
+
"max_position_embeddings": 512,
|
16 |
+
"model_type": "bert",
|
17 |
+
"num_attention_heads": 12,
|
18 |
+
"num_hidden_layers": 12,
|
19 |
+
"pad_token_id": 0,
|
20 |
+
"pooler_fc_size": 768,
|
21 |
+
"pooler_num_attention_heads": 12,
|
22 |
+
"pooler_num_fc_layers": 3,
|
23 |
+
"pooler_size_per_head": 128,
|
24 |
+
"pooler_type": "first_token_transform",
|
25 |
+
"position_embedding_type": "absolute",
|
26 |
+
"problem_type": "single_label_classification",
|
27 |
+
"torch_dtype": "float32",
|
28 |
+
"transformers_version": "4.27.3",
|
29 |
+
"type_vocab_size": 2,
|
30 |
+
"use_cache": true,
|
31 |
+
"vocab_size": 119547
|
32 |
+
}
|
models/mbert-gram/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9c99166de9b94e2c1da20c11d2f7f774d7b7b12f9a0d81f31f76514defe6747f
|
3 |
+
size 711492725
|
models/mbert-gram/special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
models/mbert-gram/tokenizer_config.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"do_basic_tokenize": true,
|
4 |
+
"do_lower_case": false,
|
5 |
+
"mask_token": "[MASK]",
|
6 |
+
"model_max_length": 512,
|
7 |
+
"never_split": null,
|
8 |
+
"pad_token": "[PAD]",
|
9 |
+
"sep_token": "[SEP]",
|
10 |
+
"special_tokens_map_file": null,
|
11 |
+
"strip_accents": null,
|
12 |
+
"tokenize_chinese_chars": true,
|
13 |
+
"tokenizer_class": "BertTokenizer",
|
14 |
+
"unk_token": "[UNK]"
|
15 |
+
}
|
models/mbert-gram/vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|