Emily666666
commited on
Commit
•
ae503da
1
Parent(s):
ba067b3
Update app.py
Browse files
app.py
CHANGED
@@ -3,16 +3,20 @@ from transformers import pipeline
|
|
3 |
|
4 |
# Load the text summarization pipeline
|
5 |
try:
|
6 |
-
|
|
|
7 |
except ValueError as e:
|
8 |
st.error(f"Error loading summarization model: {e}")
|
|
|
9 |
|
10 |
# Load the news classification pipeline
|
11 |
-
|
12 |
try:
|
13 |
-
classifier = pipeline("text-classification", model=
|
|
|
14 |
except ValueError as e:
|
15 |
st.error(f"Error loading classification model: {e}")
|
|
|
16 |
|
17 |
# Streamlit app title
|
18 |
st.title("Summarization and News Classification")
|
@@ -24,26 +28,33 @@ with tab1:
|
|
24 |
st.header("Text Summarization")
|
25 |
# Input text for summarization
|
26 |
text_to_summarize = st.text_area("Enter text to summarize:", "")
|
27 |
-
if st.button("Summarize")
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
with tab2:
|
34 |
st.header("News Classification")
|
35 |
# Input text for news classification
|
36 |
text_to_classify = st.text_area("Enter text to classify:", "")
|
37 |
-
if st.button("Classify")
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
max_score
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
3 |
|
4 |
# Load the text summarization pipeline
|
5 |
try:
|
6 |
+
summarizer = pipeline("summarization", model="syndi-models/titlewave-t5-base")
|
7 |
+
summarizer_loaded = True
|
8 |
except ValueError as e:
|
9 |
st.error(f"Error loading summarization model: {e}")
|
10 |
+
summarizer_loaded = False
|
11 |
|
12 |
# Load the news classification pipeline
|
13 |
+
model_name = "elozano/bert-base-cased-news-category"
|
14 |
try:
|
15 |
+
classifier = pipeline("text-classification", model=model_name, return_all_scores=True)
|
16 |
+
classifier_loaded = True
|
17 |
except ValueError as e:
|
18 |
st.error(f"Error loading classification model: {e}")
|
19 |
+
classifier_loaded = False
|
20 |
|
21 |
# Streamlit app title
|
22 |
st.title("Summarization and News Classification")
|
|
|
28 |
st.header("Text Summarization")
|
29 |
# Input text for summarization
|
30 |
text_to_summarize = st.text_area("Enter text to summarize:", "")
|
31 |
+
if st.button("Summarize"):
|
32 |
+
if summarizer_loaded and text_to_summarize:
|
33 |
+
try:
|
34 |
+
# Perform text summarization
|
35 |
+
summary = summarizer(text_to_summarize, max_length=130, min_length=30, do_sample=False)
|
36 |
+
# Display the summary result
|
37 |
+
st.write("Summary:", summary[0]['summary_text'])
|
38 |
+
except Exception as e:
|
39 |
+
st.error(f"Error during summarization: {e}")
|
40 |
+
else:
|
41 |
+
st.warning("Please enter text to summarize and ensure the model is loaded.")
|
42 |
|
43 |
with tab2:
|
44 |
st.header("News Classification")
|
45 |
# Input text for news classification
|
46 |
text_to_classify = st.text_area("Enter text to classify:", "")
|
47 |
+
if st.button("Classify"):
|
48 |
+
if classifier_loaded and text_to_classify:
|
49 |
+
try:
|
50 |
+
# Perform news classification
|
51 |
+
results = classifier(text_to_classify)[0]
|
52 |
+
# Find the category with the highest score
|
53 |
+
max_score = max(results, key=lambda x: x['score'])
|
54 |
+
st.write("Text:", text_to_classify)
|
55 |
+
st.write("Category:", max_score['label'])
|
56 |
+
st.write("Score:", max_score['score'])
|
57 |
+
except Exception as e:
|
58 |
+
st.error(f"Error during classification: {e}")
|
59 |
+
else:
|
60 |
+
st.warning("Please enter text to classify and ensure the model is loaded.")
|