Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,79 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
import streamlit as st
|
4 |
-
import
|
5 |
-
from
|
6 |
-
|
|
|
7 |
|
8 |
-
#
|
9 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
load_dotenv()
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
st.error("β GOOGLE_APPLICATION_CREDENTIALS is not set. Please add it to .env or Hugging Face Secrets.")
|
20 |
-
st.stop()
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
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 |
-
#
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
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 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
with st.chat_message("user"):
|
53 |
-
st.markdown(prompt)
|
54 |
-
st.session_state.history.append({"role": "user", "content": prompt})
|
55 |
|
56 |
-
|
57 |
-
|
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 |
-
|
66 |
-
|
67 |
-
**
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
response = model.generate_content(full_prompt)
|
72 |
-
result = response.text
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
st.markdown(result)
|
77 |
-
st.session_state.history.append({"role": "assistant", "content": result})
|
78 |
|
79 |
-
|
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 |
+
""")
|