ZeeAI1 commited on
Commit
0129c4f
·
verified ·
1 Parent(s): 0fc546f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -66
app.py CHANGED
@@ -1,72 +1,20 @@
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
- # Load environment variables
9
- load_dotenv()
 
 
10
 
11
- # Check for credentials and configure Gemini
12
- if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
13
- credentials, _ = default()
14
- genai.configure(credentials=credentials)
15
- else:
16
- st.error("❌ GOOGLE_APPLICATION_CREDENTIALS not set in the environment.")
17
- st.stop()
18
 
19
- # Initialize the model
20
- model = genai.GenerativeModel("gemini-2.0-pro")
21
 
22
- # Custom Streamlit styling (optional)
23
- st.markdown("""
24
- <style>
25
- body, .stApp { background-color: #121212 !important; color: #e0e0e0 !important; }
26
- .stChatInput { background: #222 !important; border: 1px solid #555 !important; }
27
- </style>
28
- """, unsafe_allow_html=True)
29
 
30
- # App Title
31
- st.title("📚 Grammar Guardian")
32
- st.caption("Correct grammar, get explanations, and improve your writing skills!")
33
-
34
- # Initialize session state
35
- if "history" not in st.session_state:
36
- st.session_state.history = []
37
-
38
- # Display chat history
39
- for message in st.session_state.history:
40
- role, content = message["role"], message["content"]
41
- with st.chat_message(role):
42
- st.markdown(content)
43
-
44
- # Chat input
45
- prompt = st.chat_input("Write a sentence you'd like to improve...")
46
-
47
- if prompt:
48
- with st.chat_message("user"):
49
- st.markdown(prompt)
50
- st.session_state.history.append({"role": "user", "content": prompt})
51
-
52
- with st.spinner("Analyzing..."):
53
- try:
54
- full_prompt = f"""
55
- You are an advanced grammar assistant. Correct the given sentence and explain the changes clearly.
56
-
57
- Respond in the following format:
58
- **Correction:** <Corrected Sentence>
59
- **Explanation:** <Why you corrected it>
60
-
61
- Sentence: {prompt}
62
- """
63
- response = model.generate_content(full_prompt)
64
- result = response.text
65
-
66
- st.session_state.history.append({"role": "assistant", "content": result})
67
-
68
- with st.chat_message("assistant"):
69
- st.markdown(result)
70
-
71
- except Exception as e:
72
- st.error(f"Error: {e}")
 
1
+ from transformers import pipeline
 
2
  import streamlit as st
 
 
 
3
 
4
+ # Load grammar correction model
5
+ @st.cache_resource
6
+ def load_model():
7
+ return pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
8
 
9
+ corrector = load_model()
 
 
 
 
 
 
10
 
11
+ # Streamlit UI
12
+ st.title("Grammar Correction Assistant")
13
 
14
+ user_input = st.text_area("Enter a sentence to correct:", "She don't like going to the gym because it make her tired.")
 
 
 
 
 
 
15
 
16
+ if st.button("Correct Sentence"):
17
+ with st.spinner("Correcting..."):
18
+ result = corrector(user_input, max_length=100, clean_up_tokenization_spaces=True)
19
+ corrected_sentence = result[0]['generated_text']
20
+ st.markdown(f"**Corrected Sentence:** {corrected_sentence}")