Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
|
4 |
+
from streamlit_extras.app_logo import add_logo
|
5 |
+
|
6 |
+
|
7 |
+
def logo():
|
8 |
+
add_logo("vocali_logo.jpeg", height=300)
|
9 |
+
|
10 |
+
|
11 |
+
def get_result_text_es_pt (list_entity, text, lang):
|
12 |
+
result_words = []
|
13 |
+
if lang == "es":
|
14 |
+
punc_tags = ['¿', '?', '¡', '!', ',', '.', ':']
|
15 |
+
else:
|
16 |
+
punc_tags = ['?', '!', ',', '.', ':']
|
17 |
+
|
18 |
+
for entity in list_entity:
|
19 |
+
tag = entity["entity"]
|
20 |
+
word = entity["word"]
|
21 |
+
start = entity["start"]
|
22 |
+
end = entity["end"]
|
23 |
+
|
24 |
+
# check punctuation
|
25 |
+
punc_in = next((p for p in punc_tags if p in tag), "")
|
26 |
+
|
27 |
+
subword = False
|
28 |
+
# check subwords
|
29 |
+
if word[0] == "#":
|
30 |
+
subword = True
|
31 |
+
if punc_in != "":
|
32 |
+
word = result_words[-1].replace(punc_in, "") + text[start:end]
|
33 |
+
else:
|
34 |
+
word = result_words[-1] + text[start:end]
|
35 |
+
|
36 |
+
if tag == "l":
|
37 |
+
word = word
|
38 |
+
elif tag == "u":
|
39 |
+
word = word.capitalize()
|
40 |
+
# case with punctuation
|
41 |
+
else:
|
42 |
+
if tag[-1] == "l":
|
43 |
+
word = (punc_in + word) if punc_in in ["¿", "¡"] else (word + punc_in)
|
44 |
+
elif tag[-1] == "u":
|
45 |
+
word = (punc_in + word.capitalize()) if punc_in in ["¿", "¡"] else (word.capitalize() + punc_in)
|
46 |
+
|
47 |
+
if tag != "l":
|
48 |
+
word = '<span style="font-weight:bold; color:rgb(142, 208, 129);">' + word + '</span>'
|
49 |
+
|
50 |
+
if subword == True:
|
51 |
+
result_words[-1] = word
|
52 |
+
else:
|
53 |
+
result_words.append(word)
|
54 |
+
|
55 |
+
return " ".join(result_words)
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
def get_result_text_ca (list_entity, text):
|
60 |
+
result_words = []
|
61 |
+
punc_tags = ['?', '!', ',', '.', ':']
|
62 |
+
|
63 |
+
for entity in list_entity:
|
64 |
+
start = entity["start"]
|
65 |
+
end = entity["end"]
|
66 |
+
tag = entity["entity"]
|
67 |
+
word = entity["word"]
|
68 |
+
|
69 |
+
# check punctuation
|
70 |
+
punc_in = next((p for p in punc_tags if p in tag), "")
|
71 |
+
|
72 |
+
subword = False
|
73 |
+
# check subwords
|
74 |
+
if word[0] != "Ġ":
|
75 |
+
subword = True
|
76 |
+
if punc_in != "":
|
77 |
+
word = result_words[-1].replace(punc_in, "") + text[start:end]
|
78 |
+
else:
|
79 |
+
word = result_words[-1] + text[start:end]
|
80 |
+
else:
|
81 |
+
word = text[start:end]
|
82 |
+
|
83 |
+
if tag == "l":
|
84 |
+
word = word
|
85 |
+
elif tag == "u":
|
86 |
+
word = word.capitalize()
|
87 |
+
# case with punctuation
|
88 |
+
else:
|
89 |
+
if tag[-1] == "l":
|
90 |
+
word = (punc_in + word) if punc_in in ["¿", "¡"] else (word + punc_in)
|
91 |
+
elif tag[-1] == "u":
|
92 |
+
word = (punc_in + word.capitalize()) if punc_in in ["¿", "¡"] else (word.capitalize() + punc_in)
|
93 |
+
|
94 |
+
if tag != "l":
|
95 |
+
word = '<span style="font-weight:bold; color:rgb(142, 208, 129);">' + word + '</span>'
|
96 |
+
|
97 |
+
if subword == True:
|
98 |
+
result_words[-1] = word
|
99 |
+
else:
|
100 |
+
result_words.append(word)
|
101 |
+
|
102 |
+
return " ".join(result_words)
|
103 |
+
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
logo()
|
107 |
+
st.title('Sanivert Punctuation And Capitalization Restoration')
|
108 |
+
|
109 |
+
model_es = AutoModelForTokenClassification.from_pretrained("VOCALINLP/spanish_capitalization_punctuation_restoration_sanivert")
|
110 |
+
tokenizer_es = AutoTokenizer.from_pretrained("VOCALINLP/spanish_capitalization_punctuation_restoration_sanivert")
|
111 |
+
pipe_es = pipeline("token-classification", model=model_es, tokenizer=tokenizer_es)
|
112 |
+
|
113 |
+
model_ca = ModelForTokenClassification.from_pretrained("VOCALINLP/catalan_capitalization_punctuation_restoration_sanivert")
|
114 |
+
tokenizer_ca = AutoTokenizer.from_pretrained("VOCALINLP/catalan_capitalization_punctuation_restoration_sanivert")
|
115 |
+
pipe_ca = pipeline("token-classification", model=model_ca, tokenizer=tokenizer_ca)
|
116 |
+
|
117 |
+
model_pt = AutoModelForTokenClassification.from_pretrained("VOCALINLP/portuguese_capitalization_punctuation_restoration_sanivert")
|
118 |
+
tokenizer_pt = AutoTokenizer.from_pretrained("VOCALINLP/portuguese_capitalization_punctuation_restoration_sanivert")
|
119 |
+
pipe_pt = pipeline("token-classification", model=model_ca, tokenizer=tokenizer_ca)
|
120 |
+
|
121 |
+
input_text = st.selectbox(
|
122 |
+
label = "Choose an language",
|
123 |
+
options = ["Spanish", "Portuguese", "Catalan"]
|
124 |
+
)
|
125 |
+
|
126 |
+
st.subheader("Enter the text to be analyzed.")
|
127 |
+
text = st.text_input('Enter text') #text is stored in this variable
|
128 |
+
if input_text == "Spanish":
|
129 |
+
result_pipe = pipe_es(text)
|
130 |
+
out = get_result_text_es_pt(result_pipe, text, "es")
|
131 |
+
elif input_text == "Portuguese":
|
132 |
+
result_pipe = pipe_pt(text)
|
133 |
+
out = get_result_text_es_pt(result_pipe, text, "pt")
|
134 |
+
elif input_text == "Catalan":
|
135 |
+
result_pipe = pipe_ca(text)
|
136 |
+
out = get_result_text_ca(result_pipe, text)
|
137 |
+
|
138 |
+
out = get_prediction(text, input_text)
|
139 |
+
st.markdown(out, unsafe_allow_html=True)
|
140 |
+
text = ""
|
141 |
+
|
142 |
+
|
143 |
+
|