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