-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
767 lines (620 loc) · 23.5 KB
/
app2.py
File metadata and controls
767 lines (620 loc) · 23.5 KB
1
2
3
4
5
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
"""
Streamlit Web Interface for AI Document Summarizer
Professional web application with drag-and-drop functionality
"""
import streamlit as st
import os
import time
import json
from pathlib import Path
import tempfile
from typing import Dict, Any, List
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from transformers import pipeline
# Import our backend components
from src.chatbot import load_qa_pipeline, answer_question
from src.document_loader import DocumentLoader
from src.text_processor import TextProcessor
from src.summarizer import DocumentSummarizer, GENSIM_AVAILABLE
qa_model = load_qa_pipeline()
def chatbot_interface(document_text: str):
if "chat_history" not in st.session_state:
st.session_state["chat_history"] = []
st.markdown("---")
st.markdown("### 💬 Chat with your document")
# Show past chats
for speaker, message in st.session_state["chat_history"]:
if speaker == "User":
st.markdown(f"**You:** {message}")
else:
st.markdown(f"**Bot:** {message}")
with st.form(key="chat_form", clear_on_submit=True):
user_question = st.text_input("Ask a question about your document:")
submitted = st.form_submit_button("Send")
if submitted and user_question:
st.write(f"Question received: {user_question}")
st.write(f"Context length: {len(document_text)}")
st.session_state["chat_history"].append(("User", user_question))
try:
answer = answer_question(qa_model, user_question, document_text)
response = answer or "Sorry, I couldn't find an answer."
except Exception as e:
st.error(f"Error in QA model: {e}")
response = "Sorry, something went wrong processing your question."
st.session_state["chat_history"].append(("Bot", response))
st.experimental_rerun() # Refresh UI to show new messages
# Page configuration
st.set_page_config(
page_title="AI Document Summarizer",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded",
)
# Custom CSS for better styling
st.markdown(
"""
<style>
.main-header {
font-size: 2.5rem;
font-weight: bold;
color: #1f77b4;
text-align: center;
margin-bottom: 2rem;
}
.sub-header {
font-size: 1.5rem;
color: #2c3e50;
margin: 1rem 0;
}
.metric-card {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
padding: 1rem;
border-radius: 10px;
color: white;
text-align: center;
margin: 0.5rem 0;
}
.success-box {
background-color: #d4edda;
border: 1px solid #c3e6cb;
border-radius: 5px;
padding: 1rem;
margin: 1rem 0;
}
.warning-box {
background-color: #fff3cd;
border: 1px solid #ffeaa7;
border-radius: 5px;
padding: 1rem;
margin: 1rem 0;
}
.error-box {
background-color: #f8d7da;
border: 1px solid #f5c6cb;
border-radius: 5px;
padding: 1rem;
margin: 1rem 0;
}
</style>
""",
unsafe_allow_html=True,
)
@st.cache_resource
def initialize_components():
"""Initialize and cache the backend components"""
with st.spinner("🔧 Initializing AI components..."):
try:
loader = DocumentLoader()
processor = TextProcessor()
summarizer = DocumentSummarizer()
return {
"loader": loader,
"processor": processor,
"summarizer": summarizer,
"status": "success",
"message": "All components initialized successfully!",
}
except Exception as e:
return {
"loader": None,
"processor": None,
"summarizer": None,
"status": "error",
"message": f"Initialization failed: {str(e)}",
}
def main():
"""Main application function"""
# Header
st.markdown(
'<h1 class="main-header">🤖 AI Document Summarizer</h1>', unsafe_allow_html=True
)
st.markdown(
'<p style="text-align: center; font-size: 1.2rem; color: #7f8c8d;">Transform any document into intelligent summaries using advanced AI</p>',
unsafe_allow_html=True,
)
# Initialize components
components = initialize_components()
if components["status"] == "error":
st.error(f"❌ {components['message']}")
st.stop()
# Success message
st.success(f"✅ {components['message']}")
# Sidebar configuration
with st.sidebar:
st.markdown("## ⚙️ Configuration")
# Method selection
st.markdown("### 🤖 Summarization Methods")
available_methods = ["transformer", "textrank", "lsa", "luhn"]
if GENSIM_AVAILABLE:
available_methods.append("gensim")
selected_methods = st.multiselect(
"Choose methods:",
available_methods,
default=["transformer", "textrank", "lsa"],
help="Select one or more summarization methods to use",
)
# Parameters
st.markdown("### 📏 Summary Parameters")
max_length = st.slider(
"Maximum length (words)",
min_value=50,
max_value=300,
value=150,
step=25,
help="Maximum number of words in the summary",
)
min_length = st.slider(
"Minimum length (words)",
min_value=20,
max_value=100,
value=50,
step=10,
help="Minimum number of words in the summary",
)
sentence_count = st.slider(
"Sentences (for traditional methods)",
min_value=1,
max_value=10,
value=3,
help="Number of sentences for TextRank, LSA, etc.",
)
# Advanced settings
with st.expander("🔧 Advanced Settings"):
word_count = st.slider("Gensim word count", 50, 200, 100)
show_details = st.checkbox("Show detailed analysis", value=True)
show_performance = st.checkbox("Show performance metrics", value=True)
# Main content area
tab1, tab2, tab3, tab4 = st.tabs(
["📄 Document Upload", "📊 Results", "📈 Analytics", "ℹ️ About"]
)
with tab1:
document_upload_section(
components,
selected_methods,
{
"max_length": max_length,
"min_length": min_length,
"sentence_count": sentence_count,
"word_count": word_count,
"show_details": show_details,
"show_performance": show_performance,
},
)
with tab2:
results_section()
with tab3:
analytics_section()
with tab4:
about_section(components)
def document_upload_section(
components: Dict[str, Any], methods: List[str], params: Dict[str, Any]
):
"""Document upload and processing section"""
st.markdown(
'<h2 class="sub-header">📁 Upload Your Document</h2>', unsafe_allow_html=True
)
# File uploader
uploaded_file = st.file_uploader(
"Choose a document file",
type=["pdf", "docx", "txt", "xlsx", "pptx", "html", "md"],
help="Supported formats: PDF, Word, Text, Excel, PowerPoint, HTML, Markdown",
)
if uploaded_file is not None:
# Display file info
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("📄 File Name", uploaded_file.name)
with col2:
st.metric("📏 File Size", f"{uploaded_file.size:,} bytes")
with col3:
st.metric("📋 File Type", uploaded_file.type)
with col4:
st.metric("🔧 Methods", len(methods))
# Process button
if st.button("🚀 Process Document", type="primary"):
if not methods:
st.error("❌ Please select at least one summarization method")
return
process_document(uploaded_file, components, methods, params)
else:
# Show supported formats
st.markdown("### 📋 Supported File Formats")
formats_data = {
"Format": [
"PDF",
"Word",
"Excel",
"PowerPoint",
"HTML",
"Text",
"Markdown",
],
"Extension": [".pdf", ".docx", ".xlsx", ".pptx", ".html", ".txt", ".md"],
"Description": [
"Documents, reports, books",
"Text documents, letters",
"Spreadsheets, data tables",
"Presentations, slides",
"Web pages, formatted text",
"Plain text files",
"Formatted text with markup",
],
}
df = pd.DataFrame(formats_data)
st.dataframe(df, use_container_width=True)
def process_document(
uploaded_file,
components: Dict[str, Any],
methods: List[str],
params: Dict[str, Any],
):
"""Process the uploaded document"""
# Create progress bar
progress_bar = st.progress(0)
status_text = st.empty()
try:
# Step 1: Save uploaded file temporarily
status_text.text("📥 Saving uploaded file...")
progress_bar.progress(10)
with tempfile.NamedTemporaryFile(
delete=False, suffix=f".{uploaded_file.name.split('.')[-1]}"
) as tmp_file:
tmp_file.write(uploaded_file.getvalue())
tmp_file_path = tmp_file.name
# Step 2: Load document
status_text.text("📄 Loading document content...")
progress_bar.progress(25)
load_result = components["loader"].load_document(tmp_file_path)
if not load_result["success"]:
st.error(f"❌ Failed to load document: {load_result['error']}")
return
# Step 3: Process text
status_text.text("🔧 Processing text...")
progress_bar.progress(50)
processing_result = components["processor"].prepare_for_summarization(
load_result["text"]
)
# Step 4: Generate summaries
status_text.text("🤖 Generating AI summaries...")
progress_bar.progress(75)
summary_result = components["summarizer"].summarize_document(
processing_result["cleaned_text"],
methods=methods,
max_length=params["max_length"],
min_length=params["min_length"],
sentence_count=params["sentence_count"],
word_count=params["word_count"],
)
# Step 5: Complete
status_text.text("✅ Processing complete!")
progress_bar.progress(100)
# Store results in session state
st.session_state["results"] = {
"load_result": load_result,
"processing_result": {
"cleaned_text": processing_result.get(
"cleaned_text", ""
) # important for chatbot context
# You can add other processing details here if needed
},
"summary_result": summary_result,
"file_name": uploaded_file.name,
"methods_used": methods,
"parameters": params,
}
# Show immediate results
show_results_preview(st.session_state["results"])
# --- Begin chatbot interface ---
document_text = processing_result.get("cleaned_text", "")
if "chat_history" not in st.session_state:
st.session_state["chat_history"] = []
# Text input box for user question
# question = st.text_input(
# "Ask a question about your document:", key="chat_input"
# )
# if st.button("Send", key="chat_send") and question:
from transformers import pipeline
@st.cache_resource(show_spinner=False)
def load_qa_model():
return pipeline("question-answering")
qa_model = load_qa_model()
# Add user question to chat history
st.session_state["chat_history"].append(("User", question))
try:
# Get answer from QA model
answer = qa_model(question=question, context=document_text)
response = answer.get("answer", "Sorry, I couldn't find an answer.")
except Exception:
response = "Sorry, something went wrong processing your question."
# Add bot response to chat history
st.session_state["chat_history"].append(("Bot", response))
# Display chat history messages
st.markdown("---")
st.markdown("### 💬 Chat with your document")
for speaker, message in st.session_state["chat_history"]:
if speaker == "User":
st.markdown(f"**You:** {message}")
else:
st.markdown(f"**Bot:** {message}")
# --- End chatbot interface ---
# Cleanup
os.unlink(tmp_file_path)
except Exception as e:
st.error(f"❌ Processing failed: {str(e)}")
status_text.text("❌ Processing failed")
# Cleanup on error
if "tmp_file_path" in locals():
try:
os.unlink(tmp_file_path)
except:
pass
def show_results_preview(results: Dict[str, Any]):
"""Show a preview of processing results"""
st.markdown("---")
st.markdown(
'<h2 class="sub-header">📊 Processing Results</h2>', unsafe_allow_html=True
)
# Success metrics
summary_result = results["summary_result"]
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric(
label="⏱️ Processing Time",
value=f"{summary_result['overall_performance']['total_processing_time']:.2f}s",
)
with col2:
st.metric(
label="✅ Success Rate",
value=f"{summary_result['overall_performance']['methods_successful']}/{summary_result['overall_performance']['methods_attempted']}",
)
with col3:
st.metric(
label="🎯 Avg Quality",
value=f"{summary_result['overall_performance']['average_quality_score']:.2f}",
)
with col4:
st.metric(
label="🏆 Best Method",
value=summary_result["recommended"]["method"].title(),
)
# Best summary
st.markdown("### 🏆 Recommended Summary")
recommended = summary_result["recommended"]
st.markdown(
f"""
<div class="success-box">
<strong>Method:</strong> {recommended['method'].title()}<br>
<strong>Quality Score:</strong> {recommended['quality_score']:.2f}<br>
<strong>Confidence:</strong> {recommended['confidence']:.2f}<br><br>
<strong>Summary:</strong><br>
{recommended['summary']}
</div>
""",
unsafe_allow_html=True,
)
# Method comparison
st.markdown("### 📊 Method Comparison")
method_data = []
for method, result in summary_result["summaries"].items():
method_data.append(
{
"Method": method.title(),
"Success": "✅" if result["success"] else "❌",
"Quality": result.get("quality_score", 0),
"Time (s)": result.get("processing_time", 0),
"Words": result.get("word_count", 0),
}
)
df_methods = pd.DataFrame(method_data)
st.dataframe(df_methods, use_container_width=True)
def results_section():
"""Detailed results section"""
if "results" not in st.session_state:
st.info("📝 Upload and process a document to see detailed results here.")
return
results = st.session_state["results"]
st.markdown(
'<h2 class="sub-header">📊 Detailed Analysis</h2>', unsafe_allow_html=True
)
# Document analysis
st.markdown("### 📄 Document Analysis")
input_analysis = results["summary_result"]["input_analysis"]
col1, col2 = st.columns(2)
with col1:
st.markdown("**Document Statistics:**")
st.write(
f"• **Original Length:** {input_analysis['original_length']:,} characters"
)
st.write(f"• **Word Count:** {input_analysis['word_count']:,} words")
st.write(f"• **Sentences:** {input_analysis['sentence_count']:,}")
st.write(f"• **Language:** {input_analysis['language']}")
st.write(f"• **Complexity:** {input_analysis['complexity']}")
with col2:
st.markdown("**Processing Quality:**")
readiness = input_analysis["readiness_score"]
st.write(f"• **Readiness Score:** {readiness:.2f}")
if readiness >= 0.8:
st.success("🎉 Excellent text quality for summarization!")
elif readiness >= 0.6:
st.warning("⚠️ Good text quality with minor issues")
else:
st.error("❌ Text quality needs improvement")
# Individual summaries
st.markdown("### 📝 All Generated Summaries")
for method, result in results["summary_result"]["summaries"].items():
if result["success"]:
with st.expander(
f"📋 {method.title()} Summary (Quality: {result['quality_score']:.2f})"
):
st.write(result["summary"])
# Detailed metrics
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Word Count", result.get("word_count", 0))
with col2:
st.metric(
"Processing Time", f"{result.get('processing_time', 0):.2f}s"
)
with col3:
st.metric(
"Compression Ratio", f"{result.get('compression_ratio', 0):.3f}"
)
else:
with st.expander(f"❌ {method.title()} (Failed)"):
st.error(result["summary"])
document_text = results.get("processing_result", {}).get("cleaned_text", "")
if document_text:
chatbot_interface(document_text)
def analytics_section():
"""Analytics and visualizations section"""
if "results" not in st.session_state:
st.info("📈 Process a document to see analytics and visualizations here.")
return
results = st.session_state["results"]
summary_result = results["summary_result"]
st.markdown(
'<h2 class="sub-header">📈 Performance Analytics</h2>', unsafe_allow_html=True
)
# Quality comparison chart
st.markdown("### 🎯 Quality Comparison")
quality_data = []
for method, result in summary_result["summaries"].items():
if result["success"]:
quality_data.append(
{
"Method": method.title(),
"Quality Score": result["quality_score"],
"Processing Time": result["processing_time"],
}
)
if quality_data:
df_quality = pd.DataFrame(quality_data)
fig = px.bar(
df_quality,
x="Method",
y="Quality Score",
title="Summary Quality by Method",
color="Quality Score",
color_continuous_scale="viridis",
)
st.plotly_chart(fig, use_container_width=True)
# Processing time comparison
st.markdown("### ⏱️ Processing Time Analysis")
fig2 = px.scatter(
df_quality,
x="Processing Time",
y="Quality Score",
size=[1] * len(df_quality),
hover_name="Method",
title="Quality vs Processing Time",
labels={"Processing Time": "Processing Time (seconds)"},
)
st.plotly_chart(fig2, use_container_width=True)
# Performance metrics
st.markdown("### 📊 Performance Metrics")
performance = summary_result["performance"]
perf_data = []
for method, perf in performance.items():
perf_data.append(
{
"Method": method.title(),
"Success": perf["success"],
"Time (s)": perf["processing_time"],
"Words/sec": perf["words_per_second"],
}
)
df_perf = pd.DataFrame(perf_data)
st.dataframe(df_perf, use_container_width=True)
# Recommendations
st.markdown("### 💡 System Recommendations")
for i, rec in enumerate(summary_result["recommendations"], 1):
st.write(f"{i}. {rec}")
def about_section(components: Dict[str, Any]):
"""About section with system information"""
st.markdown(
'<h2 class="sub-header">ℹ️ About This Application</h2>', unsafe_allow_html=True
)
st.markdown(
"""
### 🤖 AI Document Summarizer
This application uses advanced artificial intelligence and natural language processing
to automatically generate high-quality summaries from various document formats.
**Key Features:**
- 🤖 **AI-Powered**: Uses transformer models like BART for state-of-the-art summarization
- 📊 **Multiple Methods**: Combines AI with traditional algorithms (TextRank, LSA)
- 📄 **Multi-Format**: Supports PDF, Word, Excel, PowerPoint, HTML, Text, Markdown
- 🎯 **Quality Assessment**: Intelligent scoring and method comparison
- ⚡ **Fast Processing**: Optimized for performance and efficiency
- 🔧 **Customizable**: Adjustable parameters for different use cases
"""
)
# System information
st.markdown("### 🔧 System Information")
if components["summarizer"]:
perf_stats = components["summarizer"].get_performance_stats()
col1, col2 = st.columns(2)
with col1:
st.markdown("**AI Model Information:**")
st.write(f"• **Current Model:** {perf_stats['current_model']}")
st.write(f"• **Device:** {perf_stats['device'].upper()}")
st.write(f"• **Available Models:** {len(perf_stats['available_models'])}")
with col2:
st.markdown("**Processing Statistics:**")
st.write(
f"• **Total Summaries:** {perf_stats['total_summaries_processed']}"
)
st.write(f"• **Success Rate:** {perf_stats['success_rate']:.1%}")
st.write(
f"• **Avg Processing Time:** {perf_stats['average_processing_time']:.2f}s"
)
# Supported formats
st.markdown("### 📋 Supported File Formats")
if components["loader"]:
formats = components["loader"].get_supported_formats()
format_descriptions = {
fmt: components["loader"].get_format_description(fmt) for fmt in formats
}
for fmt, desc in format_descriptions.items():
st.write(f"• **{fmt.upper()}**: {desc}")
# Technical details
with st.expander("🔬 Technical Details"):
st.markdown(
"""
**Summarization Methods:**
- **Transformer (BART)**: Facebook's BART model for abstractive summarization
- **TextRank**: Graph-based extractive summarization algorithm
- **LSA**: Latent Semantic Analysis for topic-based summarization
- **Luhn**: Frequency-based summarization algorithm
- **Gensim**: Statistical summarization using word frequency analysis
**Technologies Used:**
- **Streamlit**: Web application framework
- **Transformers**: Hugging Face transformer models
- **NLTK & spaCy**: Natural language processing
- **PyTorch**: Deep learning framework
- **Plotly**: Interactive visualizations
"""
)
if __name__ == "__main__":
main()