Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
3 |
+
|
4 |
+
# Load grammar correction model
|
5 |
+
model_name = "vennify/t5-base-grammar-correction"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
8 |
+
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
9 |
+
|
10 |
+
# Load explanation model
|
11 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
12 |
+
|
13 |
+
explain_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
|
14 |
+
explain_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
|
15 |
+
|
16 |
+
def correct_text(text):
|
17 |
+
input_text = "gec: " + text
|
18 |
+
result = pipe(input_text, max_length=512, clean_up_tokenization_spaces=True)
|
19 |
+
return result[0]['generated_text']
|
20 |
+
|
21 |
+
def explain_corrections(original, corrected):
|
22 |
+
prompt = f"""Original: {original}
|
23 |
+
Corrected: {corrected}
|
24 |
+
Explain the changes made, identify grammar or spelling issues, and give writing improvement tips."""
|
25 |
+
inputs = explain_tokenizer(prompt, return_tensors="pt", truncation=True)
|
26 |
+
outputs = explain_model.generate(**inputs, max_length=512)
|
27 |
+
return explain_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
# Streamlit UI
|
30 |
+
st.title("✍️ English Writing Assistant")
|
31 |
+
user_input = st.text_area("Enter your sentence, paragraph, or essay:")
|
32 |
+
|
33 |
+
if st.button("Check & Improve"):
|
34 |
+
if user_input.strip() == "":
|
35 |
+
st.warning("Please enter some text.")
|
36 |
+
else:
|
37 |
+
corrected = correct_text(user_input)
|
38 |
+
explanation = explain_corrections(user_input, corrected)
|
39 |
+
|
40 |
+
st.subheader("✅ Corrected Text:")
|
41 |
+
st.write(corrected)
|
42 |
+
|
43 |
+
st.subheader("📘 Explanation & Suggestions:")
|
44 |
+
st.write(explanation)
|