Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from diff_match_patch import diff_match_patch
|
4 |
+
from langdetect import detect
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Load models
|
8 |
+
@st.cache_resource
|
9 |
+
def load_grammar_model():
|
10 |
+
return pipeline("text2text-generation", model="pszemraj/flan-t5-grammar-correction")
|
11 |
+
|
12 |
+
@st.cache_resource
|
13 |
+
def load_explainer_model():
|
14 |
+
return pipeline("text2text-generation", model="google/flan-t5-large")
|
15 |
+
|
16 |
+
@st.cache_resource
|
17 |
+
def load_translation_ur_to_en():
|
18 |
+
return pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
|
19 |
+
|
20 |
+
@st.cache_resource
|
21 |
+
def load_translation_en_to_ur():
|
22 |
+
return pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
|
23 |
+
|
24 |
+
# Initialize models
|
25 |
+
grammar_model = load_grammar_model()
|
26 |
+
explainer_model = load_explainer_model()
|
27 |
+
translate_ur_en = load_translation_ur_to_en()
|
28 |
+
translate_en_ur = load_translation_en_to_ur()
|
29 |
+
dmp = diff_match_patch()
|
30 |
+
|
31 |
+
st.title("π AI Grammar & Writing Assistant (Multilingual)")
|
32 |
+
st.markdown("Supports English & Urdu inputs. Fix grammar, punctuation, spelling, tenses β with explanations and writing tips.")
|
33 |
+
|
34 |
+
# Initialize session state
|
35 |
+
if "corrected_text" not in st.session_state:
|
36 |
+
st.session_state.corrected_text = ""
|
37 |
+
if "detected_lang" not in st.session_state:
|
38 |
+
st.session_state.detected_lang = ""
|
39 |
+
if "history" not in st.session_state:
|
40 |
+
st.session_state.history = []
|
41 |
+
|
42 |
+
user_input = st.text_area("βοΈ Enter your sentence, paragraph, or essay:", height=200)
|
43 |
+
|
44 |
+
# Detect & Translate Urdu if needed
|
45 |
+
def detect_and_translate_input(text):
|
46 |
+
lang = detect(text)
|
47 |
+
if lang == "ur":
|
48 |
+
st.info("π Detected Urdu input. Translating to English for grammar correction...")
|
49 |
+
translated = translate_ur_en(text)[0]['translation_text']
|
50 |
+
return translated, lang
|
51 |
+
return text, lang
|
52 |
+
|
53 |
+
# Button: Grammar Correction
|
54 |
+
if st.button("β
Correct Grammar"):
|
55 |
+
if user_input.strip():
|
56 |
+
translated_input, lang = detect_and_translate_input(user_input)
|
57 |
+
st.session_state.detected_lang = lang
|
58 |
+
|
59 |
+
corrected = grammar_model(f"grammar: {translated_input}", max_length=512, do_sample=False)[0]["generated_text"]
|
60 |
+
st.session_state.corrected_text = corrected
|
61 |
+
|
62 |
+
# Show corrected text
|
63 |
+
st.subheader("β
Corrected Text (in English)")
|
64 |
+
st.success(corrected)
|
65 |
+
|
66 |
+
# Highlight changes
|
67 |
+
st.subheader("π Changes Highlighted")
|
68 |
+
diffs = dmp.diff_main(translated_input, corrected)
|
69 |
+
dmp.diff_cleanupSemantic(diffs)
|
70 |
+
html_diff = ""
|
71 |
+
for (op, data) in diffs:
|
72 |
+
if op == -1:
|
73 |
+
html_diff += f'<span style="background-color:#fbb;">{data}</span>'
|
74 |
+
elif op == 1:
|
75 |
+
html_diff += f'<span style="background-color:#bfb;">{data}</span>'
|
76 |
+
else:
|
77 |
+
html_diff += data
|
78 |
+
st.markdown(f"<div style='font-family:monospace;'>{html_diff}</div>", unsafe_allow_html=True)
|
79 |
+
|
80 |
+
# Optional Urdu output
|
81 |
+
if lang == "ur":
|
82 |
+
urdu_back = translate_en_ur(corrected)[0]['translation_text']
|
83 |
+
st.subheader("π Corrected Text (Back in Urdu)")
|
84 |
+
st.success(urdu_back)
|
85 |
+
|
86 |
+
# Save to history
|
87 |
+
st.session_state.history.append({
|
88 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
89 |
+
"original": user_input,
|
90 |
+
"corrected": corrected,
|
91 |
+
"lang": lang
|
92 |
+
})
|
93 |
+
|
94 |
+
# Button: Explanation
|
95 |
+
if st.button("π§ Explain Corrections"):
|
96 |
+
if st.session_state.corrected_text:
|
97 |
+
st.subheader("Line-by-Line Explanation")
|
98 |
+
original_lines = user_input.split(".")
|
99 |
+
for line in original_lines:
|
100 |
+
if line.strip():
|
101 |
+
prompt = f"Explain and fix issues in this sentence:\n'{line.strip()}.'"
|
102 |
+
explanation = explainer_model(prompt, max_length=100)[0]["generated_text"]
|
103 |
+
st.markdown(f"**πΈ {line.strip()}**")
|
104 |
+
st.info(explanation)
|
105 |
+
else:
|
106 |
+
st.warning("Please correct the grammar first.")
|
107 |
+
|
108 |
+
# Button: Suggest Improvements
|
109 |
+
if st.button("π‘ Suggest Writing Improvements"):
|
110 |
+
if st.session_state.corrected_text:
|
111 |
+
prompt = f"Suggest improvements to make this text clearer and more professional:\n\n{st.session_state.corrected_text}"
|
112 |
+
suggestion = explainer_model(prompt, max_length=150)[0]["generated_text"]
|
113 |
+
st.subheader("Improvement Suggestions")
|
114 |
+
st.warning(suggestion)
|
115 |
+
else:
|
116 |
+
st.warning("Please correct the grammar first.")
|
117 |
+
|
118 |
+
# Download corrected text
|
119 |
+
if st.session_state.corrected_text:
|
120 |
+
st.download_button("β¬οΈ Download Corrected Text", st.session_state.corrected_text, file_name="corrected_text.txt")
|
121 |
+
|
122 |
+
# History viewer
|
123 |
+
if st.checkbox("π Show My Correction History"):
|
124 |
+
st.subheader("Correction History")
|
125 |
+
for record in st.session_state.history:
|
126 |
+
st.markdown(f"π **{record['timestamp']}** | Language: `{record['lang']}`")
|
127 |
+
st.markdown(f"**Original:** {record['original']}")
|
128 |
+
st.markdown(f"**Corrected:** {record['corrected']}")
|
129 |
+
st.markdown("---")
|