ZeeAI1 commited on
Commit
cc3cf7b
Β·
verified Β·
1 Parent(s): 4802f7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -63
app.py CHANGED
@@ -1,79 +1,80 @@
 
 
1
  import streamlit as st
2
- from transformers import T5ForConditionalGeneration, T5Tokenizer
3
- from spellchecker import SpellChecker
4
- import re
5
- import torch
6
 
7
- # Load model and tokenizer
8
- @st.cache_resource
9
- def load_model():
10
- model_name = "vennify/t5-base-grammar-correction"
11
- tokenizer = T5Tokenizer.from_pretrained(model_name)
12
- model = T5ForConditionalGeneration.from_pretrained(model_name)
13
- return tokenizer, model
14
 
15
- tokenizer, model = load_model()
 
16
 
17
- # Step 1: Fix typos using pyspellchecker
18
- def correct_spelling(text):
19
- spell = SpellChecker()
20
- words = re.findall(r'\b\w+\b|\S', text)
21
- corrected_words = []
 
 
22
 
23
- for word in words:
24
- # Remove non-alphanumeric characters for spellcheck
25
- clean_word = re.sub(r'[^\w\s]', '', word)
26
- if clean_word.isalpha():
27
- corrected_word = spell.correction(clean_word.lower()) or clean_word
28
- # Restore punctuation
29
- trailing = ''.join(re.findall(r'[^\w\s]', word))
30
- corrected_words.append(corrected_word + trailing)
31
- else:
32
- corrected_words.append(word)
33
 
34
- return ' '.join(corrected_words)
 
 
 
 
 
 
35
 
36
- # Step 2: Grammar Correction using the model
37
- def correct_grammar(text):
38
- input_text = "gec: " + text
39
- input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
40
- outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
41
- corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
- return corrected
43
 
44
- # UI
45
- st.set_page_config(page_title="Grammar & Spelling Assistant", page_icon="🧠")
46
- st.title("🧠 Grammar & Spelling Correction Assistant")
47
- st.write("Fixes your typos and grammar without changing your meaning.")
48
 
49
- user_input = st.text_area("✍️ Enter your sentence below:", height=150)
 
 
 
 
50
 
51
- if st.button("Correct & Explain"):
52
- if not user_input.strip():
53
- st.warning("Please enter a sentence.")
54
- else:
55
- # Step 1: Spelling correction
56
- spelling_fixed = correct_spelling(user_input)
57
 
58
- # Step 2: Grammar correction
59
- final_output = correct_grammar(spelling_fixed)
 
 
 
60
 
61
- st.markdown("### βœ… Final Correction:")
62
- st.success(final_output)
 
 
 
 
 
 
63
 
64
- st.markdown("### πŸ” Explanation of Changes:")
65
- st.info(f"""
66
- **Original Sentence:**
67
- {user_input}
68
 
69
- **After Spelling Correction:**
70
- {spelling_fixed}
 
 
71
 
72
- **After Grammar Correction:**
73
- {final_output}
 
 
74
 
75
- **Explanation:**
76
- - Typos like `ober` β†’ `over`, `dogz#` β†’ `dogs` were corrected.
77
- - Grammar and punctuation (like capitalization) were fixed.
78
- - Your original message and word choices were preserved.
79
- """)
 
1
+ import os
2
+ import time
3
  import streamlit as st
4
+ import google.generativeai as genai
5
+ from google.auth import default
6
+ from dotenv import load_dotenv
 
7
 
8
+ # βœ… Set page config FIRST
9
+ st.set_page_config(page_title="Grammar & Spelling Assistant", page_icon="🧠")
 
 
 
 
 
10
 
11
+ # Load environment variables
12
+ load_dotenv()
13
 
14
+ # βœ… Use Application Default Credentials
15
+ if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
16
+ credentials, _ = default()
17
+ genai.configure(credentials=credentials)
18
+ else:
19
+ st.error("❌ GOOGLE_APPLICATION_CREDENTIALS is not set. Please add it to .env or Hugging Face Secrets.")
20
+ st.stop()
21
 
22
+ # Initialize Gemini model
23
+ model = genai.GenerativeModel("gemini-2.0-pro")
 
 
 
 
 
 
 
 
24
 
25
+ # Optional styling
26
+ st.markdown("""
27
+ <style>
28
+ body, .stApp { background-color: #121212 !important; color: #e0e0e0 !important; }
29
+ .stChatInput { background: #222 !important; border: 1px solid #555 !important; }
30
+ </style>
31
+ """, unsafe_allow_html=True)
32
 
33
+ # App header
34
+ st.title("πŸ“š Grammar Guardian")
35
+ st.caption("Correct grammar, get explanations, and improve your writing!")
 
 
 
 
36
 
37
+ # Initialize chat history
38
+ if "history" not in st.session_state:
39
+ st.session_state.history = []
 
40
 
41
+ # Display chat history
42
+ for message in st.session_state.history:
43
+ role, content = message["role"], message["content"]
44
+ with st.chat_message(role):
45
+ st.markdown(content)
46
 
47
+ # Chat input
48
+ prompt = st.chat_input("Type a sentence you'd like to improve...")
 
 
 
 
49
 
50
+ if prompt:
51
+ # Show user message
52
+ with st.chat_message("user"):
53
+ st.markdown(prompt)
54
+ st.session_state.history.append({"role": "user", "content": prompt})
55
 
56
+ # Process input with Gemini
57
+ with st.spinner("Analyzing..."):
58
+ try:
59
+ full_prompt = f"""
60
+ You are a grammar correction assistant.
61
+ When a user gives a sentence, do two things:
62
+ 1. Correct the sentence.
63
+ 2. Explain clearly why you corrected it.
64
 
65
+ Respond using this format:
66
+ **Correction:** <Corrected sentence>
67
+ **Explanation:** <Explanation of why it was corrected>
 
68
 
69
+ Sentence: {prompt}
70
+ """
71
+ response = model.generate_content(full_prompt)
72
+ result = response.text
73
 
74
+ # Show assistant message
75
+ with st.chat_message("assistant"):
76
+ st.markdown(result)
77
+ st.session_state.history.append({"role": "assistant", "content": result})
78
 
79
+ except Exception as e:
80
+ st.error(f"Error from Gemini API: {e}")