Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,113 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
st.title("π§βπ Advanced AI English Correction & Improvement")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
@st.cache_resource
|
| 9 |
-
def load_correction_model():
|
| 10 |
-
return pipeline("text2text-generation", model="google/flan-t5-large")
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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.")
|