Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,20 +14,26 @@ def load_model():
|
|
14 |
|
15 |
tokenizer, model = load_model()
|
16 |
|
17 |
-
# Step 1: Fix
|
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 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
else:
|
27 |
corrected_words.append(word)
|
|
|
28 |
return ' '.join(corrected_words)
|
29 |
|
30 |
-
# Step 2:
|
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 |
-
#
|
39 |
-
st.
|
40 |
-
st.
|
|
|
41 |
|
42 |
-
user_input = st.text_area("βοΈ
|
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
|
57 |
{user_input}
|
58 |
|
59 |
-
**After
|
60 |
{spelling_fixed}
|
61 |
|
62 |
**After Grammar Correction:**
|
63 |
{final_output}
|
64 |
|
65 |
-
**
|
66 |
-
-
|
67 |
-
- Grammar and capitalization
|
68 |
-
-
|
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 |
""")
|