File size: 1,352 Bytes
0ff1c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
import re

# Load grammar correction model
@st.cache_resource
def load_corrector():
    return pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")

# Load explanation model
@st.cache_resource
def load_explainer():
    return pipeline("text2text-generation", model="google/flan-t5-large")

grammar_corrector = load_corrector()
explainer = load_explainer()

# App UI
st.title("🧠 Advanced Grammar Correction Assistant")
st.write("Enter a sentence. I’ll fix it and explain the correction in detail.")

user_input = st.text_area("✍️ Enter your sentence below:", height=100)

if st.button("Correct & Explain"):
    if user_input.strip() == "":
        st.warning("Please enter a sentence.")
    else:
        # Step 1: Grammar correction
        corrected = grammar_corrector(user_input, max_length=128, do_sample=False)[0]['generated_text']
        
        # Step 2: Explanation prompt
        explanation_prompt = f"Original: {user_input}\nCorrected: {corrected}\nExplain the changes simply with grammar rules and examples."
        explanation = explainer(explanation_prompt, max_length=200, do_sample=False)[0]['generated_text']
        
        # Output
        st.markdown(f"**Correction:** {corrected}")
        st.markdown(f"**Explanation:** {explanation}")