Spaces:
Sleeping
Sleeping
| 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 | |
| # ---------------------------- | |
| 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!") | |