Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,23 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Function for grammar correction
|
11 |
def correct_grammar(user_input):
|
12 |
if user_input.strip():
|
13 |
-
corrected_text = grammar_correction_pipe(user_input)[0]['generated_text']
|
14 |
return corrected_text
|
15 |
else:
|
16 |
return "Please enter some text for grammar correction."
|
@@ -18,7 +25,7 @@ def correct_grammar(user_input):
|
|
18 |
# Function for text summarization
|
19 |
def summarize_text(user_input):
|
20 |
if user_input.strip():
|
21 |
-
summary = summarizer(user_input, max_length=
|
22 |
return summary
|
23 |
else:
|
24 |
return "Please enter some text to summarize."
|
@@ -38,7 +45,7 @@ task = st.selectbox("Choose a task", ["Summarize Text", "Correct Grammar"])
|
|
38 |
# Input component for text
|
39 |
user_input = st.text_area("Enter your text here:")
|
40 |
|
41 |
-
#
|
42 |
if st.button("Submit"):
|
43 |
if task == "Summarize Text":
|
44 |
output = correct_and_summarize(user_input) # Correct grammar, then summarize
|
@@ -46,4 +53,4 @@ if st.button("Submit"):
|
|
46 |
output = correct_grammar(user_input) # Only correct grammar
|
47 |
|
48 |
# Display the output
|
49 |
-
st.text_area("Output", output, height=200
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Caching model loading to optimize memory usage
|
5 |
+
@st.cache_resource
|
6 |
+
def load_summarizer():
|
7 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
8 |
|
9 |
+
@st.cache_resource
|
10 |
+
def load_grammar_correction_pipe():
|
11 |
+
return pipeline("text2text-generation", model="pszemraj/flan-t5-large-grammar-synthesis")
|
12 |
+
|
13 |
+
# Initialize models using the cached loading functions
|
14 |
+
summarizer = load_summarizer()
|
15 |
+
grammar_correction_pipe = load_grammar_correction_pipe()
|
16 |
|
17 |
# Function for grammar correction
|
18 |
def correct_grammar(user_input):
|
19 |
if user_input.strip():
|
20 |
+
corrected_text = grammar_correction_pipe(user_input, max_length=256)[0]['generated_text']
|
21 |
return corrected_text
|
22 |
else:
|
23 |
return "Please enter some text for grammar correction."
|
|
|
25 |
# Function for text summarization
|
26 |
def summarize_text(user_input):
|
27 |
if user_input.strip():
|
28 |
+
summary = summarizer(user_input, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
|
29 |
return summary
|
30 |
else:
|
31 |
return "Please enter some text to summarize."
|
|
|
45 |
# Input component for text
|
46 |
user_input = st.text_area("Enter your text here:")
|
47 |
|
48 |
+
# Submit button
|
49 |
if st.button("Submit"):
|
50 |
if task == "Summarize Text":
|
51 |
output = correct_and_summarize(user_input) # Correct grammar, then summarize
|
|
|
53 |
output = correct_grammar(user_input) # Only correct grammar
|
54 |
|
55 |
# Display the output
|
56 |
+
st.text_area("Output", output, height=200)
|