Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,49 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
7 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
8 |
-
|
9 |
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
else:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
st.subheader("β
Corrected
|
22 |
st.write(corrected)
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, T5Tokenizer, T5ForConditionalGeneration
|
3 |
|
4 |
+
# Model 1: Grammar & spelling correction
|
5 |
+
grammar_model = "vennify/t5-base-grammar-correction"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(grammar_model)
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(grammar_model)
|
8 |
+
corrector = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
+
# Model 2: Explanation & suggestions
|
11 |
+
explain_model = "google/flan-t5-large"
|
12 |
+
exp_tokenizer = T5Tokenizer.from_pretrained(explain_model)
|
13 |
+
exp_model = T5ForConditionalGeneration.from_pretrained(explain_model)
|
14 |
|
15 |
+
def correct_text(text):
|
16 |
+
input_text = "gec: " + text
|
17 |
+
result = corrector(input_text, max_length=512, clean_up_tokenization_spaces=True)
|
18 |
+
return result[0]['generated_text']
|
19 |
|
20 |
+
def explain_and_suggest(original, corrected):
|
21 |
+
prompt = (
|
22 |
+
f"Original: {original}\n"
|
23 |
+
f"Corrected: {corrected}\n"
|
24 |
+
"Explain the grammar, spelling, or style changes made. "
|
25 |
+
"Mention any alternate better words. Then suggest how the writer can improve in the future."
|
26 |
+
)
|
27 |
+
inputs = exp_tokenizer(prompt, return_tensors="pt", truncation=True)
|
28 |
+
outputs = exp_model.generate(**inputs, max_length=512)
|
29 |
+
return exp_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
+
|
31 |
+
# Streamlit UI
|
32 |
+
st.set_page_config(page_title="Grammar & Writing Coach", layout="centered")
|
33 |
+
st.title("π Grammar & Writing Coach")
|
34 |
+
st.markdown("Enter a sentence, paragraph, or essay. We'll correct it, explain the changes, and help you improve.")
|
35 |
+
|
36 |
+
text_input = st.text_area("βοΈ Your text here:", height=200)
|
37 |
+
|
38 |
+
if st.button("π Analyze & Improve"):
|
39 |
+
if not text_input.strip():
|
40 |
+
st.warning("Please enter some text first.")
|
41 |
else:
|
42 |
+
corrected = correct_text(text_input)
|
43 |
+
explanation = explain_and_suggest(text_input, corrected)
|
44 |
+
|
45 |
+
st.subheader("β
Corrected Version:")
|
46 |
st.write(corrected)
|
47 |
+
|
48 |
+
st.subheader("π Explanation & Suggestions:")
|
49 |
+
st.write(explanation)
|