Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,33 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
st.set_page_config(page_title="Grammar & Spell Corrector", page_icon="π")
|
5 |
|
|
|
6 |
st.title("π AI Grammar & Spell Corrector")
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
if st.button("Correct Text"):
|
11 |
if text.strip():
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
st.subheader("β
Corrected Text")
|
17 |
-
st.write(corrected_text)
|
18 |
else:
|
19 |
st.warning("Please enter some text.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
import torch
|
|
|
4 |
|
5 |
+
st.set_page_config(page_title="Grammar Corrector", page_icon="π")
|
6 |
st.title("π AI Grammar & Spell Corrector")
|
7 |
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("vennify/t5-base-grammar-correction")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("vennify/t5-base-grammar-correction")
|
12 |
+
return model, tokenizer
|
13 |
+
|
14 |
+
model, tokenizer = load_model()
|
15 |
+
|
16 |
+
def correct_grammar(text):
|
17 |
+
input_text = "grammar: " + text
|
18 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
|
21 |
+
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
return corrected
|
23 |
+
|
24 |
+
text = st.text_area("Enter your text:", height=200)
|
25 |
|
26 |
if st.button("Correct Text"):
|
27 |
if text.strip():
|
28 |
+
with st.spinner("Correcting..."):
|
29 |
+
corrected = correct_grammar(text)
|
30 |
+
st.subheader("β
Corrected Text")
|
31 |
+
st.write(corrected)
|
|
|
|
|
32 |
else:
|
33 |
st.warning("Please enter some text.")
|