nikesh66 commited on
Commit
bcb25ae
1 Parent(s): 395606f

Add application file

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from happytransformer import HappyTextToText, TTSettings
3
+ from annotated_text import annotated_text
4
+ import difflib
5
+
6
+ checkpoint = "team-writing-assistant/t5-base-c4jfleg"
7
+
8
+
9
+ def diff_strings(a, b):
10
+ result = []
11
+ diff = difflib.Differ().compare(a.split(), b.split())
12
+ replacement = ""
13
+ for line in diff:
14
+ if line.startswith(" "):
15
+ if len(replacement) == 0:
16
+ result.append(" ")
17
+ result.append(line[2:])
18
+ else:
19
+ result.append(" ")
20
+ result.append(("", replacement, "#ffd"))
21
+ replacement = ""
22
+ result.append(line[2:])
23
+ elif line.startswith("- "):
24
+ if len(replacement) == 0:
25
+ replacement = line[2:]
26
+ else:
27
+ result.append(" ")
28
+ result.append(("", replacement, "#fdd"))
29
+ replacement = ""
30
+ elif line.startswith("+ "):
31
+ if len(replacement) == 0:
32
+ result.append(("", line[2:], "#dfd"))
33
+ else:
34
+ result.append(" ")
35
+ result.append((line[2:], replacement, "#ddf"))
36
+ replacement = ""
37
+ return result
38
+
39
+
40
+ @st.cache(suppress_st_warning=True, allow_output_mutation=True)
41
+ def get_happy_text(model_name):
42
+ st.write(f"Loading the HappyTextToText model {model_name}, please wait...")
43
+ return HappyTextToText("T5", model_name)
44
+
45
+
46
+ happy_tt = get_happy_text(checkpoint)
47
+ args = TTSettings(num_beams=5, min_length=1)
48
+
49
+ st.title("Check & Improve English Grammar")
50
+ st.markdown("This writing assistant detects 🔍 and corrects ✍️ grammatical mistakes for you!")
51
+
52
+ st.subheader("Example text: ")
53
+ col1, col2 = st.columns([1, 1])
54
+ with col1:
55
+ example_1 = st.button("Speed of light is fastest then speed of sound")
56
+ with col2:
57
+ example_2 = st.button("Who are the president?")
58
+
59
+ input_text = st.text_area('Enter your text here')
60
+ button = st.button('Submit')
61
+
62
+
63
+ def output(input_text):
64
+ with st.spinner('Detecting 🔍..'):
65
+ input_text = "grammar: " + input_text
66
+ result = happy_tt.generate_text(input_text, args=args)
67
+ # st.markdown("## " + result.text)
68
+ diff = diff_strings(input_text[9:], result.text)
69
+ annotated_text(*diff)
70
+
71
+
72
+ if example_1:
73
+ output("Speed of light is fastest then speed of sound")
74
+ elif example_2:
75
+ output("Who are the president?")
76
+ elif input_text:
77
+ output(input_text)
78
+
79
+ st.text("")
80
+ st.text("Built with ❤️")