Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load model and tokenizer
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
-
model_name =
|
| 9 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 10 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 11 |
return tokenizer, model
|
| 12 |
|
| 13 |
tokenizer, model = load_model()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 19 |
outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
|
| 20 |
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
return corrected
|
| 22 |
|
| 23 |
# Streamlit UI
|
| 24 |
-
st.title("π Advanced Grammar Correction Assistant")
|
| 25 |
-
st.write("
|
| 26 |
|
| 27 |
-
user_input = st.text_area("
|
| 28 |
|
| 29 |
if st.button("Correct & Explain"):
|
| 30 |
-
if user_input.strip()
|
| 31 |
st.warning("Please enter a sentence.")
|
| 32 |
else:
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
st.markdown("### β
Correction:")
|
| 36 |
st.success(corrected)
|
|
@@ -38,12 +55,11 @@ if st.button("Correct & Explain"):
|
|
| 38 |
st.markdown("### π Explanation:")
|
| 39 |
st.info(f"""
|
| 40 |
*Original:* {user_input}
|
| 41 |
-
*
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
-
|
| 45 |
-
-
|
|
|
|
| 46 |
""")
|
| 47 |
-
|
| 48 |
-
st.caption("Model used: vennify/t5-base-grammar-correction")
|
| 49 |
-
|
|
|
|
| 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
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
+
model_name = "vennify/t5-base-grammar-correction"
|
| 10 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 11 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 12 |
return tokenizer, 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)
|
| 35 |
outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
|
| 36 |
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 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)
|
|
|
|
| 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 |
""")
|
|
|
|
|
|
|
|
|