Spaces:
Running
Running
Update app.py
Browse files
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:
|
| 17 |
def correct_spelling(text):
|
| 18 |
spell = SpellChecker()
|
| 19 |
-
words =
|
| 20 |
-
|
|
|
|
| 21 |
for word in words:
|
| 22 |
if word.isalpha():
|
| 23 |
-
|
| 24 |
else:
|
| 25 |
-
|
| 26 |
-
|
| 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:
|
| 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("
|
| 41 |
-
st.write("Fixes
|
| 42 |
|
| 43 |
-
user_input = st.text_area("
|
| 44 |
|
| 45 |
if st.button("Correct & Explain"):
|
| 46 |
if not user_input.strip():
|
| 47 |
st.warning("Please enter a sentence.")
|
| 48 |
else:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
st.markdown("### β
Correction:")
|
| 53 |
-
st.success(
|
| 54 |
|
| 55 |
st.markdown("### π Explanation:")
|
| 56 |
st.info(f"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
**
|
| 62 |
-
-
|
| 63 |
-
-
|
| 64 |
-
-
|
| 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 |
""")
|