Emanuel commited on
Commit
5657400
0 Parent(s):

Initial commit

Browse files
Files changed (3) hide show
  1. .gitignore +138 -0
  2. app.py +64 -0
  3. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
98
+ __pypackages__/
99
+
100
+ # Celery stuff
101
+ celerybeat-schedule
102
+ celerybeat.pid
103
+
104
+ # SageMath parsed files
105
+ *.sage.py
106
+
107
+ # Environments
108
+ .env
109
+ .venv
110
+ env/
111
+ venv/
112
+ ENV/
113
+ env.bak/
114
+ venv.bak/
115
+
116
+ # Spyder project settings
117
+ .spyderproject
118
+ .spyproject
119
+
120
+ # Rope project settings
121
+ .ropeproject
122
+
123
+ # mkdocs documentation
124
+ /site
125
+
126
+ # mypy
127
+ .mypy_cache/
128
+ .dmypy.json
129
+ dmypy.json
130
+
131
+ # Pyre type checker
132
+ .pyre/
133
+
134
+ # pytype static type analyzer
135
+ .pytype/
136
+
137
+ # Cython debug symbols
138
+ cython_debug/
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple
2
+
3
+ import torch
4
+ import streamlit as st
5
+ from transformers import AutoModelForTokenClassification, AutoTokenizer
6
+ from dante_tokenizer import DanteTokenizer
7
+ from dante_tokenizer.data.preprocessing import expand_contractions
8
+ from annotated_text import annotated_text
9
+
10
+
11
+ def get_pos_tag_model(model_name: str = "Emanuel/autonlp-pos-tag-bosque") -> Tuple[AutoModelForTokenClassification, AutoTokenizer]:
12
+ model = AutoModelForTokenClassification.from_pretrained(model_name)
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+
15
+ return model, tokenizer
16
+
17
+ def get_tag_color(tag: str) -> str:
18
+ """
19
+ Return the color for a given part-of-speech tag from the Universal Dependencies tagset.
20
+ See: https://universaldependencies.org/u/pos/
21
+ """
22
+ pallete = {
23
+ "ADJ": "#2E4C6D",
24
+ "ADP": "#FBE7C6",
25
+ "ADV": "#DADDFC",
26
+ "AUX": "#FC997C",
27
+ "CCONJ": "#544179",
28
+ "DET": "#A0E7E5",
29
+ "INTJ": "#32C1CD",
30
+ "NOUN": "#17D7A0",
31
+ "PART": "#C85C5C",
32
+ "PRON": "#F9975D",
33
+ "PROPN": "#FBD148",
34
+ "PUNCT": "#B2EA70",
35
+ "SCONJ": "#AA14F0",
36
+ "SYM": "#34BE82",
37
+ "VERB": "#FFBF86",
38
+ "X": "#2F86A6",
39
+ }
40
+ return pallete[tag]
41
+
42
+ def main():
43
+ text = st.text_area("Digite seu texto de entrada!")
44
+ dt = DanteTokenizer()
45
+ model, tokenizer = get_pos_tag_model()
46
+
47
+ if text:
48
+ tokens = dt.tokenize(text)
49
+ input_cleaned_text = expand_contractions(text)
50
+ inputs = tokenizer(text, return_tensors="pt")
51
+ outputs = model(**inputs)
52
+ labelids = outputs.logits.squeeze().argmax(axis=-1)
53
+ scores, _ = torch.nn.functional.softmax(outputs.logits, dim=1).squeeze().max(axis=-1)
54
+ scores = scores.tolist()
55
+ labels = [model.config.id2label[int(x)] for x in labelids]
56
+ labels = labels[1:-1]
57
+
58
+ answer = []
59
+ for token, label, score in zip(tokens, labels, scores):
60
+ answer.append((token, label, get_tag_color(label)))
61
+ annotated_text(*answer)
62
+
63
+ if __name__ == "__main__":
64
+ main()
requirements.txt ADDED
Binary file (4.23 kB). View file