Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,58 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from keybert import KeyBERT
|
3 |
-
from sentence_transformers import SentenceTransformer
|
4 |
-
from transformers import pipeline
|
5 |
-
|
6 |
-
# π§ Must be first Streamlit command
|
7 |
-
st.set_page_config(page_title="Keyword & Summary Bot", page_icon="π§ ")
|
8 |
-
|
9 |
-
# π¦ Load models only once
|
10 |
-
@st.cache_resource
|
11 |
-
def load_models():
|
12 |
-
kw_model = KeyBERT(SentenceTransformer('all-MiniLM-L6-v2'))
|
13 |
-
summarizer = pipeline("summarization", model="
|
14 |
-
return kw_model, summarizer
|
15 |
-
|
16 |
-
kw_model, summarizer = load_models()
|
17 |
-
|
18 |
-
# π§ UI
|
19 |
-
st.title("π€ NLP Assistant: Keyword Extractor & Summarizer")
|
20 |
-
st.write("Welcome! Select a task below and enter your text to get smart results.")
|
21 |
-
|
22 |
-
# π§ Task Selection
|
23 |
-
task = st.selectbox("Choose your task:", ["Select task", "Keyword Extraction", "Text Summarization"])
|
24 |
-
|
25 |
-
# βοΈ User Input
|
26 |
-
user_input = st.text_area("Enter your text here:")
|
27 |
-
|
28 |
-
# π Submit Button
|
29 |
-
if st.button("Submit") and user_input.strip():
|
30 |
-
|
31 |
-
# π Keyword Extraction
|
32 |
-
if task == "Keyword Extraction":
|
33 |
-
keywords = kw_model.extract_keywords(
|
34 |
-
user_input,
|
35 |
-
keyphrase_ngram_range=(1, 2),
|
36 |
-
stop_words='english',
|
37 |
-
top_n=5
|
38 |
-
)
|
39 |
-
keyword_list = [kw[0] for kw in keywords]
|
40 |
-
st.success(f"π Keywords: {', '.join(keyword_list)}")
|
41 |
-
|
42 |
-
# π Text Summarization
|
43 |
-
elif task == "Text Summarization":
|
44 |
-
if len(user_input.split()) < 50:
|
45 |
-
st.warning("β οΈ Enter a longer paragraph (at least 50 words) for better summarization.")
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from keybert import KeyBERT
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# π§ Must be first Streamlit command
|
7 |
+
st.set_page_config(page_title="Keyword & Summary Bot", page_icon="π§ ")
|
8 |
+
|
9 |
+
# π¦ Load models only once
|
10 |
+
@st.cache_resource
|
11 |
+
def load_models():
|
12 |
+
kw_model = KeyBERT(SentenceTransformer('all-MiniLM-L6-v2'))
|
13 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
14 |
+
return kw_model, summarizer
|
15 |
+
|
16 |
+
kw_model, summarizer = load_models()
|
17 |
+
|
18 |
+
# π§ UI
|
19 |
+
st.title("π€ NLP Assistant: Keyword Extractor & Summarizer")
|
20 |
+
st.write("Welcome! Select a task below and enter your text to get smart results.")
|
21 |
+
|
22 |
+
# π§ Task Selection
|
23 |
+
task = st.selectbox("Choose your task:", ["Select task", "Keyword Extraction", "Text Summarization"])
|
24 |
+
|
25 |
+
# βοΈ User Input
|
26 |
+
user_input = st.text_area("Enter your text here:")
|
27 |
+
|
28 |
+
# π Submit Button
|
29 |
+
if st.button("Submit") and user_input.strip():
|
30 |
+
|
31 |
+
# π Keyword Extraction
|
32 |
+
if task == "Keyword Extraction":
|
33 |
+
keywords = kw_model.extract_keywords(
|
34 |
+
user_input,
|
35 |
+
keyphrase_ngram_range=(1, 2),
|
36 |
+
stop_words='english',
|
37 |
+
top_n=5
|
38 |
+
)
|
39 |
+
keyword_list = [kw[0] for kw in keywords]
|
40 |
+
st.success(f"π Keywords: {', '.join(keyword_list)}")
|
41 |
+
|
42 |
+
# π Text Summarization
|
43 |
+
elif task == "Text Summarization":
|
44 |
+
if len(user_input.split()) < 50:
|
45 |
+
st.warning("β οΈ Enter a longer paragraph (at least 50 words) for better summarization.")
|
46 |
+
elif len(user_input.split()) > 500:
|
47 |
+
st.warning("β οΈ Your input is too long. Try to shorten it below 500 words.")
|
48 |
+
else:
|
49 |
+
summary = summarizer(
|
50 |
+
user_input,
|
51 |
+
max_length=100,
|
52 |
+
min_length=30,
|
53 |
+
do_sample=False
|
54 |
+
)
|
55 |
+
st.success(f"π Summary: {summary[0]['summary_text']}")
|
56 |
+
|
57 |
+
else:
|
58 |
+
st.warning("β οΈ Please select a task to perform.")
|