aseifert commited on
Commit
4a1e2da
β€’
1 Parent(s): d43f5fb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -26
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import time
2
 
3
  import errant
 
4
  import streamlit as st
5
  from happytransformer import HappyTextToText, TTSettings
6
 
@@ -14,6 +15,12 @@ checkpoints = [
14
  ]
15
 
16
 
 
 
 
 
 
 
17
  @st.cache(suppress_st_warning=True, allow_output_mutation=True)
18
  def get_model(model_name):
19
  return HappyTextToText("T5", model_name)
@@ -24,30 +31,10 @@ def get_annotator(lang: str):
24
  return errant.load(lang)
25
 
26
 
27
- st.title("πŸ€— Writing Assistant")
28
- st.markdown(
29
- """This writing assistant will proofread any text for you! See my [GitHub repo](https://github.com/aseifert/hf-writing-assistant) for implementation details."""
30
- )
31
-
32
- checkpoint = st.selectbox("Choose model", checkpoints)
33
- happy_tt = get_model(checkpoint)
34
- annotator = get_annotator("en_core_web_sm")
35
- args = TTSettings(num_beams=5, min_length=1, max_length=1024)
36
-
37
- default_text = "A dog is bigger then mouse."
38
- default_text = "A dog is bigger then mouse."
39
- input_text = st.text_area(
40
- label="Original text",
41
- value=default_text,
42
- placeholder="Enter your text here",
43
- )
44
- button = st.button("✍️ Check")
45
-
46
-
47
- def output(input_text):
48
  with st.spinner("Checking for errors πŸ”"):
49
  prefixed_input_text = "Grammar: " + input_text
50
- result = happy_tt.generate_text(prefixed_input_text, args=args).text
51
 
52
  try:
53
  st.success(result)
@@ -58,7 +45,32 @@ def output(input_text):
58
  st.stop()
59
 
60
 
61
- start = time.time()
62
- output(input_text)
63
- st.write("---")
64
- st.text(f"Built by Team Writing Assistant ❀️ – prediction took {time.time() - start:.2f}s")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import time
2
 
3
  import errant
4
+ import spacy
5
  import streamlit as st
6
  from happytransformer import HappyTextToText, TTSettings
7
 
 
15
  ]
16
 
17
 
18
+ @st.cache
19
+ def initialize_spacy(model="en_core_web_sm"):
20
+ spacy.cli.download(model) # type: ignore
21
+ return True
22
+
23
+
24
  @st.cache(suppress_st_warning=True, allow_output_mutation=True)
25
  def get_model(model_name):
26
  return HappyTextToText("T5", model_name)
 
31
  return errant.load(lang)
32
 
33
 
34
+ def output(model, args, annotator, input_text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  with st.spinner("Checking for errors πŸ”"):
36
  prefixed_input_text = "Grammar: " + input_text
37
+ result = model.generate_text(prefixed_input_text, args=args).text
38
 
39
  try:
40
  st.success(result)
 
45
  st.stop()
46
 
47
 
48
+ def main():
49
+ st.title("πŸ€— Writing Assistant")
50
+ st.markdown(
51
+ """This writing assistant will proofread any text for you! See my [GitHub repo](https://github.com/aseifert/hf-writing-assistant) for implementation details."""
52
+ )
53
+
54
+ initialize_spacy()
55
+ annotator = get_annotator("en")
56
+ checkpoint = st.selectbox("Choose model", checkpoints)
57
+ model = get_model(checkpoint)
58
+ args = TTSettings(num_beams=5, min_length=1, max_length=1024)
59
+
60
+ default_text = "A dog is bigger then mouse."
61
+ default_text = "what be the reason for everyone leave the comapny"
62
+ input_text = st.text_area(
63
+ label="Original text",
64
+ value=default_text,
65
+ placeholder="Enter your text here",
66
+ )
67
+
68
+ if st.button("✍️ Check"):
69
+ start = time.time()
70
+ output(model, args, annotator, input_text)
71
+ st.write("---")
72
+ st.text(f"Built by Team Writing Assistant ❀️ – prediction took {time.time() - start:.2f}s")
73
+
74
+
75
+ if __name__ == "__main__":
76
+ main()