File size: 2,324 Bytes
8056289
e1fb94e
 
8056289
e1fb94e
10f3712
 
 
 
e1fb94e
 
10f3712
 
 
 
 
e1fb94e
 
 
 
 
 
 
 
 
 
10f3712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import streamlit as st
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import torch

# Define the summarization pipeline
try:
    summarizer_ntg = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-summarize-news")
except Exception as e:
    st.error(f"Error loading summarization model: {e}")

# Load the tokenizer and model for classification
try:
    tokenizer_bb = AutoTokenizer.from_pretrained("Lauraayu/News_Classi_Model")
    model_bb = AutoModelForSequenceClassification.from_pretrained("Lauraayu/News_Classi_Model")
except Exception as e:
    st.error(f"Error loading classification model or tokenizer: {e}")

# Streamlit application title
st.title("News Article Summarizer and Classifier")
st.write("Enter a news article text to get its summary and category.")

# Text input for user to enter the news article text
text = st.text_area("Enter the news article text here:")

# Perform summarization and classification when the user clicks the "Classify" button
if st.button("Classify"):
    if not text:
        st.error("Please enter some text to classify.")
    else:
        try:
            # Perform text summarization
            summary = summarizer_ntg(text)[0]['summary_text']
            
            # Tokenize the summarized text
            inputs = tokenizer_bb(summary, return_tensors="pt", truncation=True, padding=True, max_length=512)
            
            # Move inputs and model to the same device (GPU or CPU)
            device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
            inputs = {k: v.to(device) for k, v in inputs.items()}
            model_bb.to(device)
            
            # Perform text classification
            with torch.no_grad():
                outputs = model_bb(**inputs)
            
            # Get the predicted label
            predicted_label_id = torch.argmax(outputs.logits, dim=-1).item()
            label_mapping = model_bb.config.id2label
            predicted_label = label_mapping[predicted_label_id]
            
            # Display the summary and classification result
            st.write("Summary:", summary)
            st.write("Category:", predicted_label)
        except Exception as e:
            st.error(f"Error during summarization or classification: {e}")