ZeeAI1 commited on
Commit
aa0f607
Β·
verified Β·
1 Parent(s): 68091bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -96
app.py CHANGED
@@ -1,113 +1,69 @@
1
  import streamlit as st
2
- import requests
 
3
 
4
- st.title("πŸš€ English Grammar & Spelling Correction Assistant")
5
 
6
- API_KEY = "YOUR_SAPLING_API_KEY"
 
 
 
 
 
7
 
8
- # User input area
9
- user_text = st.text_area("Enter your sentence or paragraph:", height=200)
10
 
11
- def correct_text_with_sapling(text, api_key):
12
- url = "https://api.sapling.ai/api/v1/edits"
13
- headers = {"Content-Type": "application/json"}
14
- data = {
15
- "key": api_key,
16
- "text": text,
17
- "session_id": "streamlit-session"
18
- }
19
 
20
- response = requests.post(url, json=data, headers=headers)
21
-
22
- if response.status_code == 200:
23
- corrections = response.json().get("edits", [])
24
- corrected_text = text
25
- offset = 0
26
- details = []
27
-
28
- # Apply corrections
29
- for correction in corrections:
30
- start = correction['start'] + offset
31
- end = correction['end'] + offset
32
- original = corrected_text[start:end]
33
- replacement = correction['replacement']
34
-
35
- # Classify error type
36
- error_type = correction.get('general_error_type', 'Unknown')
37
-
38
- details.append({
39
- "original": original,
40
- "replacement": replacement,
41
- "type": error_type,
42
- "sentence": corrected_text[correction['sentence_start']:correction['sentence_end']]
43
- })
44
-
45
- corrected_text = corrected_text[:start] + replacement + corrected_text[end:]
46
- offset += len(replacement) - len(original)
47
-
48
- return corrected_text, details
49
- else:
50
- return None, None
51
-
52
- if st.button("Correct & Explain"):
53
  if user_text.strip() == "":
54
- st.warning("⚠️ Please enter text to correct!")
55
  else:
56
- corrected_text, details = correct_text_with_sapling(user_text, API_KEY)
57
-
58
- if corrected_text:
59
- st.subheader("βœ… Corrected Text:")
60
- st.write(corrected_text)
61
-
62
- grammar_issues = []
63
- punctuation_issues = []
64
- spelling_pluralization_issues = []
65
-
66
- for detail in details:
67
- issue_type = detail['type'].lower()
68
 
69
- if "punctuation" in issue_type:
70
- punctuation_issues.append(detail)
71
- elif "spelling" in issue_type or "plural" in issue_type:
72
- spelling_pluralization_issues.append(detail)
73
- else:
74
- grammar_issues.append(detail)
75
 
76
- # Display explanations
77
- st.subheader("πŸ“ Detailed Explanations:")
 
78
 
79
- if grammar_issues:
80
- st.markdown("**πŸ“Œ Grammar Issues:**")
81
- for issue in grammar_issues:
82
- st.markdown(f"- `{issue['original']}` ➑️ `{issue['replacement']}`")
83
- st.markdown(f" **Context:** {issue['sentence']}")
84
 
85
- if punctuation_issues:
86
- st.markdown("**πŸ“Œ Punctuation Issues:**")
87
- for issue in punctuation_issues:
88
- st.markdown(f"- `{issue['original']}` ➑️ `{issue['replacement']}`")
89
- st.markdown(f" **Context:** {issue['sentence']}")
90
 
91
- if spelling_pluralization_issues:
92
- st.markdown("**πŸ“Œ Spelling/Pluralization Issues:**")
93
- for issue in spelling_pluralization_issues:
94
- st.markdown(f"- `{issue['original']}` ➑️ `{issue['replacement']}`")
95
- st.markdown(f" **Context:** {issue['sentence']}")
96
 
97
- if not (grammar_issues or punctuation_issues or spelling_pluralization_issues):
98
- st.success("No issues found. Excellent job!")
 
 
99
 
100
- # Learning Suggestions
101
- st.subheader("πŸ“š Learning Suggestions:")
102
- st.markdown("""
103
- - **Grammar**: Review sentence structure, verb tenses, subject-verb agreement.
104
- - **Punctuation**: Focus on proper comma, question mark, and exclamation mark usage.
105
- - **Spelling & Pluralization**: Pay attention to correct spelling and plural forms of nouns.
106
-
107
- Recommended resources:
108
- - [EnglishGrammar.org](https://www.englishgrammar.org/)
109
- - [Grammarly Blog](https://www.grammarly.com/blog/category/handbook/)
110
- """)
111
 
 
112
  else:
113
- st.error("Error with API connection. Check your API key or internet connection.")
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  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
+ """)