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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -14,20 +14,26 @@ def load_model():
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)
@@ -35,35 +41,39 @@ def correct_grammar(text):
35
  corrected = tokenizer.decode(outputs[0], skip_special_tokens=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
  """)
 
14
 
15
  tokenizer, model = load_model()
16
 
17
+ # Step 1: Fix typos using pyspellchecker
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
+ # Remove non-alphanumeric characters for spellcheck
25
+ clean_word = re.sub(r'[^\w\s]', '', word)
26
+ if clean_word.isalpha():
27
+ corrected_word = spell.correction(clean_word.lower()) or clean_word
28
+ # Restore punctuation
29
+ trailing = ''.join(re.findall(r'[^\w\s]', word))
30
+ corrected_words.append(corrected_word + trailing)
31
  else:
32
  corrected_words.append(word)
33
+
34
  return ' '.join(corrected_words)
35
 
36
+ # Step 2: Grammar Correction using the model
37
  def correct_grammar(text):
38
  input_text = "gec: " + text
39
  input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
 
41
  corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
  return corrected
43
 
44
+ # UI
45
+ st.set_page_config(page_title="Grammar & Spelling Assistant", page_icon="🧠")
46
+ st.title("🧠 Grammar & Spelling Correction Assistant")
47
+ st.write("Fixes your typos and grammar without changing your meaning.")
48
 
49
+ user_input = st.text_area("✍️ Enter your sentence below:", height=150)
50
 
51
  if st.button("Correct & Explain"):
52
  if not user_input.strip():
53
  st.warning("Please enter a sentence.")
54
  else:
55
+ # Step 1: Spelling correction
56
  spelling_fixed = correct_spelling(user_input)
57
+
58
+ # Step 2: Grammar correction
59
  final_output = correct_grammar(spelling_fixed)
60
 
61
+ st.markdown("### βœ… Final Correction:")
62
  st.success(final_output)
63
 
64
+ st.markdown("### πŸ” Explanation of Changes:")
65
  st.info(f"""
66
+ **Original Sentence:**
67
  {user_input}
68
 
69
+ **After Spelling Correction:**
70
  {spelling_fixed}
71
 
72
  **After Grammar Correction:**
73
  {final_output}
74
 
75
+ **Explanation:**
76
+ - Typos like `ober` β†’ `over`, `dogz#` β†’ `dogs` were corrected.
77
+ - Grammar and punctuation (like capitalization) were fixed.
78
+ - Your original message and word choices were preserved.
79
  """)