ZeeAI1 commited on
Commit
3c5a224
Β·
verified Β·
1 Parent(s): 7642bfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -15
app.py CHANGED
@@ -1,22 +1,49 @@
1
  import streamlit as st
2
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
3
 
4
- # Load grammar correction model
5
- model_name = "vennify/t5-base-grammar-correction"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
- pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
9
 
10
- st.title("✍️ English Grammar & Writing Assistant")
 
 
 
11
 
12
- text = st.text_area("Enter your sentence or paragraph:")
 
 
 
13
 
14
- if st.button("Correct Text"):
15
- if not text.strip():
16
- st.warning("Please enter some text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
- input_text = "gec: " + text
19
- result = pipe(input_text, max_length=512)
20
- corrected = result[0]['generated_text']
21
- st.subheader("βœ… Corrected Text:")
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)