File size: 13,701 Bytes
6880cd9 6324ab5 6880cd9 |
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 |
import streamlit as st
import requests
import json
import time
from typing import Dict, Any, Optional
import io
# Page configuration
st.set_page_config(
page_title="Book Summarizer AI",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
# API configuration
API_BASE_URL = "http://localhost:8000"
def main():
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 3rem;
font-weight: bold;
text-align: center;
color: #1f77b4;
margin-bottom: 2rem;
}
.sub-header {
font-size: 1.5rem;
color: #666;
text-align: center;
margin-bottom: 2rem;
}
.success-box {
background-color: #d4edda;
border: 1px solid #c3e6cb;
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;
}
.info-box {
background-color: #d1ecf1;
border: 1px solid #bee5eb;
border-radius: 5px;
padding: 1rem;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Header
st.markdown('<h1 class="main-header">π Book Summarizer AI</h1>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">Transform your PDF books into intelligent summaries using AI</p>', unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.header("βοΈ Settings")
# Model selection
st.subheader("AI Model")
try:
models_response = requests.get(f"{API_BASE_URL}/models")
if models_response.status_code == 200:
models_data = models_response.json()
models = models_data.get('models', [])
current_model = models_data.get('current_model', '')
model_names = [model['name'] for model in models]
selected_model = st.selectbox(
"Choose AI Model",
model_names,
index=model_names.index(current_model) if current_model in model_names else 0
)
# Show model description
selected_model_info = next((m for m in models if m['name'] == selected_model), None)
if selected_model_info:
st.info(f"**{selected_model_info['description']}**")
else:
st.error("Failed to load models")
selected_model = "facebook/bart-large-cnn"
except Exception as e:
st.error(f"Error loading models: {str(e)}")
selected_model = "facebook/bart-large-cnn"
# Summary settings
st.subheader("Summary Settings")
max_length = st.slider("Maximum Summary Length", 50, 500, 150, help="Maximum number of words in the summary")
min_length = st.slider("Minimum Summary Length", 10, 200, 50, help="Minimum number of words in the summary")
# Advanced settings
with st.expander("Advanced Settings"):
chunk_size = st.slider("Chunk Size", 500, 2000, 1000, help="Size of text chunks for processing")
overlap = st.slider("Chunk Overlap", 50, 200, 100, help="Overlap between text chunks")
# API status
st.subheader("API Status")
try:
health_response = requests.get(f"{API_BASE_URL}/health")
if health_response.status_code == 200:
st.success("β
API Connected")
else:
st.error("β API Error")
except:
st.error("β API Unavailable")
# Main content
tab1, tab2, tab3 = st.tabs(["π Summarize Book", "π Text Analysis", "βΉοΈ About"])
with tab1:
st.header("π Book Summarization")
# File upload
uploaded_file = st.file_uploader(
"Choose a PDF book file",
type=['pdf'],
help="Upload a PDF file (max 50MB)"
)
if uploaded_file is not None:
# File info
file_size = len(uploaded_file.getvalue()) / (1024 * 1024) # MB
st.info(f"π **File:** {uploaded_file.name} ({file_size:.1f} MB)")
# Validate file
if st.button("π Validate PDF", type="secondary"):
with st.spinner("Validating PDF..."):
try:
files = {"file": uploaded_file.getvalue()}
response = requests.post(f"{API_BASE_URL}/upload-pdf", files=files)
if response.status_code == 200:
data = response.json()
st.success(f"β
{data['message']}")
# Display metadata
metadata = data.get('metadata', {})
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Pages", data['pages'])
with col2:
st.metric("Size", f"{data['size_mb']:.1f} MB")
with col3:
st.metric("Title", metadata.get('title', 'Unknown'))
else:
st.error(f"β Validation failed: {response.json().get('detail', 'Unknown error')}")
except Exception as e:
st.error(f"β Error: {str(e)}")
# Summarize button
if st.button("π Generate Summary", type="primary"):
if uploaded_file is not None:
with st.spinner("Processing your book..."):
try:
# Prepare request
files = {"file": uploaded_file.getvalue()}
data = {
"max_length": max_length,
"min_length": min_length,
"chunk_size": chunk_size,
"overlap": overlap,
"model_name": selected_model
}
# Send request
response = requests.post(f"{API_BASE_URL}/summarize", files=files, data=data)
if response.status_code == 200:
result = response.json()
# Display success message
st.success("β
Summary generated successfully!")
# Display statistics
col1, col2, col3, col4 = st.columns(4)
stats = result.get('statistics', {})
orig_stats = result.get('original_statistics', {})
with col1:
st.metric("Original Words", f"{orig_stats.get('total_words', 0):,}")
with col2:
st.metric("Summary Words", f"{stats.get('final_summary_length', 0):,}")
with col3:
compression = stats.get('overall_compression_ratio', 0)
st.metric("Compression", f"{compression:.1%}")
with col4:
st.metric("Chunks Processed", stats.get('total_chunks', 0))
# Display summary
st.subheader("π Generated Summary")
summary = result.get('summary', '')
st.text_area(
"Summary",
value=summary,
height=400,
disabled=True
)
# Download button
summary_bytes = summary.encode('utf-8')
st.download_button(
label="π₯ Download Summary",
data=summary_bytes,
file_name=f"{uploaded_file.name.replace('.pdf', '')}_summary.txt",
mime="text/plain"
)
else:
error_msg = response.json().get('detail', 'Unknown error')
st.error(f"β Summarization failed: {error_msg}")
except Exception as e:
st.error(f"β Error: {str(e)}")
with tab2:
st.header("π Text Analysis")
if uploaded_file is not None:
if st.button("π Analyze Text"):
with st.spinner("Analyzing text..."):
try:
files = {"file": uploaded_file.getvalue()}
response = requests.post(f"{API_BASE_URL}/extract-text", files=files)
if response.status_code == 200:
data = response.json()
stats = data.get('statistics', {})
# Display statistics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Words", f"{stats.get('total_words', 0):,}")
with col2:
st.metric("Total Sentences", f"{stats.get('total_sentences', 0):,}")
with col3:
st.metric("Avg Words/Sentence", f"{stats.get('average_words_per_sentence', 0):.1f}")
with col4:
st.metric("Reading Time", f"{stats.get('estimated_reading_time_minutes', 0):.1f} min")
# Text preview
st.subheader("π Text Preview")
text_response = requests.post(f"{API_BASE_URL}/extract-text", files=files)
if text_response.status_code == 200:
text_data = text_response.json()
preview_text = text_data.get('text', '')[:1000] + "..." if len(text_data.get('text', '')) > 1000 else text_data.get('text', '')
st.text_area("First 1000 characters:", value=preview_text, height=200, disabled=True)
else:
st.error(f"β Analysis failed: {response.json().get('detail', 'Unknown error')}")
except Exception as e:
st.error(f"β Error: {str(e)}")
else:
st.info("π Please upload a PDF file to analyze its text.")
with tab3:
st.header("βΉοΈ About")
st.markdown("""
## π€ Book Summarizer AI
This application uses advanced AI models to automatically summarize PDF books.
It processes the text in chunks and generates comprehensive summaries while
maintaining the key information and context.
### β¨ Features
- **PDF Text Extraction**: Advanced PDF processing with fallback methods
- **AI Summarization**: State-of-the-art transformer models
- **Configurable Settings**: Adjust summary length and processing parameters
- **Multiple Models**: Choose from different AI models for various use cases
- **Text Analysis**: Detailed statistics about the book content
### π οΈ Technology Stack
- **Frontend**: Streamlit
- **Backend**: FastAPI
- **AI Models**: Hugging Face Transformers (BART, T5)
- **PDF Processing**: PyPDF2, pdfplumber
- **Text Processing**: NLTK
### π How It Works
1. **Upload**: Select a PDF book file (max 50MB)
2. **Extract**: The system extracts and cleans text from the PDF
3. **Chunk**: Large texts are split into manageable chunks
4. **Summarize**: AI models process each chunk and generate summaries
5. **Combine**: Individual summaries are combined into a final summary
6. **Download**: Get your summary in text format
### π Getting Started
1. Make sure the API server is running (`uvicorn api.main:app --reload`)
2. Upload a PDF book file
3. Configure your preferred settings
4. Click "Generate Summary" and wait for processing
5. Download your AI-generated summary
### π Support
For issues or questions, please check the API documentation at `/docs`
when the server is running.
""")
if __name__ == "__main__":
main() |