Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,69 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
-
st.title("
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
user_text = st.text_area("Enter your sentence or paragraph:", height=200)
|
| 10 |
|
| 11 |
-
|
| 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 |
-
|
| 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
|
| 55 |
else:
|
| 56 |
-
|
| 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 |
-
|
| 70 |
-
|
| 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 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
st.markdown(f"- `{issue['original']}` β‘οΈ `{issue['replacement']}`")
|
| 83 |
-
st.markdown(f" **Context:** {issue['sentence']}")
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
for issue in punctuation_issues:
|
| 88 |
-
st.markdown(f"- `{issue['original']}` β‘οΈ `{issue['replacement']}`")
|
| 89 |
-
st.markdown(f" **Context:** {issue['sentence']}")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
if
|
| 98 |
-
st.
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
""")
|