| import streamlit as st |
| from src.document_processor import process_document |
| from src.summarizer import TextSummarizer |
| import logging |
| from textblob import TextBlob |
| import http.server |
| import threading |
| import json |
|
|
| |
| logging.basicConfig(level=logging.DEBUG) |
|
|
| |
| def main(): |
| |
| st.set_page_config( |
| page_title="SumItUp | Document Summarizer", |
| page_icon="βοΈ", |
| layout="wide" |
| ) |
|
|
| st.title("βοΈ SumItUp") |
| st.subheader("Intelligent Document Summarization Made Easy") |
|
|
| if health_check(): |
| return |
|
|
| |
| st.sidebar.header("Summarization Settings") |
| summary_length = st.sidebar.slider( |
| "Summary Length", |
| min_value=100, |
| max_value=400, |
| value=250 |
| ) |
|
|
| |
| tab1, tab2 = st.tabs(["Paste Text", "Upload Document"]) |
|
|
| |
| summarizer = TextSummarizer() |
|
|
| |
| def classify_sentiment(polarity): |
| if polarity > 0: |
| return "Positive π" |
| elif polarity < 0: |
| return "Negative π" |
| else: |
| return "Neutral π" |
|
|
| |
| with tab1: |
| st.header("Direct Text Input") |
| text_input = st.text_area( |
| "Paste your text here:", |
| height=300, |
| help="Enter the text you want to summarize" |
| ) |
|
|
| if st.button("Summarize Text", key="text_summarize"): |
| if text_input: |
| with st.spinner('Generating summary and sentiment analysis...'): |
| try: |
| |
| summary = summarizer.generate_summary( |
| text_input, |
| max_length=summary_length, |
| min_length=summary_length // 2 |
| ) |
| st.subheader("Summary") |
| st.write(summary) |
|
|
| |
| if text_input.strip(): |
| sentiment = TextBlob(text_input).sentiment |
| sentiment_class = classify_sentiment(sentiment.polarity) |
| st.subheader("Sentiment Analysis") |
| st.write(f"Sentiment: {sentiment_class}") |
| st.write(f"Polarity: {sentiment.polarity:.2f} (Range: -1 to 1)") |
| st.write(f"Subjectivity: {sentiment.subjectivity:.2f} (Range: 0 to 1)") |
| else: |
| st.warning("No valid text for sentiment analysis.") |
|
|
| except Exception as e: |
| st.error(f"Summarization failed: {e}") |
| else: |
| st.warning("Please enter some text to summarize.") |
|
|
| |
| with tab2: |
| st.header("Document Upload") |
| uploaded_file = st.file_uploader( |
| "Choose a file", |
| type=['txt', 'pdf', 'docx'], |
| help="Upload a text, PDF, or Word document" |
| ) |
|
|
| if uploaded_file is not None: |
| if st.button("Summarize Document", key="doc_summarize"): |
| with st.spinner('Processing, summarizing, and analyzing sentiment...'): |
| try: |
| |
| document_text = process_document(uploaded_file) |
|
|
| |
| summary = summarizer.generate_summary( |
| document_text, |
| max_length=summary_length, |
| min_length=summary_length // 2 |
| ) |
| st.subheader("Summary") |
| st.write(summary) |
|
|
| |
| if document_text.strip(): |
| sentiment = TextBlob(document_text).sentiment |
| sentiment_class = classify_sentiment(sentiment.polarity) |
| st.subheader("Sentiment Analysis") |
| st.write(f"Sentiment: {sentiment_class}") |
| st.write(f"Polarity: {sentiment.polarity:.2f} (Range: -1 to 1)") |
| st.write(f"Subjectivity: {sentiment.subjectivity:.2f} (Range: 0 to 1)") |
| else: |
| st.warning("No valid text for sentiment analysis.") |
|
|
| except Exception as e: |
| st.error(f"Error processing document: {e}") |
|
|
| def health_check(): |
| """Simple health check endpoint that returns JSON""" |
| params = st.experimental_get_query_params() |
| if 'health' in params and params['health'][0] == 'true': |
| st.write('{"status": "OK"}') |
| st.cache_data.clear() |
| return True |
| return False |
| |
| if __name__ == "__main__": |
| main() |
|
|