File size: 2,418 Bytes
374d18f
0af0de7
374d18f
 
00b387e
 
 
aad4ca6
374d18f
aad4ca6
0af0de7
374d18f
 
 
 
aad4ca6
 
0af0de7
 
aad4ca6
 
0af0de7
374d18f
aad4ca6
374d18f
0af0de7
aad4ca6
0af0de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374d18f
 
 
aad4ca6
 
0af0de7
aad4ca6
374d18f
 
 
0af0de7
aad4ca6
374d18f
0af0de7
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
import torch

# βœ… Page config must be first
st.set_page_config(page_title="Grammar Fixer & Coach", layout="centered")

# Load grammar correction model
@st.cache_resource
def load_grammar_model():
    model_name = "vennify/t5-base-grammar-correction"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
    return tokenizer, model

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

grammar_tokenizer, grammar_model = load_grammar_model()
explanation_model = load_explanation_model()

# Grammar correction function
def correct_grammar(text):
    inputs = grammar_tokenizer.encode(text, return_tensors="pt", truncation=True)
    outputs = grammar_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
    corrected = grammar_tokenizer.decode(outputs[0], skip_special_tokens=True)
    return corrected

# Explanation function
def get_detailed_feedback(original, corrected):
    prompt = (
        f"Analyze and explain all grammar, spelling, and punctuation corrections made when changing the following sentence:\n\n"
        f"Original: {original}\n"
        f"Corrected: {corrected}\n\n"
        f"Give a list of corrections with the reason for each, and also suggest how the user can improve their writing."
    )
    explanation = explanation_model(prompt)[0]['generated_text']
    return explanation

# Streamlit UI
st.title("🧠 Grammar Fixer & Writing Coach")
st.write("Paste your sentence or paragraph. The AI will correct it and explain each fix to help you learn.")

user_input = st.text_area("✍️ Enter your text below:", height=200, placeholder="e.g., I, want you! to please foucs on you work only!!")

if st.button("Correct & Explain"):
    if user_input.strip():
        with st.spinner("Correcting grammar..."):
            corrected = correct_grammar(user_input)

        with st.spinner("Explaining corrections..."):
            explanation = get_detailed_feedback(user_input, corrected)

        st.subheader("βœ… Corrected Text")
        st.success(corrected)

        st.subheader("πŸ“˜ Detailed Explanation")
        st.markdown(explanation)
    else:
        st.warning("Please enter some text.")