ZeeAI1 commited on
Commit
285efdb
Β·
verified Β·
1 Parent(s): aa0f607

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -55
app.py CHANGED
@@ -2,68 +2,57 @@ import streamlit as st
2
  from transformers import pipeline
3
  from textblob import TextBlob
4
 
5
- st.title("πŸ“– English Grammar, Spelling & Punctuation Assistant")
6
 
7
- # Load models once (cached for efficiency)
8
  @st.cache_resource
9
- def load_models():
10
  grammar_model = pipeline('text2text-generation', model='prithivida/grammar_error_correcter_v1')
11
- punctuation_model = pipeline('text2text-generation', model='oliverguhr/fullstop-punctuation-multilang-large')
12
- return grammar_model, punctuation_model
13
 
14
- grammar_model, punctuation_model = load_models()
15
 
16
- user_text = st.text_area("Enter text to correct:", height=200)
17
 
18
- if st.button("Correct and Explain"):
19
- if user_text.strip() == "":
20
- st.warning("⚠️ Please enter text first.")
21
  else:
22
- # Step 1: Grammar correction
23
- grammar_corrected = grammar_model(user_text, max_length=512)[0]['generated_text']
24
-
25
- # Step 2: Punctuation correction
26
- punctuation_corrected = punctuation_model(grammar_corrected, max_length=512)[0]['generated_text']
27
-
28
- # Step 3: Spelling and pluralization correction using TextBlob
29
- blob = TextBlob(punctuation_corrected)
30
- spelling_corrected = str(blob.correct())
31
-
32
- # Display final corrected text
33
- st.subheader("βœ… Fully Corrected Text:")
34
- st.write(spelling_corrected)
35
-
36
- # Provide clear explanations of corrections
37
- st.subheader("πŸ“ Detailed Explanations:")
38
-
39
- if user_text != spelling_corrected:
40
- if user_text != grammar_corrected:
41
- st.markdown("**πŸ“Œ Grammar Corrections Applied:**")
42
- st.write(grammar_corrected)
43
- st.markdown("---")
44
-
45
- if grammar_corrected != punctuation_corrected:
46
- st.markdown("**πŸ“Œ Punctuation Corrections Applied:**")
47
- st.write(punctuation_corrected)
48
- st.markdown("---")
49
-
50
- if punctuation_corrected != spelling_corrected:
51
- st.markdown("**πŸ“Œ Spelling & Pluralization Corrections Applied:**")
52
- st.write(spelling_corrected)
53
- st.markdown("---")
54
-
55
- st.info("Review each step carefully to understand the corrections made.")
56
- else:
57
- st.success("πŸŽ‰ Your text had no detectable errors!")
58
-
59
- # Learning Recommendations
60
- st.subheader("πŸ“š Recommendations to Improve Your Writing:")
61
  st.markdown("""
62
- - **Grammar**: Review rules related to verb tenses, articles, and sentence structures.
63
- - **Punctuation**: Focus on correct usage of commas, periods, question marks, and exclamation points.
64
- - **Spelling & Pluralization**: Regularly check spelling and plural forms, especially for irregular words.
65
 
66
- **Recommended resources:**
67
  - [Grammarly Handbook](https://www.grammarly.com/blog/category/handbook/)
68
- - [English Grammar Lessons](https://www.englishgrammar.org/)
69
- """)
 
2
  from transformers import pipeline
3
  from textblob import TextBlob
4
 
5
+ st.title("πŸ“– AI Grammar & Spelling Correction")
6
 
7
+ # Load the grammar correction model
8
  @st.cache_resource
9
+ def load_model():
10
  grammar_model = pipeline('text2text-generation', model='prithivida/grammar_error_correcter_v1')
11
+ return grammar_model
 
12
 
13
+ grammar_model = load_model()
14
 
15
+ user_text = st.text_area("Enter text for correction:", height=200)
16
 
17
+ if st.button("Correct & Explain"):
18
+ if not user_text.strip():
19
+ st.warning("⚠️ Please enter some text.")
20
  else:
21
+ # Grammar & punctuation correction
22
+ corrected_output = grammar_model(user_text, max_length=512)
23
+ corrected_text = corrected_output[0]['generated_text']
24
+
25
+ # Spelling & pluralization correction using TextBlob
26
+ blob = TextBlob(corrected_text)
27
+ final_text = str(blob.correct())
28
+
29
+ # Display corrected text
30
+ st.subheader("βœ… Corrected Text:")
31
+ st.write(final_text)
32
+
33
+ # Explanation of corrections clearly
34
+ st.subheader("πŸ“ Explanation of Corrections:")
35
+
36
+ if user_text != corrected_text:
37
+ st.markdown("**Grammar & Punctuation Corrections Applied:**")
38
+ st.markdown(f"- `{corrected_text}`")
39
+ st.markdown("---")
40
+
41
+ if corrected_text != final_text:
42
+ st.markdown("**Spelling & Pluralization Corrections Applied:**")
43
+ st.markdown(f"- `{final_text}`")
44
+ st.markdown("---")
45
+
46
+ if user_text == final_text:
47
+ st.success("No corrections needed, your text was already correct!")
48
+
49
+ st.subheader("πŸ“š Recommendations to Improve:")
 
 
 
 
 
 
 
 
 
 
50
  st.markdown("""
51
+ - Regularly practice grammar, punctuation, and spelling.
52
+ - Review differences carefully to understand corrections.
53
+ - Use tools like Grammarly for continuous learning.
54
 
55
+ **Useful Links:**
56
  - [Grammarly Handbook](https://www.grammarly.com/blog/category/handbook/)
57
+ - [EnglishGrammar.org](https://www.englishgrammar.org/)
58
+ """)