Spaces:
Sleeping
Sleeping
File size: 2,305 Bytes
7ec2de1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import difflib
# Load model and tokenizer
@st.cache_resource
def load_model():
model_name = "vennify/t5-base-grammar-correction"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
return tokenizer, model
tokenizer, model = load_model()
# Function to correct grammar
def correct_text(text):
input_text = "gec: " + text
inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True)
outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return corrected_text
# Word-by-word diff
def get_word_diffs(original, corrected):
orig_words = original.split()
corr_words = corrected.split()
diff = list(difflib.ndiff(orig_words, corr_words))
return diff
# Streamlit UI
st.set_page_config(page_title="Grammar Agent", layout="wide")
st.title("π Grammar Agent β Like Grammarly, Powered by AI")
st.markdown("""
This AI grammar agent helps you:
- β
Correct grammar, spelling, and punctuation
- β
Improve sentence clarity
- β
See word-by-word differences
- β
Get suggestions on how to rewrite
""")
text_input = st.text_area("Enter your sentence, paragraph, or essay:", height=200)
if st.button("Correct Grammar") and text_input.strip():
corrected = correct_text(text_input)
diffs = get_word_diffs(text_input, corrected)
st.subheader("π§ Corrected Version")
st.success(corrected)
st.subheader("π Word-by-Word Differences")
diff_text = []
for word in diffs:
if word.startswith("-"):
diff_text.append(f"β {word[2:]}")
elif word.startswith("+"):
diff_text.append(f"β
{word[2:]}")
st.markdown(" ".join(diff_text))
st.subheader("π‘ Writing Suggestion")
if len(text_input.split()) > len(corrected.split()):
st.info("Try simplifying your sentences for better clarity.")
elif len(corrected.split()) > len(text_input.split()):
st.info("The AI added helpful detail β good for formal writing.")
else:
st.info("The AI cleaned grammar and punctuation with minimal changes.")
|