Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,142 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from src.document_processor import process_document
|
| 3 |
-
from src.summarizer import TextSummarizer
|
| 4 |
-
import logging
|
| 5 |
-
from textblob import TextBlob # Ensure this library is installed
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from src.document_processor import process_document
|
| 3 |
+
from src.summarizer import TextSummarizer
|
| 4 |
+
import logging
|
| 5 |
+
from textblob import TextBlob # Ensure this library is installed
|
| 6 |
+
import http.server
|
| 7 |
+
import threading
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Set up logging
|
| 11 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 12 |
+
|
| 13 |
+
class HealthCheck(http.server.BaseHTTPRequestHandler):
|
| 14 |
+
def do_GET(self):
|
| 15 |
+
if self.path == '/health':
|
| 16 |
+
self.send_response(200)
|
| 17 |
+
self.send_header('Content-type', 'text/plain')
|
| 18 |
+
self.end_headers()
|
| 19 |
+
self.wfile.write(b"OK")
|
| 20 |
+
|
| 21 |
+
def do_HEAD(self):
|
| 22 |
+
if self.path == '/health':
|
| 23 |
+
self.send_response(200)
|
| 24 |
+
self.send_header('Content-type', 'text/plain')
|
| 25 |
+
self.end_headers()
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
threading.Thread(target=lambda: http.server.HTTPServer(('', 8080), HealthCheck).serve_forever(), daemon=True).start()
|
| 29 |
+
# Streamlit app configuration
|
| 30 |
+
st.set_page_config(
|
| 31 |
+
page_title="SumItUp | Document Summarizer",
|
| 32 |
+
page_icon="βοΈ", # Or another icon that represents summarization
|
| 33 |
+
layout="wide"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
st.title("βοΈ SumItUp")
|
| 37 |
+
st.subheader("Intelligent Document Summarization Made Easy")
|
| 38 |
+
|
| 39 |
+
# Sidebar for configuration
|
| 40 |
+
st.sidebar.header("Summarization Settings")
|
| 41 |
+
summary_length = st.sidebar.slider(
|
| 42 |
+
"Summary Length",
|
| 43 |
+
min_value=100,
|
| 44 |
+
max_value=400,
|
| 45 |
+
value=250
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Tabs for different input methods
|
| 49 |
+
tab1, tab2 = st.tabs(["Paste Text", "Upload Document"])
|
| 50 |
+
|
| 51 |
+
# Initialize summarizer
|
| 52 |
+
summarizer = TextSummarizer()
|
| 53 |
+
|
| 54 |
+
# Function to classify sentiment
|
| 55 |
+
def classify_sentiment(polarity):
|
| 56 |
+
if polarity > 0:
|
| 57 |
+
return "Positive π"
|
| 58 |
+
elif polarity < 0:
|
| 59 |
+
return "Negative π"
|
| 60 |
+
else:
|
| 61 |
+
return "Neutral π"
|
| 62 |
+
|
| 63 |
+
# Tab 1: Direct Text Input
|
| 64 |
+
with tab1:
|
| 65 |
+
st.header("Direct Text Input")
|
| 66 |
+
text_input = st.text_area(
|
| 67 |
+
"Paste your text here:",
|
| 68 |
+
height=300,
|
| 69 |
+
help="Enter the text you want to summarize"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if st.button("Summarize Text", key="text_summarize"):
|
| 73 |
+
if text_input:
|
| 74 |
+
with st.spinner('Generating summary and sentiment analysis...'):
|
| 75 |
+
try:
|
| 76 |
+
# Generate summary
|
| 77 |
+
summary = summarizer.generate_summary(
|
| 78 |
+
text_input,
|
| 79 |
+
max_length=summary_length,
|
| 80 |
+
min_length=summary_length // 2 # Optional: set min_length proportionally
|
| 81 |
+
)
|
| 82 |
+
st.subheader("Summary")
|
| 83 |
+
st.write(summary)
|
| 84 |
+
|
| 85 |
+
# Perform sentiment analysis
|
| 86 |
+
if text_input.strip():
|
| 87 |
+
sentiment = TextBlob(text_input).sentiment
|
| 88 |
+
sentiment_class = classify_sentiment(sentiment.polarity)
|
| 89 |
+
st.subheader("Sentiment Analysis")
|
| 90 |
+
st.write(f"Sentiment: {sentiment_class}")
|
| 91 |
+
st.write(f"Polarity: {sentiment.polarity:.2f} (Range: -1 to 1)")
|
| 92 |
+
st.write(f"Subjectivity: {sentiment.subjectivity:.2f} (Range: 0 to 1)")
|
| 93 |
+
else:
|
| 94 |
+
st.warning("No valid text for sentiment analysis.")
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
st.error(f"Summarization failed: {e}")
|
| 98 |
+
else:
|
| 99 |
+
st.warning("Please enter some text to summarize.")
|
| 100 |
+
|
| 101 |
+
# Tab 2: Document Upload
|
| 102 |
+
with tab2:
|
| 103 |
+
st.header("Document Upload")
|
| 104 |
+
uploaded_file = st.file_uploader(
|
| 105 |
+
"Choose a file",
|
| 106 |
+
type=['txt', 'pdf', 'docx'],
|
| 107 |
+
help="Upload a text, PDF, or Word document"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if uploaded_file is not None:
|
| 111 |
+
if st.button("Summarize Document", key="doc_summarize"):
|
| 112 |
+
with st.spinner('Processing, summarizing, and analyzing sentiment...'):
|
| 113 |
+
try:
|
| 114 |
+
# Process document
|
| 115 |
+
document_text = process_document(uploaded_file)
|
| 116 |
+
|
| 117 |
+
# Generate summary
|
| 118 |
+
summary = summarizer.generate_summary(
|
| 119 |
+
document_text,
|
| 120 |
+
max_length=summary_length,
|
| 121 |
+
min_length=summary_length // 2 # Optional: set min_length proportionally
|
| 122 |
+
)
|
| 123 |
+
st.subheader("Summary")
|
| 124 |
+
st.write(summary)
|
| 125 |
+
|
| 126 |
+
# Perform sentiment analysis
|
| 127 |
+
if document_text.strip():
|
| 128 |
+
sentiment = TextBlob(document_text).sentiment
|
| 129 |
+
sentiment_class = classify_sentiment(sentiment.polarity)
|
| 130 |
+
st.subheader("Sentiment Analysis")
|
| 131 |
+
st.write(f"Sentiment: {sentiment_class}")
|
| 132 |
+
st.write(f"Polarity: {sentiment.polarity:.2f} (Range: -1 to 1)")
|
| 133 |
+
st.write(f"Subjectivity: {sentiment.subjectivity:.2f} (Range: 0 to 1)")
|
| 134 |
+
else:
|
| 135 |
+
st.warning("No valid text for sentiment analysis.")
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
st.error(f"Error processing document: {e}")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
main()
|