File size: 2,288 Bytes
4452f46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
from transformers import pipeline
from keybert import KeyBERT

# ----------------------------
# PAGE CONFIG
# ----------------------------
st.set_page_config(page_title="πŸ“ Smart Text Analyzer", layout="wide")
st.title("πŸ“ Smart Text Analyzer App")
st.markdown("Paste your text below and explore summaries, sentiment, and keywords.")

# ----------------------------
# LOAD MODELS
# ----------------------------
@st.cache_resource
def load_models():
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
    sentiment_analyzer = pipeline("sentiment-analysis")
    kw_model = KeyBERT()
    return summarizer, sentiment_analyzer, kw_model

summarizer, sentiment_analyzer, kw_model = load_models()

# ----------------------------
# USER INPUT
# ----------------------------
text_input = st.text_area("Enter your text here:", height=200)
summary_length = st.radio("Select summary length:", ["Short", "Medium", "Long"])

length_map = {"Short": 50, "Medium": 100, "Long": 150}

# ----------------------------
# ACTION
# ----------------------------
if st.button("Analyze Text"):
    if text_input.strip():
        with st.spinner("Processing..."):

            # Summarization
            summary = summarizer(
                text_input,
                max_length=length_map[summary_length],
                min_length=25,
                do_sample=False
            )[0]["summary_text"]

            # Sentiment
            sentiment = sentiment_analyzer(text_input[:512])[0]

            # Keywords
            keywords = kw_model.extract_keywords(text_input, keyphrase_ngram_range=(1,2), stop_words='english', top_n=5)
            keywords = [kw[0] for kw in keywords]

        # ----------------------------
        # OUTPUTS
        # ----------------------------
        st.subheader("πŸ“Œ Summary:")
        st.write(summary)

        st.subheader("😊 Sentiment Analysis:")
        st.write(f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}")

        st.subheader("πŸ”‘ Top Keywords:")
        st.write(", ".join(keywords))

        # Download Option
        st.download_button("πŸ’Ύ Download Summary", data=summary, file_name="summary.txt")

    else:
        st.warning("⚠️ Please enter some text first!")