File size: 2,509 Bytes
d2e71f1
 
5bdcff2
 
d2e71f1
d1cc6ee
634e006
 
5bdcff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f24b138
5bdcff2
 
 
 
 
 
 
634e006
 
 
 
 
 
 
d2e71f1
 
d1cc6ee
 
d2e71f1
d1cc6ee
634e006
d1cc6ee
634e006
d1cc6ee
634e006
d1cc6ee
 
 
 
634e006
d1cc6ee
634e006
 
 
5bdcff2
 
 
634e006
d1cc6ee
 
634e006
a6b45d1
634e006
a6b45d1
634e006
d1cc6ee
 
 
634e006
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
from happytransformer import HappyTextToText, TTSettings
from annotated_text import annotated_text
import difflib

checkpoint = "team-writing-assistant/t5-base-c4jfleg"


def diff_strings(a, b):
    result = []
    diff = difflib.Differ().compare(a.split(), b.split())
    replacement = ""
    for line in diff:
        if line.startswith("  "):
            if len(replacement) == 0:
                result.append(" ")
                result.append(line[2:])
            else:
                result.append(" ")
                result.append(("", replacement, "#ffd"))
                replacement = ""
                result.append(line[2:])
        elif line.startswith("- "):
            if len(replacement) == 0:
                replacement = line[2:]
            else:
                result.append(" ")
                result.append(("", replacement, "#fdd"))
                replacement = ""
        elif line.startswith("+ "):
            if len(replacement) == 0:
                result.append((line[2:], "", "#dfd"))
            else:
                result.append(" ")
                result.append((line[2:], replacement, "#ddf"))
                replacement = ""
    return result


@st.cache(suppress_st_warning=True, allow_output_mutation=True)
def get_happy_text(model_name):
    st.write(f"Loading the HappyTextToText model {model_name}, please wait...")
    return HappyTextToText("T5", model_name)


happy_tt = get_happy_text(checkpoint)
args = TTSettings(num_beams=5, min_length=1)

st.title("Check & Improve English Grammar")
st.markdown("This writing assistant detects πŸ” and corrects ✍️ grammatical mistakes for you!")

st.subheader("Example text: ")
col1, col2 = st.columns([1, 1])
with col1:
    example_1 = st.button("Speed of light is fastest then speed of sound")
with col2:
    example_2 = st.button("Who are the president?")

input_text = st.text_area('Enter your text here')
button = st.button('Submit')


def output(input_text):
    with st.spinner('Detecting πŸ”..'):
        input_text = "grammar: " + input_text
        result = happy_tt.generate_text(input_text, args=args)
        # st.markdown("## " + result.text)
        diff = diff_strings(input_text[9:], result.text)
        annotated_text(*diff)


if example_1:
    output("Speed of light is fastest then speed of sound")
elif example_2:
    output("Who are the president?")
elif input_text:
    output(input_text)
  
st.subheader("")

st.text("Built by Team Writing Assistant ❀️")