Jayant399 commited on
Commit
c692b85
Β·
verified Β·
1 Parent(s): fc72e61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -1,19 +1,33 @@
1
  import streamlit as st
2
- import language_tool_python
3
-
4
- st.set_page_config(page_title="Grammar & Spell Corrector", page_icon="πŸ“")
5
 
 
6
  st.title("πŸ“ AI Grammar & Spell Corrector")
7
 
8
- text = st.text_area("Enter your text below:", height=200)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  if st.button("Correct Text"):
11
  if text.strip():
12
- tool = language_tool_python.LanguageTool('en-US')
13
- matches = tool.check(text)
14
- corrected_text = language_tool_python.utils.correct(text, matches)
15
-
16
- st.subheader("βœ… Corrected Text")
17
- st.write(corrected_text)
18
  else:
19
  st.warning("Please enter some text.")
 
1
  import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+ import torch
 
4
 
5
+ st.set_page_config(page_title="Grammar Corrector", page_icon="πŸ“")
6
  st.title("πŸ“ AI Grammar & Spell Corrector")
7
 
8
+ @st.cache_resource
9
+ def load_model():
10
+ model = AutoModelForSeq2SeqLM.from_pretrained("vennify/t5-base-grammar-correction")
11
+ tokenizer = AutoTokenizer.from_pretrained("vennify/t5-base-grammar-correction")
12
+ return model, tokenizer
13
+
14
+ model, tokenizer = load_model()
15
+
16
+ def correct_grammar(text):
17
+ input_text = "grammar: " + text
18
+ inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
19
+ with torch.no_grad():
20
+ outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
21
+ corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+ return corrected
23
+
24
+ text = st.text_area("Enter your text:", height=200)
25
 
26
  if st.button("Correct Text"):
27
  if text.strip():
28
+ with st.spinner("Correcting..."):
29
+ corrected = correct_grammar(text)
30
+ st.subheader("βœ… Corrected Text")
31
+ st.write(corrected)
 
 
32
  else:
33
  st.warning("Please enter some text.")