aseifert commited on
Commit
b69b754
β€’
1 Parent(s): 4b30b6f

initial commit - app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import errant
4
+ import streamlit as st
5
+ from happytransformer import HappyTextToText, TTSettings
6
+
7
+ from highlighter import show_edits, show_highlights
8
+
9
+ checkpoints = [
10
+ "aseifert/t5-base-jfleg-wi",
11
+ "aseifert/byt5-base-jfleg-wi",
12
+ "prithivida/grammar_error_correcter_v2",
13
+ "Modfiededition/t5-base-fine-tuned-on-jfleg",
14
+ ]
15
+
16
+
17
+ @st.cache(suppress_st_warning=True, allow_output_mutation=True)
18
+ def get_model(model_name):
19
+ return HappyTextToText("T5", model_name)
20
+
21
+
22
+ @st.cache(suppress_st_warning=True, allow_output_mutation=True)
23
+ def get_annotator(lang: str):
24
+ return errant.load(lang)
25
+
26
+
27
+ st.title("Check & Improve English Grammar")
28
+ st.markdown("This writing assistant detects πŸ” and corrects ✍️ grammatical mistakes for you!")
29
+
30
+ checkpoint = st.selectbox("Choose model", checkpoints)
31
+ happy_tt = get_model(checkpoint)
32
+ annotator = get_annotator("en")
33
+ args = TTSettings(num_beams=5, min_length=1, max_length=1024)
34
+
35
+ input_text = st.text_area(
36
+ label="Original text",
37
+ value="A dog is bigger then mouse.",
38
+ placeholder="Enter your text here",
39
+ )
40
+ button = st.button("✍️ Check")
41
+
42
+
43
+ def output(input_text):
44
+ with st.spinner("Checking for errors πŸ”"):
45
+ prefixed_input_text = "Grammar: " + input_text
46
+ result = happy_tt.generate_text(prefixed_input_text, args=args).text
47
+
48
+ try:
49
+ st.success(result)
50
+ show_highlights(annotator, input_text, result)
51
+ # st.table(show_edits(annotator, input_text, result))
52
+ except Exception as e:
53
+ st.error("Some error occured!" + str(e))
54
+ st.stop()
55
+
56
+
57
+ start = time.time()
58
+ output(input_text)
59
+ st.write("---")
60
+ st.text(f"Built by Team Writing Assistant ❀️ – prediction took {time.time() - start:.2f}s")