Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load model and tokenizer from Hugging Face
|
6 |
+
@st.cache_resource
|
7 |
+
def load_model():
|
8 |
+
model_name = "vennify/t5-base-grammar-correction"
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
10 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
11 |
+
return tokenizer, model
|
12 |
+
|
13 |
+
tokenizer, model = load_model()
|
14 |
+
|
15 |
+
# Function to generate corrected sentence
|
16 |
+
def correct_sentence(sentence):
|
17 |
+
input_text = "gec: " + sentence
|
18 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
19 |
+
outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
|
20 |
+
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
return corrected
|
22 |
+
|
23 |
+
# Streamlit UI
|
24 |
+
st.title("π Advanced Grammar Correction Assistant")
|
25 |
+
st.write("Enter a sentence. I'll correct it and explain the changes.")
|
26 |
+
|
27 |
+
user_input = st.text_area("Your Sentence", height=150)
|
28 |
+
|
29 |
+
if st.button("Correct & Explain"):
|
30 |
+
if user_input.strip() == "":
|
31 |
+
st.warning("Please enter a sentence.")
|
32 |
+
else:
|
33 |
+
corrected = correct_sentence(user_input)
|
34 |
+
|
35 |
+
st.markdown("### β
Correction:")
|
36 |
+
st.success(corrected)
|
37 |
+
|
38 |
+
st.markdown("### π Explanation:")
|
39 |
+
st.info(f"""
|
40 |
+
*Original:* {user_input}
|
41 |
+
*Corrected:* {corrected}
|
42 |
+
|
43 |
+
Here's what changed:
|
44 |
+
- I used an AI model trained to correct grammar and sentence structure.
|
45 |
+
- To give detailed explanations for each mistake (like verb tense, subject-verb agreement, or punctuation), the app can be extended using another model or logic to compare the two sentences line by line.
|
46 |
+
""")
|
47 |
+
|
48 |
+
st.caption("Model used: vennify/t5-base-grammar-correction")
|
49 |
+
|