garvitcpp commited on
Commit
ade2da4
Β·
verified Β·
1 Parent(s): 8199fc6

Update app.py

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