File size: 3,263 Bytes
0e678a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1dc89
9b23548
cf1dc89
 
 
 
 
 
 
9b23548
cf1dc89
7f2c613
 
9e38a61
7f2c613
 
 
 
cf1dc89
 
 
7d77ba4
 
cf1dc89
 
 
 
 
33432c2
cf1dc89
 
c83e710
cf1dc89
6e36694
cf1dc89
3644159
781a561
 
cf1dc89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e36694
cf1dc89
3644159
781a561
 
cf1dc89
 
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
83
84
85
86
87
88
89
import streamlit as st
from happytransformer import HappyTextToText, TTSettings
from annotated_text import annotated_text
import difflib
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events

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):
  return HappyTextToText("T5", model_name)
         
happy_tt = get_happy_text(checkpoint)
args = TTSettings(num_beams=5, min_length=1)

st.title("Grammar Corrector Two")
st.markdown("Paste or type text. Submit. The machine will attempt to correct your text's grammar and highlight its corrections.")

st.subheader("Example text: ")
col1, col2, col3 = st.columns([1, 2, 1])
with col1:
  example_1 = st.button("Intrailly, the costumers was mad about why they will not but Fast Fashion again as they")
with col2:
  example_2 = st.button("Firstly,why i think this policy should be changed is because sometime the customer may buy wrong size,if our company’s no-exchange policy,customers have threatened no never buy from Fast Fashion again.")
with col3:
  example_3 = st.button("I try my best but still nervous. I hope I can get a good result.")

input_text = st.text_area('Paste or type text')
button = st.button('Submit')

def output(text):
  with st.spinner('Correcting'):
    text = "grammar: " + text
    result = happy_tt.generate_text(text, args=args)
    diff = diff_strings(text[9:], result.text)
    annotated_text(*diff)

    copy_button = Button(label="Copy the Result")
    copy_button.js_on_event("button_click", CustomJS(args=dict(result=result.text), code="""
      navigator.clipboard.writeText(result);
      """))
    streamlit_bokeh_events(
      copy_button,
      events="GET_TEXT",
      key="get_text",
      refresh_on_update=True,
      override_height=75,
      debounce_time=0)

if example_1:
  output("Intrailly, the costumers was mad about why they will not but Fast Fashion again as they")
elif example_2:
  output("Firstly,why i think this policy should be changed is because sometime the customer may buy wrong size,if our company’s no-exchange policy,customers have threatened no never buy from Fast Fashion again.")
elif example_3:
  output("I try my best but still nervous. I hope I can get a good result.")
elif input_text:
  output(input_text)