Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,60 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
# Load grammar correction model
|
6 |
@st.cache_resource
|
7 |
def load_grammar_model():
|
8 |
-
model_name = "
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
11 |
return tokenizer, model
|
12 |
|
13 |
# Load explanation model
|
14 |
@st.cache_resource
|
15 |
-
def
|
16 |
-
|
17 |
-
return explainer
|
18 |
|
19 |
grammar_tokenizer, grammar_model = load_grammar_model()
|
20 |
-
explanation_model =
|
21 |
|
22 |
# Grammar correction function
|
23 |
def correct_grammar(text):
|
24 |
-
|
25 |
-
inputs = grammar_tokenizer.encode(input_text, return_tensors="pt", truncation=True)
|
26 |
outputs = grammar_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
|
27 |
-
|
28 |
-
return
|
29 |
-
|
30 |
-
# Explanation function
|
31 |
-
def
|
32 |
-
prompt =
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
if user_input.strip():
|
44 |
with st.spinner("Correcting grammar..."):
|
45 |
corrected = correct_grammar(user_input)
|
46 |
|
47 |
with st.spinner("Explaining corrections..."):
|
48 |
-
explanation =
|
49 |
|
50 |
st.subheader("β
Corrected Text")
|
51 |
st.success(corrected)
|
52 |
|
53 |
-
st.subheader("π Explanation
|
54 |
st.markdown(explanation)
|
55 |
else:
|
56 |
-
st.warning("Please enter some text
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
import torch
|
4 |
|
5 |
# Load grammar correction model
|
6 |
@st.cache_resource
|
7 |
def load_grammar_model():
|
8 |
+
model_name = "vennify/t5-base-grammar-correction"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
11 |
return tokenizer, model
|
12 |
|
13 |
# Load explanation model
|
14 |
@st.cache_resource
|
15 |
+
def load_explanation_model():
|
16 |
+
return pipeline("text2text-generation", model="google/flan-t5-large", max_length=512)
|
|
|
17 |
|
18 |
grammar_tokenizer, grammar_model = load_grammar_model()
|
19 |
+
explanation_model = load_explanation_model()
|
20 |
|
21 |
# Grammar correction function
|
22 |
def correct_grammar(text):
|
23 |
+
inputs = grammar_tokenizer.encode(text, return_tensors="pt", truncation=True)
|
|
|
24 |
outputs = grammar_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
|
25 |
+
corrected = grammar_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
+
return corrected
|
27 |
+
|
28 |
+
# Explanation function
|
29 |
+
def get_detailed_feedback(original, corrected):
|
30 |
+
prompt = (
|
31 |
+
f"Analyze and explain all grammar, spelling, and punctuation corrections made when changing the following sentence:\n\n"
|
32 |
+
f"Original: {original}\n"
|
33 |
+
f"Corrected: {corrected}\n\n"
|
34 |
+
f"Give a list of corrections with the reason for each, and also suggest how the user can improve their writing."
|
35 |
+
)
|
36 |
+
explanation = explanation_model(prompt)[0]['generated_text']
|
37 |
+
return explanation
|
38 |
+
|
39 |
+
# Streamlit UI
|
40 |
+
st.set_page_config(page_title="Grammar Fixer & Coach", layout="centered")
|
41 |
+
st.title("π§ Grammar Fixer & Writing Coach")
|
42 |
+
st.write("Paste your sentence or paragraph. The AI will correct it and explain each fix to help you learn.")
|
43 |
+
|
44 |
+
user_input = st.text_area("βοΈ Enter your text below:", height=200, placeholder="e.g., I, want you! to please foucs on you work only!!")
|
45 |
+
|
46 |
+
if st.button("Correct & Explain"):
|
47 |
if user_input.strip():
|
48 |
with st.spinner("Correcting grammar..."):
|
49 |
corrected = correct_grammar(user_input)
|
50 |
|
51 |
with st.spinner("Explaining corrections..."):
|
52 |
+
explanation = get_detailed_feedback(user_input, corrected)
|
53 |
|
54 |
st.subheader("β
Corrected Text")
|
55 |
st.success(corrected)
|
56 |
|
57 |
+
st.subheader("π Detailed Explanation")
|
58 |
st.markdown(explanation)
|
59 |
else:
|
60 |
+
st.warning("Please enter some text.")
|