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