ZeeAI1 commited on
Commit
bff2055
Β·
verified Β·
1 Parent(s): f999b2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -1,36 +1,53 @@
1
  import streamlit as st
2
  from transformers import T5ForConditionalGeneration, T5Tokenizer
 
3
  import torch
4
 
5
- # Load model and tokenizer from Hugging Face
6
  @st.cache_resource
7
  def load_model():
8
- model_name = model_name = "pszemraj/flan-t5-large-grammar-synthesis"
9
  tokenizer = T5Tokenizer.from_pretrained(model_name)
10
  model = T5ForConditionalGeneration.from_pretrained(model_name)
11
  return tokenizer, model
12
 
13
  tokenizer, model = load_model()
14
 
15
- # Function to generate corrected sentence
16
- def correct_sentence(sentence):
17
- input_text = "gec: " + sentence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
19
  outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
20
  corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
  return corrected
22
 
23
  # Streamlit UI
24
- st.title("πŸ“ Advanced Grammar Correction Assistant")
25
- st.write("Enter a sentence. I'll correct it and explain the changes.")
26
 
27
- user_input = st.text_area("Your Sentence", height=150)
28
 
29
  if st.button("Correct & Explain"):
30
- if user_input.strip() == "":
31
  st.warning("Please enter a sentence.")
32
  else:
33
- corrected = correct_sentence(user_input)
 
34
 
35
  st.markdown("### βœ… Correction:")
36
  st.success(corrected)
@@ -38,12 +55,11 @@ if st.button("Correct & Explain"):
38
  st.markdown("### πŸ” Explanation:")
39
  st.info(f"""
40
  *Original:* {user_input}
41
- *Corrected:* {corrected}
 
42
 
43
- Here's what changed:
44
- - I used an AI model trained to correct grammar and sentence structure.
45
- - To give detailed explanations for each mistake (like verb tense, subject-verb agreement, or punctuation), the app can be extended using another model or logic to compare the two sentences line by line.
 
46
  """)
47
-
48
- st.caption("Model used: vennify/t5-base-grammar-correction")
49
-
 
1
  import streamlit as st
2
  from transformers import T5ForConditionalGeneration, T5Tokenizer
3
+ from spellchecker import SpellChecker
4
  import torch
5
 
6
+ # Load model and tokenizer
7
  @st.cache_resource
8
  def load_model():
9
+ model_name = "vennify/t5-base-grammar-correction"
10
  tokenizer = T5Tokenizer.from_pretrained(model_name)
11
  model = T5ForConditionalGeneration.from_pretrained(model_name)
12
  return tokenizer, model
13
 
14
  tokenizer, model = load_model()
15
 
16
+ # Step 1: Spell Correction
17
+ def correct_spelling(text):
18
+ spell = SpellChecker()
19
+ words = text.split()
20
+ corrected = []
21
+ for word in words:
22
+ if word.isalpha():
23
+ corrected.append(spell.correction(word) or word)
24
+ else:
25
+ # Handle punctuation-attached words
26
+ stripped = ''.join(filter(str.isalpha, word))
27
+ corrected_word = spell.correction(stripped) if stripped else word
28
+ corrected.append(corrected_word + ''.join(filter(lambda c: not c.isalpha(), word)))
29
+ return ' '.join(corrected)
30
+
31
+ # Step 2: Grammar Correction using model
32
+ def correct_grammar(text):
33
+ input_text = "gec: " + text
34
  input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
35
  outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
36
  corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
37
  return corrected
38
 
39
  # Streamlit UI
40
+ st.title("πŸ“ Advanced Grammar & Spelling Correction Assistant")
41
+ st.write("Fixes spelling issues first, then corrects grammar while keeping the meaning intact.")
42
 
43
+ user_input = st.text_area("Enter your sentence:", height=150)
44
 
45
  if st.button("Correct & Explain"):
46
+ if not user_input.strip():
47
  st.warning("Please enter a sentence.")
48
  else:
49
+ step1 = correct_spelling(user_input)
50
+ corrected = correct_grammar(step1)
51
 
52
  st.markdown("### βœ… Correction:")
53
  st.success(corrected)
 
55
  st.markdown("### πŸ” Explanation:")
56
  st.info(f"""
57
  *Original:* {user_input}
58
+ *After Spellcheck:* {step1}
59
+ *Final Grammar Fix:* {corrected}
60
 
61
+ **Explanation:**
62
+ - Typos like `ober`, `laZy`, and `dogz#` were detected and fixed.
63
+ - Then grammar structure and capitalization were adjusted.
64
+ - This two-step method avoids changing the sentence meaning.
65
  """)