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