Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import re
|
4 |
+
|
5 |
+
# Load grammar correction model
|
6 |
+
@st.cache_resource
|
7 |
+
def load_corrector():
|
8 |
+
return pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
9 |
+
|
10 |
+
# Load explanation model
|
11 |
+
@st.cache_resource
|
12 |
+
def load_explainer():
|
13 |
+
return pipeline("text2text-generation", model="google/flan-t5-large")
|
14 |
+
|
15 |
+
grammar_corrector = load_corrector()
|
16 |
+
explainer = load_explainer()
|
17 |
+
|
18 |
+
# App UI
|
19 |
+
st.title("🧠 Advanced Grammar Correction Assistant")
|
20 |
+
st.write("Enter a sentence. I’ll fix it and explain the correction in detail.")
|
21 |
+
|
22 |
+
user_input = st.text_area("✍️ Enter your sentence below:", height=100)
|
23 |
+
|
24 |
+
if st.button("Correct & Explain"):
|
25 |
+
if user_input.strip() == "":
|
26 |
+
st.warning("Please enter a sentence.")
|
27 |
+
else:
|
28 |
+
# Step 1: Grammar correction
|
29 |
+
corrected = grammar_corrector(user_input, max_length=128, do_sample=False)[0]['generated_text']
|
30 |
+
|
31 |
+
# Step 2: Explanation prompt
|
32 |
+
explanation_prompt = f"Original: {user_input}\nCorrected: {corrected}\nExplain the changes simply with grammar rules and examples."
|
33 |
+
explanation = explainer(explanation_prompt, max_length=200, do_sample=False)[0]['generated_text']
|
34 |
+
|
35 |
+
# Output
|
36 |
+
st.markdown(f"**Correction:** {corrected}")
|
37 |
+
st.markdown(f"**Explanation:** {explanation}")
|