Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from keybert import KeyBERT
|
| 4 |
+
|
| 5 |
+
# ----------------------------
|
| 6 |
+
# PAGE CONFIG
|
| 7 |
+
# ----------------------------
|
| 8 |
+
st.set_page_config(page_title="π Smart Text Analyzer", layout="wide")
|
| 9 |
+
st.title("π Smart Text Analyzer App")
|
| 10 |
+
st.markdown("Paste your text below and explore summaries, sentiment, and keywords.")
|
| 11 |
+
|
| 12 |
+
# ----------------------------
|
| 13 |
+
# LOAD MODELS
|
| 14 |
+
# ----------------------------
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_models():
|
| 17 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 19 |
+
kw_model = KeyBERT()
|
| 20 |
+
return summarizer, sentiment_analyzer, kw_model
|
| 21 |
+
|
| 22 |
+
summarizer, sentiment_analyzer, kw_model = load_models()
|
| 23 |
+
|
| 24 |
+
# ----------------------------
|
| 25 |
+
# USER INPUT
|
| 26 |
+
# ----------------------------
|
| 27 |
+
text_input = st.text_area("Enter your text here:", height=200)
|
| 28 |
+
summary_length = st.radio("Select summary length:", ["Short", "Medium", "Long"])
|
| 29 |
+
|
| 30 |
+
length_map = {"Short": 50, "Medium": 100, "Long": 150}
|
| 31 |
+
|
| 32 |
+
# ----------------------------
|
| 33 |
+
# ACTION
|
| 34 |
+
# ----------------------------
|
| 35 |
+
if st.button("Analyze Text"):
|
| 36 |
+
if text_input.strip():
|
| 37 |
+
with st.spinner("Processing..."):
|
| 38 |
+
|
| 39 |
+
# Summarization
|
| 40 |
+
summary = summarizer(
|
| 41 |
+
text_input,
|
| 42 |
+
max_length=length_map[summary_length],
|
| 43 |
+
min_length=25,
|
| 44 |
+
do_sample=False
|
| 45 |
+
)[0]["summary_text"]
|
| 46 |
+
|
| 47 |
+
# Sentiment
|
| 48 |
+
sentiment = sentiment_analyzer(text_input[:512])[0]
|
| 49 |
+
|
| 50 |
+
# Keywords
|
| 51 |
+
keywords = kw_model.extract_keywords(text_input, keyphrase_ngram_range=(1,2), stop_words='english', top_n=5)
|
| 52 |
+
keywords = [kw[0] for kw in keywords]
|
| 53 |
+
|
| 54 |
+
# ----------------------------
|
| 55 |
+
# OUTPUTS
|
| 56 |
+
# ----------------------------
|
| 57 |
+
st.subheader("π Summary:")
|
| 58 |
+
st.write(summary)
|
| 59 |
+
|
| 60 |
+
st.subheader("π Sentiment Analysis:")
|
| 61 |
+
st.write(f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}")
|
| 62 |
+
|
| 63 |
+
st.subheader("π Top Keywords:")
|
| 64 |
+
st.write(", ".join(keywords))
|
| 65 |
+
|
| 66 |
+
# Download Option
|
| 67 |
+
st.download_button("πΎ Download Summary", data=summary, file_name="summary.txt")
|
| 68 |
+
|
| 69 |
+
else:
|
| 70 |
+
st.warning("β οΈ Please enter some text first!")
|