Spaces:
Sleeping
Sleeping
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.") | |