cahya commited on
Commit
5bdcff2
β€’
1 Parent(s): a6b45d1

add annotated text, and compare original text and the corrected text

Browse files
Files changed (2) hide show
  1. app.py +36 -1
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,9 +1,42 @@
1
  import streamlit as st
2
  from happytransformer import HappyTextToText, TTSettings
 
 
3
 
4
  checkpoint = "team-writing-assistant/t5-base-c4jfleg"
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @st.cache(suppress_st_warning=True, allow_output_mutation=True)
8
  def get_happy_text(model_name):
9
  st.write(f"Loading the HappyTextToText model {model_name}, please wait...")
@@ -31,7 +64,9 @@ def output(input_text):
31
  with st.spinner('Detecting πŸ”..'):
32
  input_text = "grammar: " + input_text
33
  result = happy_tt.generate_text(input_text, args=args)
34
- st.markdown("## " + result.text)
 
 
35
 
36
 
37
  if example_1:
 
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...")
 
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:
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- happytransformer
 
 
1
+ happytransformer
2
+ st-annotated-text