ZeeAI1 commited on
Commit
0af0de7
Β·
verified Β·
1 Parent(s): aad4ca6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -1,56 +1,60 @@
1
  import streamlit as st
2
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
3
  import torch
4
 
5
  # Load grammar correction model
6
  @st.cache_resource
7
  def load_grammar_model():
8
- model_name = "prithivida/grammar_error_correcter_v1"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
  return tokenizer, model
12
 
13
  # Load explanation model
14
  @st.cache_resource
15
- def load_explainer():
16
- explainer = pipeline("text2text-generation", model="google/flan-t5-base", max_length=256)
17
- return explainer
18
 
19
  grammar_tokenizer, grammar_model = load_grammar_model()
20
- explanation_model = load_explainer()
21
 
22
  # Grammar correction function
23
  def correct_grammar(text):
24
- input_text = "gec: " + text
25
- inputs = grammar_tokenizer.encode(input_text, return_tensors="pt", truncation=True)
26
  outputs = grammar_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
27
- corrected_text = grammar_tokenizer.decode(outputs[0], skip_special_tokens=True)
28
- return corrected_text
29
-
30
- # Explanation function using second model
31
- def explain_correction(original, corrected):
32
- prompt = f"Explain the grammar improvements made from: \"{original}\" to: \"{corrected}\""
33
- result = explanation_model(prompt)[0]['generated_text']
34
- return result
35
-
36
- # Streamlit App UI
37
- st.title("πŸ“ Smart Grammar Correction with Explanations")
38
- st.write("Enter your text below. The AI will correct grammar **and explain why** the changes were made using grammar principles.")
39
-
40
- user_input = st.text_area("Your Text", height=200, placeholder="Type or paste your text here...")
41
-
42
- if st.button("Correct and Explain"):
 
 
 
 
 
 
43
  if user_input.strip():
44
  with st.spinner("Correcting grammar..."):
45
  corrected = correct_grammar(user_input)
46
 
47
  with st.spinner("Explaining corrections..."):
48
- explanation = explain_correction(user_input, corrected)
49
 
50
  st.subheader("βœ… Corrected Text")
51
  st.success(corrected)
52
 
53
- st.subheader("πŸ“˜ Explanation (Why it was changed)")
54
  st.markdown(explanation)
55
  else:
56
- st.warning("Please enter some text to correct.")
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  import torch
4
 
5
  # Load grammar correction model
6
  @st.cache_resource
7
  def load_grammar_model():
8
+ model_name = "vennify/t5-base-grammar-correction"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
  return tokenizer, model
12
 
13
  # Load explanation model
14
  @st.cache_resource
15
+ def load_explanation_model():
16
+ return pipeline("text2text-generation", model="google/flan-t5-large", max_length=512)
 
17
 
18
  grammar_tokenizer, grammar_model = load_grammar_model()
19
+ explanation_model = load_explanation_model()
20
 
21
  # Grammar correction function
22
  def correct_grammar(text):
23
+ inputs = grammar_tokenizer.encode(text, return_tensors="pt", truncation=True)
 
24
  outputs = grammar_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
25
+ corrected = grammar_tokenizer.decode(outputs[0], skip_special_tokens=True)
26
+ return corrected
27
+
28
+ # Explanation function
29
+ def get_detailed_feedback(original, corrected):
30
+ prompt = (
31
+ f"Analyze and explain all grammar, spelling, and punctuation corrections made when changing the following sentence:\n\n"
32
+ f"Original: {original}\n"
33
+ f"Corrected: {corrected}\n\n"
34
+ f"Give a list of corrections with the reason for each, and also suggest how the user can improve their writing."
35
+ )
36
+ explanation = explanation_model(prompt)[0]['generated_text']
37
+ return explanation
38
+
39
+ # Streamlit UI
40
+ st.set_page_config(page_title="Grammar Fixer & Coach", layout="centered")
41
+ st.title("🧠 Grammar Fixer & Writing Coach")
42
+ st.write("Paste your sentence or paragraph. The AI will correct it and explain each fix to help you learn.")
43
+
44
+ user_input = st.text_area("✍️ Enter your text below:", height=200, placeholder="e.g., I, want you! to please foucs on you work only!!")
45
+
46
+ if st.button("Correct & Explain"):
47
  if user_input.strip():
48
  with st.spinner("Correcting grammar..."):
49
  corrected = correct_grammar(user_input)
50
 
51
  with st.spinner("Explaining corrections..."):
52
+ explanation = get_detailed_feedback(user_input, corrected)
53
 
54
  st.subheader("βœ… Corrected Text")
55
  st.success(corrected)
56
 
57
+ st.subheader("πŸ“˜ Detailed Explanation")
58
  st.markdown(explanation)
59
  else:
60
+ st.warning("Please enter some text.")