ZeeAI1 commited on
Commit
7ec2de1
Β·
verified Β·
1 Parent(s): ef12942

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+ import difflib
4
+
5
+ # Load model and tokenizer
6
+ @st.cache_resource
7
+
8
+ def load_model():
9
+ model_name = "vennify/t5-base-grammar-correction"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
12
+ return tokenizer, model
13
+
14
+ tokenizer, model = load_model()
15
+
16
+ # Function to correct grammar
17
+ def correct_text(text):
18
+ input_text = "gec: " + text
19
+ inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True)
20
+ outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
21
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+ return corrected_text
23
+
24
+ # Word-by-word diff
25
+ def get_word_diffs(original, corrected):
26
+ orig_words = original.split()
27
+ corr_words = corrected.split()
28
+ diff = list(difflib.ndiff(orig_words, corr_words))
29
+ return diff
30
+
31
+ # Streamlit UI
32
+ st.set_page_config(page_title="Grammar Agent", layout="wide")
33
+ st.title("πŸ“ Grammar Agent – Like Grammarly, Powered by AI")
34
+
35
+ st.markdown("""
36
+ This AI grammar agent helps you:
37
+ - βœ… Correct grammar, spelling, and punctuation
38
+ - βœ… Improve sentence clarity
39
+ - βœ… See word-by-word differences
40
+ - βœ… Get suggestions on how to rewrite
41
+ """)
42
+
43
+ text_input = st.text_area("Enter your sentence, paragraph, or essay:", height=200)
44
+
45
+ if st.button("Correct Grammar") and text_input.strip():
46
+ corrected = correct_text(text_input)
47
+ diffs = get_word_diffs(text_input, corrected)
48
+
49
+ st.subheader("πŸ”§ Corrected Version")
50
+ st.success(corrected)
51
+
52
+ st.subheader("πŸ” Word-by-Word Differences")
53
+ diff_text = []
54
+ for word in diffs:
55
+ if word.startswith("-"):
56
+ diff_text.append(f"❌ {word[2:]}")
57
+ elif word.startswith("+"):
58
+ diff_text.append(f"βœ… {word[2:]}")
59
+ st.markdown(" ".join(diff_text))
60
+
61
+ st.subheader("πŸ’‘ Writing Suggestion")
62
+ if len(text_input.split()) > len(corrected.split()):
63
+ st.info("Try simplifying your sentences for better clarity.")
64
+ elif len(corrected.split()) > len(text_input.split()):
65
+ st.info("The AI added helpful detail – good for formal writing.")
66
+ else:
67
+ st.info("The AI cleaned grammar and punctuation with minimal changes.")