Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from diff_match_patch import diff_match_patch
|
| 4 |
+
|
| 5 |
+
# Load grammar correction pipeline
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_grammar_model():
|
| 8 |
+
return pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 9 |
+
|
| 10 |
+
# Optional: load explanation model (like flan-t5)
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_explainer_model():
|
| 13 |
+
return pipeline("text2text-generation", model="google/flan-t5-large")
|
| 14 |
+
|
| 15 |
+
grammar_model = load_grammar_model()
|
| 16 |
+
explainer_model = load_explainer_model()
|
| 17 |
+
dmp = diff_match_patch()
|
| 18 |
+
|
| 19 |
+
st.title("Grammarly-like AI Writing Assistant")
|
| 20 |
+
st.markdown("Fix grammar, punctuation, spelling, tenses — with explanations and tips!")
|
| 21 |
+
|
| 22 |
+
# User input
|
| 23 |
+
user_input = st.text_area("Enter your sentence, paragraph, or essay:", height=200)
|
| 24 |
+
|
| 25 |
+
if st.button("Correct Grammar"):
|
| 26 |
+
if user_input.strip():
|
| 27 |
+
# Correct the input
|
| 28 |
+
output = grammar_model(f"grammar: {user_input}", max_length=512, do_sample=False)[0]["generated_text"]
|
| 29 |
+
st.subheader("Corrected Text")
|
| 30 |
+
st.success(output)
|
| 31 |
+
|
| 32 |
+
# Show word-by-word diff
|
| 33 |
+
st.subheader("Changes Highlighted")
|
| 34 |
+
diffs = dmp.diff_main(user_input, output)
|
| 35 |
+
dmp.diff_cleanupSemantic(diffs)
|
| 36 |
+
html_diff = ""
|
| 37 |
+
for (op, data) in diffs:
|
| 38 |
+
if op == -1:
|
| 39 |
+
html_diff += f'<span style="background-color:#fbb;">{data}</span>'
|
| 40 |
+
elif op == 1:
|
| 41 |
+
html_diff += f'<span style="background-color:#bfb;">{data}</span>'
|
| 42 |
+
else:
|
| 43 |
+
html_diff += data
|
| 44 |
+
st.markdown(f"<div style='font-family:monospace;'>{html_diff}</div>", unsafe_allow_html=True)
|
| 45 |
+
|
| 46 |
+
# Explanation
|
| 47 |
+
if st.button("Explain Corrections"):
|
| 48 |
+
explanation_prompt = f"Explain the grammar issues in this text and how it was improved: {user_input}"
|
| 49 |
+
explanation = explainer_model(explanation_prompt, max_length=200)[0]['generated_text']
|
| 50 |
+
st.subheader("Explanation")
|
| 51 |
+
st.info(explanation)
|
| 52 |
+
|
| 53 |
+
# Suggest improvements
|
| 54 |
+
if st.button("Suggest Improvements"):
|
| 55 |
+
suggest_prompt = f"Suggest improvements to make this writing more professional: {output}"
|
| 56 |
+
suggestions = explainer_model(suggest_prompt, max_length=200)[0]['generated_text']
|
| 57 |
+
st.subheader("Suggestions")
|
| 58 |
+
st.warning(suggestions)
|