Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,43 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
st.write("Enter a news article text to get its category:")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Perform
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Define the summarization pipeline
|
| 6 |
+
summarizer_ntg = pipeline("summarization", model="mrm8488/t5-base-finetuned-summarize-news")
|
| 7 |
+
|
| 8 |
+
# Load the tokenizer and model for classification
|
| 9 |
+
tokenizer_bb = AutoTokenizer.from_pretrained("your-username/your-model-name")
|
| 10 |
+
model_bb = AutoModelForSequenceClassification.from_pretrained("your-username/your-model-name")
|
| 11 |
+
|
| 12 |
+
# Streamlit application title
|
| 13 |
+
st.title("News Article Summarizer and Classifier")
|
| 14 |
+
st.write("Enter a news article text to get its summary and category.")
|
| 15 |
+
|
| 16 |
+
# Text input for user to enter the news article text
|
| 17 |
+
text = st.text_area("Enter the news article text here:")
|
| 18 |
+
|
| 19 |
+
# Perform summarization and classification when the user clicks the "Classify" button
|
| 20 |
+
if st.button("Classify"):
|
| 21 |
+
# Perform text summarization
|
| 22 |
+
summary = summarizer_ntg(text)[0]['summary_text']
|
| 23 |
|
| 24 |
+
# Tokenize the summarized text
|
| 25 |
+
inputs = tokenizer_bb(summary, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
|
|
|
| 26 |
|
| 27 |
+
# Move inputs and model to the same device (GPU or CPU)
|
| 28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 30 |
+
model_bb.to(device)
|
| 31 |
|
| 32 |
+
# Perform text classification
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model_bb(**inputs)
|
| 35 |
+
|
| 36 |
+
# Get the predicted label
|
| 37 |
+
predicted_label_id = torch.argmax(outputs.logits, dim=-1).item()
|
| 38 |
+
label_mapping = model_bb.config.id2label
|
| 39 |
+
predicted_label = label_mapping[predicted_label_id]
|
| 40 |
+
|
| 41 |
+
# Display the summary and classification result
|
| 42 |
+
st.write("Summary:", summary)
|
| 43 |
+
st.write("Category:", predicted_label)
|