Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load pre-trained model and tokenizer (Grammar correction model)
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_name = "prithivida/grammar_error_correcter_v1"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 11 |
+
return tokenizer, model
|
| 12 |
+
|
| 13 |
+
tokenizer, model = load_model()
|
| 14 |
+
|
| 15 |
+
# Function to correct grammar
|
| 16 |
+
def correct_grammar(text):
|
| 17 |
+
input_text = "gec: " + text
|
| 18 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True)
|
| 19 |
+
outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
|
| 20 |
+
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return corrected_text
|
| 22 |
+
|
| 23 |
+
# Streamlit UI
|
| 24 |
+
st.title("📝 Grammar Correction App")
|
| 25 |
+
st.write("Enter a sentence or paragraph below, and the AI will correct any grammatical errors.")
|
| 26 |
+
|
| 27 |
+
user_input = st.text_area("Your Text", height=200, placeholder="Type or paste your text here...")
|
| 28 |
+
|
| 29 |
+
if st.button("Correct Grammar"):
|
| 30 |
+
if user_input.strip():
|
| 31 |
+
with st.spinner("Correcting grammar..."):
|
| 32 |
+
corrected = correct_grammar(user_input)
|
| 33 |
+
st.subheader("✅ Corrected Text")
|
| 34 |
+
st.success(corrected)
|
| 35 |
+
else:
|
| 36 |
+
st.warning("Please enter some text to correct.")
|