Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,20 @@
|
|
| 1 |
-
import
|
| 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
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 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 |
-
#
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 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 |
-
|
| 31 |
-
st.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|