Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from textblob import TextBlob | |
st.title("π English Grammar, Spelling & Punctuation Assistant") | |
# Load models once (cached for efficiency) | |
def load_models(): | |
grammar_model = pipeline('text2text-generation', model='prithivida/grammar_error_correcter_v1') | |
punctuation_model = pipeline('text2text-generation', model='oliverguhr/fullstop-punctuation-multilang-large') | |
return grammar_model, punctuation_model | |
grammar_model, punctuation_model = load_models() | |
user_text = st.text_area("Enter text to correct:", height=200) | |
if st.button("Correct and Explain"): | |
if user_text.strip() == "": | |
st.warning("β οΈ Please enter text first.") | |
else: | |
# Step 1: Grammar correction | |
grammar_corrected = grammar_model(user_text, max_length=512)[0]['generated_text'] | |
# Step 2: Punctuation correction | |
punctuation_corrected = punctuation_model(grammar_corrected, max_length=512)[0]['generated_text'] | |
# Step 3: Spelling and pluralization correction using TextBlob | |
blob = TextBlob(punctuation_corrected) | |
spelling_corrected = str(blob.correct()) | |
# Display final corrected text | |
st.subheader("β Fully Corrected Text:") | |
st.write(spelling_corrected) | |
# Provide clear explanations of corrections | |
st.subheader("π Detailed Explanations:") | |
if user_text != spelling_corrected: | |
if user_text != grammar_corrected: | |
st.markdown("**π Grammar Corrections Applied:**") | |
st.write(grammar_corrected) | |
st.markdown("---") | |
if grammar_corrected != punctuation_corrected: | |
st.markdown("**π Punctuation Corrections Applied:**") | |
st.write(punctuation_corrected) | |
st.markdown("---") | |
if punctuation_corrected != spelling_corrected: | |
st.markdown("**π Spelling & Pluralization Corrections Applied:**") | |
st.write(spelling_corrected) | |
st.markdown("---") | |
st.info("Review each step carefully to understand the corrections made.") | |
else: | |
st.success("π Your text had no detectable errors!") | |
# Learning Recommendations | |
st.subheader("π Recommendations to Improve Your Writing:") | |
st.markdown(""" | |
- **Grammar**: Review rules related to verb tenses, articles, and sentence structures. | |
- **Punctuation**: Focus on correct usage of commas, periods, question marks, and exclamation points. | |
- **Spelling & Pluralization**: Regularly check spelling and plural forms, especially for irregular words. | |
**Recommended resources:** | |
- [Grammarly Handbook](https://www.grammarly.com/blog/category/handbook/) | |
- [English Grammar Lessons](https://www.englishgrammar.org/) | |
""") |