Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -18,4 +18,69 @@ def diff_strings(a, b):
|
|
18 |
result.append(" ")
|
19 |
result.append(line[2:])
|
20 |
else:
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
result.append(" ")
|
19 |
result.append(line[2:])
|
20 |
else:
|
21 |
+
result.append(" ")
|
22 |
+
result.append((""), replacement, "#ffd"))
|
23 |
+
replacement = ""
|
24 |
+
result.append(line[2:])
|
25 |
+
elif line.startswith("- "):
|
26 |
+
if len(replacement) == 0:
|
27 |
+
replacement = line[2:]
|
28 |
+
else:
|
29 |
+
result.append(" ")
|
30 |
+
result.append((""), replacement, "#fdd"))
|
31 |
+
replacement = ""
|
32 |
+
elif line.startswith("+ "):
|
33 |
+
if len(replacement) == 0:
|
34 |
+
result.append((line[2:]), "", "#dfd"))
|
35 |
+
else:
|
36 |
+
result.append(" ")
|
37 |
+
result.append((line[2:], replacement, "#ddf"))
|
38 |
+
replacement = ""
|
39 |
+
return result
|
40 |
+
|
41 |
+
@st.cache(suppress_st_warning=True, allow_output_mutation=True)
|
42 |
+
def get_happy_text(model_name):
|
43 |
+
st.write(f"Loading the HappyTextToText model {model_name}, please wait...")
|
44 |
+
return HappyTextToText("T5", model_name)
|
45 |
+
|
46 |
+
happy_tt = get_happy_text(checkpoint)
|
47 |
+
args = TTSettings(num_beams=5, min_length=1)
|
48 |
+
|
49 |
+
st.title("Grammar Corrector Two")
|
50 |
+
st.markdown("Paste or type text, enter and the machine will attempt to correct your text's grammar.")
|
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('Paste or type text')
|
60 |
+
button = st.button('Submit')
|
61 |
+
|
62 |
+
def output(text):
|
63 |
+
with st.spinner('Correcting'):
|
64 |
+
text = "grammar: " + text
|
65 |
+
result = happy_tt.generate_text(text, args=args)
|
66 |
+
diff = diff_strings(text[9:], result.text)
|
67 |
+
annotated_text(*diff)
|
68 |
+
|
69 |
+
copy_button = Button(label="Copy the Result")
|
70 |
+
copy_button.js_on_event("button_click", CustomJS(args=dict(result=result.text), code="""
|
71 |
+
navigator.clipboard.writeText(result);
|
72 |
+
"""))
|
73 |
+
streamlit_bokeh_events(
|
74 |
+
copy_button,
|
75 |
+
events="GET_TEXT",
|
76 |
+
key="get_text",
|
77 |
+
refresh_on_update=True,
|
78 |
+
override_height=75,
|
79 |
+
debounce_time=0)
|
80 |
+
|
81 |
+
if example_1:
|
82 |
+
output("Speed of light is fastest then speed of sound")
|
83 |
+
elif example_2:
|
84 |
+
output("Who are the president?")
|
85 |
+
elif input_text:
|
86 |
+
output(input_text)
|