Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| import torch | |
| # Load pre-trained model and tokenizer (Grammar correction model) | |
| def load_model(): | |
| model_name = "prithivida/grammar_error_correcter_v1" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
| return tokenizer, model | |
| tokenizer, model = load_model() | |
| # Function to correct grammar | |
| def correct_grammar(text): | |
| input_text = "gec: " + text | |
| inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True) | |
| outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True) | |
| corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return corrected_text | |
| # Streamlit UI | |
| st.title("π Grammar Correction App") | |
| st.write("Enter a sentence or paragraph below, and the AI will correct any grammatical errors.") | |
| user_input = st.text_area("Your Text", height=200, placeholder="Type or paste your text here...") | |
| if st.button("Correct Grammar"): | |
| if user_input.strip(): | |
| with st.spinner("Correcting grammar..."): | |
| corrected = correct_grammar(user_input) | |
| st.subheader("β Corrected Text") | |
| st.success(corrected) | |
| else: | |
| st.warning("Please enter some text to correct.") | |