File size: 1,182 Bytes
3302b52
c692b85
 
42b2f28
c692b85
3302b52
42b2f28
c692b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e2709
3302b52
 
c692b85
 
 
 
3302b52
 
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
import streamlit as st
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch

st.set_page_config(page_title="Grammar Corrector", page_icon="πŸ“")
st.title("πŸ“ AI Grammar & Spell Corrector")

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

model, tokenizer = load_model()

def correct_grammar(text):
    input_text = "grammar: " + text
    inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
    with torch.no_grad():
        outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
    corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return corrected

text = st.text_area("Enter your text:", height=200)

if st.button("Correct Text"):
    if text.strip():
        with st.spinner("Correcting..."):
            corrected = correct_grammar(text)
            st.subheader("βœ… Corrected Text")
            st.write(corrected)
    else:
        st.warning("Please enter some text.")