First commit
Browse files- app.py +149 -0
- images/GLogo.png +0 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from annotated_text import annotated_text
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
from gramformer import Gramformer
|
4 |
+
import streamlit as st
|
5 |
+
import pandas as pd
|
6 |
+
import torch
|
7 |
+
import math
|
8 |
+
import re
|
9 |
+
|
10 |
+
def set_seed(seed):
|
11 |
+
torch.manual_seed(seed)
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
torch.cuda.manual_seed_all(seed)
|
14 |
+
set_seed(1212)
|
15 |
+
|
16 |
+
class GramformerDemo:
|
17 |
+
|
18 |
+
def __init__(self):
|
19 |
+
st.set_page_config(
|
20 |
+
page_title="Gramformer Demo",
|
21 |
+
initial_sidebar_state="expanded",
|
22 |
+
layout="wide"
|
23 |
+
)
|
24 |
+
self.model_map = {
|
25 |
+
'Corrector': 1,
|
26 |
+
'Detector - coming soon': 2
|
27 |
+
}
|
28 |
+
self.examples = [
|
29 |
+
"what be the reason for everyone leave the comapny",
|
30 |
+
"He are moving here.",
|
31 |
+
"I am doing fine. How is you?",
|
32 |
+
"How is they?",
|
33 |
+
"Matt like fish",
|
34 |
+
"the collection of letters was original used by the ancient Romans",
|
35 |
+
"We enjoys horror movies",
|
36 |
+
"Anna and Mike is going skiing",
|
37 |
+
"I walk to the store and I bought milk",
|
38 |
+
" We all eat the fish and then made dessert",
|
39 |
+
"I will eat fish for dinner and drink milk",
|
40 |
+
]
|
41 |
+
|
42 |
+
@st.cache(show_spinner=False, suppress_st_warning=True, allow_output_mutation=True)
|
43 |
+
def load_gf(self, model: int):
|
44 |
+
"""
|
45 |
+
Load Gramformer model
|
46 |
+
"""
|
47 |
+
gf = Gramformer(models=model, use_gpu=False)
|
48 |
+
return gf
|
49 |
+
|
50 |
+
def show_highlights(self, gf: object, input_text: str, corrected_sentence: str):
|
51 |
+
"""
|
52 |
+
To show highlights
|
53 |
+
"""
|
54 |
+
try:
|
55 |
+
strikeout = lambda x: '\u0336'.join(x) + '\u0336'
|
56 |
+
highlight_text = gf.highlight(input_text, corrected_sentence)
|
57 |
+
color_map = {'d':'#faa', 'a':'#afa', 'c':'#fea'}
|
58 |
+
tokens = re.split(r'(<[dac]\s.*?<\/[dac]>)', highlight_text)
|
59 |
+
annotations = []
|
60 |
+
for token in tokens:
|
61 |
+
soup = BeautifulSoup(token, 'html.parser')
|
62 |
+
tags = soup.findAll()
|
63 |
+
if tags:
|
64 |
+
_tag = tags[0].name
|
65 |
+
_type = tags[0]['type']
|
66 |
+
_text = tags[0]['edit']
|
67 |
+
_color = color_map[_tag]
|
68 |
+
|
69 |
+
if _tag == 'd':
|
70 |
+
_text = strikeout(tags[0].text)
|
71 |
+
|
72 |
+
annotations.append((_text, _type, _color))
|
73 |
+
else:
|
74 |
+
annotations.append(token)
|
75 |
+
args = {
|
76 |
+
'height': 45*(math.ceil(len(highlight_text)/100)),
|
77 |
+
'scrolling': True
|
78 |
+
}
|
79 |
+
annotated_text(*annotations, **args)
|
80 |
+
except Exception as e:
|
81 |
+
st.error('Some error occured!')
|
82 |
+
st.stop()
|
83 |
+
|
84 |
+
def show_edits(self, gf: object, input_text: str, corrected_sentence: str):
|
85 |
+
"""
|
86 |
+
To show edits
|
87 |
+
"""
|
88 |
+
try:
|
89 |
+
edits = gf.get_edits(input_text, corrected_sentence)
|
90 |
+
df = pd.DataFrame(edits, columns=['type','original word', 'original start', 'original end', 'correct word', 'correct start', 'correct end'])
|
91 |
+
df = df.set_index('type')
|
92 |
+
st.table(df)
|
93 |
+
except Exception as e:
|
94 |
+
st.error('Some error occured!')
|
95 |
+
st.stop()
|
96 |
+
|
97 |
+
def main(self):
|
98 |
+
github_repo = 'https://github.com/PrithivirajDamodaran/Gramformer'
|
99 |
+
st.title("Gramformer")
|
100 |
+
st.write(f'GitHub Link - [{github_repo}]({github_repo})')
|
101 |
+
st.markdown('A framework for detecting, highlighting and correcting grammatical errors on natural language text')
|
102 |
+
|
103 |
+
model_type = st.sidebar.selectbox(
|
104 |
+
label='Choose Model',
|
105 |
+
options=list(self.model_map.keys())
|
106 |
+
)
|
107 |
+
if model_type == 'Corrector':
|
108 |
+
max_candidates = st.sidebar.number_input(
|
109 |
+
label='Max candidates',
|
110 |
+
min_value=1,
|
111 |
+
max_value=10,
|
112 |
+
value=1
|
113 |
+
)
|
114 |
+
else:
|
115 |
+
# NOTE:
|
116 |
+
st.warning('TO BE IMPLEMENTED !!')
|
117 |
+
st.stop()
|
118 |
+
|
119 |
+
with st.spinner('Loading model..'):
|
120 |
+
gf = self.load_gf(self.model_map[model_type])
|
121 |
+
|
122 |
+
input_text = st.selectbox(
|
123 |
+
label="Choose an example",
|
124 |
+
options=self.examples
|
125 |
+
)
|
126 |
+
input_text = st.text_input(
|
127 |
+
label="Input text",
|
128 |
+
value=input_text
|
129 |
+
)
|
130 |
+
|
131 |
+
if input_text.strip():
|
132 |
+
results = gf.correct(input_text, max_candidates=max_candidates)
|
133 |
+
corrected_sentence, score = results[0]
|
134 |
+
st.markdown(f'#### Output:')
|
135 |
+
st.write('')
|
136 |
+
st.success(corrected_sentence)
|
137 |
+
exp1 = st.beta_expander(label='Show highlights', expanded=True)
|
138 |
+
with exp1:
|
139 |
+
self.show_highlights(gf, input_text, corrected_sentence)
|
140 |
+
exp2 = st.beta_expander(label='Show edits')
|
141 |
+
with exp2:
|
142 |
+
self.show_edits(gf, input_text, corrected_sentence)
|
143 |
+
|
144 |
+
else:
|
145 |
+
st.warning("Please select/enter text to proceed")
|
146 |
+
|
147 |
+
if __name__ == "__main__":
|
148 |
+
obj = GramformerDemo()
|
149 |
+
obj.main()
|
images/GLogo.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
st-annotated-text
|
2 |
+
beautifulsoup4
|
3 |
+
git+https://github.com/PrithivirajDamodaran/Gramformer.git
|