Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="Grammar Agent - Grammarly Alternative", layout="wide")
|
| 6 |
+
st.title("π Grammar Agent - English Text Corrector")
|
| 7 |
+
|
| 8 |
+
# Load grammar correction model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 12 |
+
|
| 13 |
+
grammar_corrector = load_model()
|
| 14 |
+
|
| 15 |
+
# Utility functions
|
| 16 |
+
def capitalize_sentences(text):
|
| 17 |
+
sentences = re.split(r'(?<=[.!?]) +', text)
|
| 18 |
+
return ' '.join(s[0].upper() + s[1:] if s else '' for s in sentences)
|
| 19 |
+
|
| 20 |
+
def correct_text(text):
|
| 21 |
+
corrected = grammar_corrector(text, max_length=512)[0]['generated_text']
|
| 22 |
+
corrected = capitalize_sentences(corrected)
|
| 23 |
+
return corrected
|
| 24 |
+
|
| 25 |
+
def word_by_word_diff(original, corrected):
|
| 26 |
+
original_words = original.split()
|
| 27 |
+
corrected_words = corrected.split()
|
| 28 |
+
diffs = []
|
| 29 |
+
for i in range(min(len(original_words), len(corrected_words))):
|
| 30 |
+
if original_words[i] != corrected_words[i]:
|
| 31 |
+
diffs.append((original_words[i], corrected_words[i]))
|
| 32 |
+
if len(original_words) != len(corrected_words):
|
| 33 |
+
extra = corrected_words[len(original_words):]
|
| 34 |
+
for word in extra:
|
| 35 |
+
diffs.append(("", word))
|
| 36 |
+
return diffs
|
| 37 |
+
|
| 38 |
+
def analyze_differences(original, corrected):
|
| 39 |
+
diffs = word_by_word_diff(original, corrected)
|
| 40 |
+
analysis = []
|
| 41 |
+
for orig, corr in diffs:
|
| 42 |
+
reason = "Grammar, Spelling or Punctuation correction"
|
| 43 |
+
if orig == "":
|
| 44 |
+
reason = "Missing word"
|
| 45 |
+
elif orig.lower() != corr.lower():
|
| 46 |
+
if orig.lower() == corr:
|
| 47 |
+
reason = "Capitalization"
|
| 48 |
+
elif re.sub(r'[.,!?;]', '', orig.lower()) == re.sub(r'[.,!?;]', '', corr.lower()):
|
| 49 |
+
reason = "Punctuation"
|
| 50 |
+
elif len(orig) <= 3 or len(corr) <= 3:
|
| 51 |
+
reason = "Spelling"
|
| 52 |
+
analysis.append({
|
| 53 |
+
"Original": orig,
|
| 54 |
+
"Correction": corr,
|
| 55 |
+
"Reason": reason
|
| 56 |
+
})
|
| 57 |
+
return analysis
|
| 58 |
+
|
| 59 |
+
# Input Area
|
| 60 |
+
input_text = st.text_area("Enter your sentence, paragraph, or essay:", height=250)
|
| 61 |
+
|
| 62 |
+
if st.button("Correct and Analyze") and input_text:
|
| 63 |
+
with st.spinner("Analyzing and correcting text..."):
|
| 64 |
+
corrected = correct_text(input_text)
|
| 65 |
+
analysis = analyze_differences(input_text, corrected)
|
| 66 |
+
|
| 67 |
+
st.subheader("β
Corrected Text:")
|
| 68 |
+
st.success(corrected)
|
| 69 |
+
|
| 70 |
+
st.subheader("π Error Analysis:")
|
| 71 |
+
if analysis:
|
| 72 |
+
for item in analysis:
|
| 73 |
+
st.markdown(f"**Original:** `{item['Original']}` β **Correction:** `{item['Correction']}`")
|
| 74 |
+
st.markdown(f"_Reason_: {item['Reason']}")
|
| 75 |
+
st.markdown("---")
|
| 76 |
+
else:
|
| 77 |
+
st.write("No major corrections found. Great job!")
|
| 78 |
+
|
| 79 |
+
st.subheader("π Side-by-Side Comparison")
|
| 80 |
+
st.columns(2)[0].markdown(f"**Original:**\n\n{input_text}")
|
| 81 |
+
st.columns(2)[1].markdown(f"**Corrected:**\n\n{corrected}")
|
| 82 |
+
else:
|
| 83 |
+
st.info("Enter text above and click 'Correct and Analyze' to begin.")
|