Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,18 +14,27 @@ def load_model():
|
|
| 14 |
|
| 15 |
tokenizer, model = load_model()
|
| 16 |
|
| 17 |
-
# Step
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
|
@@ -33,7 +42,7 @@ def correct_spelling(text):
|
|
| 33 |
|
| 34 |
return ' '.join(corrected_words)
|
| 35 |
|
| 36 |
-
# Step 2: Grammar
|
| 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,31 +50,38 @@ def correct_grammar(text):
|
|
| 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
|
| 48 |
|
| 49 |
-
user_input = st.text_area("βοΈ Enter your sentence
|
| 50 |
|
| 51 |
if st.button("Correct & Explain"):
|
| 52 |
if not user_input.strip():
|
| 53 |
st.warning("Please enter a sentence.")
|
| 54 |
else:
|
| 55 |
-
# Step
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 65 |
st.info(f"""
|
| 66 |
**Original Sentence:**
|
| 67 |
{user_input}
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
**After Spelling Correction:**
|
| 70 |
{spelling_fixed}
|
| 71 |
|
|
@@ -73,7 +89,8 @@ if st.button("Correct & Explain"):
|
|
| 73 |
{final_output}
|
| 74 |
|
| 75 |
**Explanation:**
|
| 76 |
-
-
|
| 77 |
-
-
|
| 78 |
-
-
|
|
|
|
| 79 |
""")
|
|
|
|
| 14 |
|
| 15 |
tokenizer, model = load_model()
|
| 16 |
|
| 17 |
+
# Step 0: Preprocess the input
|
| 18 |
+
def preprocess_input(text):
|
| 19 |
+
# Remove special characters like '#' from the end
|
| 20 |
+
cleaned = re.sub(r'[^\w\s]$', '', text.strip())
|
| 21 |
+
|
| 22 |
+
# Ensure sentence ends with a period if not already
|
| 23 |
+
if not cleaned.endswith('.'):
|
| 24 |
+
cleaned += '.'
|
| 25 |
+
|
| 26 |
+
return cleaned
|
| 27 |
+
|
| 28 |
+
# Step 1: Spelling correction
|
| 29 |
def correct_spelling(text):
|
| 30 |
spell = SpellChecker()
|
| 31 |
words = re.findall(r'\b\w+\b|\S', text)
|
| 32 |
corrected_words = []
|
| 33 |
|
| 34 |
for word in words:
|
|
|
|
| 35 |
clean_word = re.sub(r'[^\w\s]', '', word)
|
| 36 |
if clean_word.isalpha():
|
| 37 |
corrected_word = spell.correction(clean_word.lower()) or clean_word
|
|
|
|
| 38 |
trailing = ''.join(re.findall(r'[^\w\s]', word))
|
| 39 |
corrected_words.append(corrected_word + trailing)
|
| 40 |
else:
|
|
|
|
| 42 |
|
| 43 |
return ' '.join(corrected_words)
|
| 44 |
|
| 45 |
+
# Step 2: Grammar correction
|
| 46 |
def correct_grammar(text):
|
| 47 |
input_text = "gec: " + text
|
| 48 |
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
|
|
|
| 50 |
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 51 |
return corrected
|
| 52 |
|
| 53 |
+
# Streamlit UI
|
| 54 |
st.set_page_config(page_title="Grammar & Spelling Assistant", page_icon="π§ ")
|
| 55 |
st.title("π§ Grammar & Spelling Correction Assistant")
|
| 56 |
+
st.write("Fixes grammar and spelling errors without changing your original meaning.")
|
| 57 |
|
| 58 |
+
user_input = st.text_area("βοΈ Enter your sentence:", height=150)
|
| 59 |
|
| 60 |
if st.button("Correct & Explain"):
|
| 61 |
if not user_input.strip():
|
| 62 |
st.warning("Please enter a sentence.")
|
| 63 |
else:
|
| 64 |
+
# Step 0: Preprocess
|
| 65 |
+
preprocessed = preprocess_input(user_input)
|
| 66 |
+
|
| 67 |
+
# Step 1: Spell check
|
| 68 |
+
spelling_fixed = correct_spelling(preprocessed)
|
| 69 |
|
| 70 |
# Step 2: Grammar correction
|
| 71 |
final_output = correct_grammar(spelling_fixed)
|
| 72 |
|
| 73 |
+
# Output
|
| 74 |
st.markdown("### β
Final Correction:")
|
| 75 |
st.success(final_output)
|
| 76 |
|
| 77 |
+
st.markdown("### π Explanation:")
|
| 78 |
st.info(f"""
|
| 79 |
**Original Sentence:**
|
| 80 |
{user_input}
|
| 81 |
|
| 82 |
+
**After Preprocessing (remove #, enforce period):**
|
| 83 |
+
{preprocessed}
|
| 84 |
+
|
| 85 |
**After Spelling Correction:**
|
| 86 |
{spelling_fixed}
|
| 87 |
|
|
|
|
| 89 |
{final_output}
|
| 90 |
|
| 91 |
**Explanation:**
|
| 92 |
+
- Special characters like `#` were removed
|
| 93 |
+
- Misspelled words like `ober` β `over`, `dogz` β `dogs` were fixed
|
| 94 |
+
- Grammar (capitalization, punctuation) was corrected
|
| 95 |
+
- No unwanted words like `#5` were added
|
| 96 |
""")
|