ZeeAI1 commited on
Commit
eb6dcce
Β·
verified Β·
1 Parent(s): bff2055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from transformers import T5ForConditionalGeneration, T5Tokenizer
3
  from spellchecker import SpellChecker
 
4
  import torch
5
 
6
  # Load model and tokenizer
@@ -13,22 +14,20 @@ def load_model():
13
 
14
  tokenizer, model = load_model()
15
 
16
- # Step 1: Spell Correction
17
  def correct_spelling(text):
18
  spell = SpellChecker()
19
- words = text.split()
20
- corrected = []
 
21
  for word in words:
22
  if word.isalpha():
23
- corrected.append(spell.correction(word) or word)
24
  else:
25
- # Handle punctuation-attached words
26
- stripped = ''.join(filter(str.isalpha, word))
27
- corrected_word = spell.correction(stripped) if stripped else word
28
- corrected.append(corrected_word + ''.join(filter(lambda c: not c.isalpha(), word)))
29
- return ' '.join(corrected)
30
 
31
- # Step 2: Grammar Correction using model
32
  def correct_grammar(text):
33
  input_text = "gec: " + text
34
  input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
@@ -37,29 +36,34 @@ def correct_grammar(text):
37
  return corrected
38
 
39
  # Streamlit UI
40
- st.title("πŸ“ Advanced Grammar & Spelling Correction Assistant")
41
- st.write("Fixes spelling issues first, then corrects grammar while keeping the meaning intact.")
42
 
43
- user_input = st.text_area("Enter your sentence:", height=150)
44
 
45
  if st.button("Correct & Explain"):
46
  if not user_input.strip():
47
  st.warning("Please enter a sentence.")
48
  else:
49
- step1 = correct_spelling(user_input)
50
- corrected = correct_grammar(step1)
51
 
52
  st.markdown("### βœ… Correction:")
53
- st.success(corrected)
54
 
55
  st.markdown("### πŸ” Explanation:")
56
  st.info(f"""
57
- *Original:* {user_input}
58
- *After Spellcheck:* {step1}
59
- *Final Grammar Fix:* {corrected}
 
 
 
 
 
60
 
61
- **Explanation:**
62
- - Typos like `ober`, `laZy`, and `dogz#` were detected and fixed.
63
- - Then grammar structure and capitalization were adjusted.
64
- - This two-step method avoids changing the sentence meaning.
65
  """)
 
1
  import streamlit as st
2
  from transformers import T5ForConditionalGeneration, T5Tokenizer
3
  from spellchecker import SpellChecker
4
+ import re
5
  import torch
6
 
7
  # Load model and tokenizer
 
14
 
15
  tokenizer, model = load_model()
16
 
17
+ # Step 1: Fix obvious typos using spellchecker
18
  def correct_spelling(text):
19
  spell = SpellChecker()
20
+ words = re.findall(r'\b\w+\b|\S', text)
21
+ corrected_words = []
22
+
23
  for word in words:
24
  if word.isalpha():
25
+ corrected_words.append(spell.correction(word.lower()) or word)
26
  else:
27
+ corrected_words.append(word)
28
+ return ' '.join(corrected_words)
 
 
 
29
 
30
+ # Step 2: Run grammar correction model
31
  def correct_grammar(text):
32
  input_text = "gec: " + text
33
  input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
 
36
  return corrected
37
 
38
  # Streamlit UI
39
+ st.title("🧠 AI Grammar & Spelling Fixer (No Weird Word Changes)")
40
+ st.write("Fixes grammar and spelling without changing your intended words.")
41
 
42
+ user_input = st.text_area("✍️ Type your sentence:", height=150)
43
 
44
  if st.button("Correct & Explain"):
45
  if not user_input.strip():
46
  st.warning("Please enter a sentence.")
47
  else:
48
+ spelling_fixed = correct_spelling(user_input)
49
+ final_output = correct_grammar(spelling_fixed)
50
 
51
  st.markdown("### βœ… Correction:")
52
+ st.success(final_output)
53
 
54
  st.markdown("### πŸ” Explanation:")
55
  st.info(f"""
56
+ **Original Input:**
57
+ {user_input}
58
+
59
+ **After Spell Check (typo fix only):**
60
+ {spelling_fixed}
61
+
62
+ **After Grammar Correction:**
63
+ {final_output}
64
 
65
+ **What Changed:**
66
+ - Fixed typos: `ober` β†’ `over`, `dogz#` β†’ `dogs#`
67
+ - Grammar and capitalization improved
68
+ - Meaning and keywords like "brown fox" and "lazy dog" were preserved
69
  """)