ZeeAI1 commited on
Commit
5a43a4c
Β·
verified Β·
1 Parent(s): c2bb9b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -39
app.py CHANGED
@@ -1,48 +1,113 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- # App Title
5
- st.title("πŸ§‘β€πŸŽ“ Advanced AI English Correction & Improvement")
6
 
7
- # Load Google's FLAN-T5-LARGE model (cached)
8
- @st.cache_resource
9
- def load_correction_model():
10
- return pipeline("text2text-generation", model="google/flan-t5-large")
11
 
12
- correction_model = load_correction_model()
 
13
 
14
- # User input
15
- user_text = st.text_area("Enter your sentence, paragraph, or text here:", height=200)
 
 
 
 
 
 
16
 
17
- # Correction and analysis logic
18
- if st.button("Correct & Improve"):
19
- if not user_text.strip():
20
- st.warning("⚠️ Please enter text to analyze!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  else:
22
- prompt = f"Correct the grammar, punctuation, and improve readability: {user_text}"
23
- corrected_output = correction_model(prompt, max_length=512, clean_up_tokenization_spaces=True)
24
-
25
- improved_text = corrected_output[0]['generated_text']
26
-
27
- # Display Results
28
- st.subheader("βœ… Corrected & Improved Text:")
29
- st.write(improved_text)
30
-
31
- # Provide detailed explanation (AI-based)
32
- st.subheader("πŸ“ What's improved:")
33
- if user_text != improved_text:
34
- st.markdown("- Improved sentence structure & readability.")
35
- st.markdown("- Corrected grammar & punctuation.")
36
- st.markdown("- Enhanced clarity & coherence.")
37
- st.info("Review the corrected text carefully to better understand punctuation, grammar rules, and style improvements.")
38
- else:
39
- st.success("No issues found. Excellent writing!")
40
 
41
- # Learning recommendations
42
- st.subheader("πŸ“š Tips to Learn & Improve:")
43
- st.markdown("""
44
- - Review punctuation usage (commas, question marks, exclamation points).
45
- - Practice sentence structuring for clarity and readability.
46
- - Use resources like [Grammarly](https://www.grammarly.com/blog/category/handbook/) and [Purdue OWL](https://owl.purdue.edu/owl/purdue_owl.html) to enhance your writing skills.
47
- """)
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")