shivam701171 commited on
Commit
e297f25
Β·
verified Β·
1 Parent(s): 3e8529c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +368 -0
app.py ADDED
@@ -0,0 +1,368 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # AI DOCUMENT SUMMARIZER - BULLETPROOF HF SPACES VERSION
3
+ # This version handles all dependency issues gracefully
4
+ # =============================================================================
5
+
6
+ import gradio as gr
7
+ import sys
8
+ import time
9
+ import warnings
10
+ from typing import Tuple, Optional
11
+
12
+ print("πŸš€ Starting AI Document Summarizer...")
13
+ print(f"Python version: {sys.version}")
14
+
15
+ # Handle dependencies with proper error messages
16
+ dependencies_available = True
17
+ error_messages = []
18
+
19
+ try:
20
+ import torch
21
+ print("βœ… PyTorch imported successfully")
22
+ TORCH_AVAILABLE = True
23
+ device_info = f"πŸ–₯️ Device: {'GPU' if torch.cuda.is_available() else 'CPU'}"
24
+ except ImportError as e:
25
+ print(f"❌ PyTorch import failed: {e}")
26
+ TORCH_AVAILABLE = False
27
+ dependencies_available = False
28
+ error_messages.append("PyTorch not found")
29
+
30
+ try:
31
+ from transformers import pipeline
32
+ print("βœ… Transformers imported successfully")
33
+ TRANSFORMERS_AVAILABLE = True
34
+ except ImportError as e:
35
+ print(f"❌ Transformers import failed: {e}")
36
+ TRANSFORMERS_AVAILABLE = False
37
+ dependencies_available = False
38
+ error_messages.append("Transformers not found")
39
+
40
+ # Suppress warnings if libraries are available
41
+ if dependencies_available:
42
+ warnings.filterwarnings("ignore")
43
+
44
+ class DocumentSummarizer:
45
+ """Robust document summarizer with dependency checking"""
46
+
47
+ def __init__(self):
48
+ self.models = {}
49
+ self.available = dependencies_available
50
+
51
+ if not self.available:
52
+ print("❌ Dependencies not available - running in demo mode")
53
+ return
54
+
55
+ self.configs = {
56
+ "BART": {
57
+ "model_id": "facebook/bart-large-cnn",
58
+ "description": "πŸ“° Great for news and general text"
59
+ },
60
+ "T5-Small": {
61
+ "model_id": "t5-small",
62
+ "description": "⚑ Fastest processing"
63
+ },
64
+ "DistilBART": {
65
+ "model_id": "sshleifer/distilbart-cnn-12-6",
66
+ "description": "πŸš€ Lightweight and fast"
67
+ }
68
+ }
69
+ print("βœ… Document Summarizer initialized!")
70
+
71
+ def load_model(self, model_name: str):
72
+ """Load model with comprehensive error handling"""
73
+ if not self.available:
74
+ return None
75
+
76
+ if model_name not in self.models:
77
+ try:
78
+ config = self.configs.get(model_name, self.configs["T5-Small"])
79
+ print(f"πŸ”„ Loading {model_name}...")
80
+
81
+ model = pipeline(
82
+ "summarization",
83
+ model=config["model_id"],
84
+ device=-1, # Force CPU for HF Spaces stability
85
+ return_tensors="pt"
86
+ )
87
+
88
+ self.models[model_name] = model
89
+ print(f"βœ… {model_name} loaded successfully!")
90
+ return model
91
+
92
+ except Exception as e:
93
+ print(f"❌ Failed to load {model_name}: {str(e)}")
94
+ return None
95
+
96
+ return self.models[model_name]
97
+
98
+ def summarize(self, text: str, model_name: str, max_length: int, min_length: int) -> Tuple[str, str, str, str]:
99
+ """Generate summary or return appropriate error message"""
100
+
101
+ # Check if dependencies are available
102
+ if not self.available:
103
+ error_msg = f"""
104
+ ❌ **Dependencies Missing**
105
+
106
+ The following packages are not installed:
107
+ {', '.join(error_messages)}
108
+
109
+ **To fix this issue:**
110
+ 1. Make sure your `requirements.txt` contains:
111
+ ```
112
+ torch
113
+ transformers
114
+ gradio
115
+ accelerate
116
+ sentencepiece
117
+ ```
118
+
119
+ 2. Or try the corrected requirements.txt below.
120
+
121
+ This is likely a Hugging Face Spaces configuration issue.
122
+ """
123
+ return error_msg, "Please check the requirements.txt file", "", ""
124
+
125
+ # Input validation
126
+ if not text or not text.strip():
127
+ return "⚠️ Please enter some text to summarize.", "", "", ""
128
+
129
+ text = text.strip()
130
+ word_count = len(text.split())
131
+
132
+ if word_count < 10:
133
+ return "⚠️ Text too short. Please provide at least 10 words.", "", "", ""
134
+
135
+ # Load model
136
+ model = self.load_model(model_name)
137
+ if model is None:
138
+ return f"❌ Could not load {model_name} model. Try T5-Small.", "", "", ""
139
+
140
+ try:
141
+ start_time = time.time()
142
+
143
+ # Handle T5 special case
144
+ input_text = f"summarize: {text}" if "T5" in model_name else text
145
+
146
+ result = model(
147
+ input_text,
148
+ max_length=max_length,
149
+ min_length=min_length,
150
+ do_sample=False,
151
+ truncation=True
152
+ )
153
+
154
+ processing_time = time.time() - start_time
155
+ summary = result[0]['summary_text']
156
+
157
+ # Calculate metrics
158
+ summary_words = len(summary.split())
159
+ compression_ratio = (summary_words / word_count) * 100
160
+
161
+ metrics = f"""
162
+ πŸ“Š **Results:**
163
+ - Original: {word_count:,} words
164
+ - Summary: {summary_words:,} words
165
+ - Compression: {compression_ratio:.1f}%
166
+ - Time: {processing_time:.1f}s
167
+ - Model: {model_name}
168
+ """
169
+
170
+ return summary, metrics, f"{word_count:,}", f"{summary_words:,}"
171
+
172
+ except Exception as e:
173
+ return f"❌ Error: {str(e)}", "", "", ""
174
+
175
+ # Initialize summarizer
176
+ summarizer = DocumentSummarizer()
177
+
178
+ # Sample texts
179
+ SAMPLES = {
180
+ "Technology News": "Artificial intelligence and machine learning are transforming industries worldwide. From healthcare diagnostics to autonomous vehicles, AI systems are becoming more sophisticated and capable. However, this rapid advancement also raises questions about ethics, job displacement, and the need for proper regulation. Companies are investing billions in AI research while governments work to establish frameworks for responsible AI development.",
181
+
182
+ "Business Update": "The global economy shows mixed signals as markets navigate inflation concerns and supply chain disruptions. Technology stocks have seen volatility while energy sectors gain momentum. Central banks worldwide are adjusting monetary policies to balance growth and inflation targets. Investors remain cautious but optimistic about long-term recovery prospects.",
183
+
184
+ "Health Research": "Recent studies highlight the importance of preventive healthcare and lifestyle modifications. Research shows that regular exercise, balanced nutrition, and adequate sleep significantly impact long-term health outcomes. Mental health awareness is also growing, with new therapeutic approaches showing promising results in clinical trials."
185
+ }
186
+
187
+ def get_sample(choice):
188
+ return SAMPLES.get(choice, "")
189
+
190
+ def process_request(text, model, max_len, min_len, sample):
191
+ if sample != "None":
192
+ text = get_sample(sample)
193
+ return summarizer.summarize(text, model, max_len, min_len)
194
+
195
+ # Create Gradio interface
196
+ def create_app():
197
+ with gr.Blocks(
198
+ title="AI Document Summarizer",
199
+ theme=gr.themes.Default(primary_hue="blue")
200
+ ) as app:
201
+
202
+ gr.Markdown("""
203
+ # πŸ“„ AI Document Summarizer
204
+ ### Intelligent text summarization using transformer models
205
+ """)
206
+
207
+ # Show status
208
+ if dependencies_available:
209
+ gr.Markdown("βœ… **Status:** All dependencies loaded successfully!")
210
+ else:
211
+ gr.Markdown(f"""
212
+ ❌ **Status:** Missing dependencies - {', '.join(error_messages)}
213
+
214
+ **Fix:** Update your `requirements.txt` with the correct dependencies (see below).
215
+ """)
216
+
217
+ with gr.Row():
218
+ with gr.Column(scale=2):
219
+ gr.Markdown("## Input")
220
+
221
+ sample_choice = gr.Dropdown(
222
+ ["None"] + list(SAMPLES.keys()),
223
+ label="πŸ“‹ Try a sample:",
224
+ value="None"
225
+ )
226
+
227
+ text_input = gr.Textbox(
228
+ label="πŸ“ Your text:",
229
+ placeholder="Enter text to summarize...",
230
+ lines=8
231
+ )
232
+
233
+ sample_choice.change(get_sample, sample_choice, text_input)
234
+
235
+ with gr.Column(scale=1):
236
+ gr.Markdown("## Settings")
237
+
238
+ model_select = gr.Dropdown(
239
+ ["BART", "T5-Small", "DistilBART"],
240
+ label="πŸ€– Model:",
241
+ value="T5-Small"
242
+ )
243
+
244
+ max_length = gr.Slider(50, 200, 100, label="Max length")
245
+ min_length = gr.Slider(20, 80, 30, label="Min length")
246
+
247
+ submit_btn = gr.Button("πŸš€ Summarize", variant="primary")
248
+
249
+ with gr.Row():
250
+ summary_out = gr.Textbox(label="πŸ“‹ Summary", lines=5)
251
+ metrics_out = gr.Markdown("Metrics will appear here")
252
+
253
+ with gr.Row():
254
+ orig_count = gr.Textbox(label="Original", interactive=False)
255
+ summ_count = gr.Textbox(label="Summary", interactive=False)
256
+
257
+ submit_btn.click(
258
+ process_request,
259
+ [text_input, model_select, max_length, min_length, sample_choice],
260
+ [summary_out, metrics_out, orig_count, summ_count]
261
+ )
262
+
263
+ # Troubleshooting section
264
+ gr.Markdown("""
265
+ ---
266
+ ## πŸ”§ Troubleshooting
267
+
268
+ **If you see dependency errors, create these files:**
269
+
270
+ ### requirements.txt
271
+ ```
272
+ torch
273
+ transformers
274
+ gradio
275
+ accelerate
276
+ sentencepiece
277
+ numpy
278
+ ```
279
+
280
+ ### README.md
281
+ ```
282
+ ---
283
+ title: AI Document Summarizer
284
+ emoji: πŸ“„
285
+ colorFrom: blue
286
+ colorTo: green
287
+ sdk: gradio
288
+ sdk_version: 4.0.0
289
+ app_file: app.py
290
+ pinned: false
291
+ ---
292
+ ```
293
+ """)
294
+
295
+ return app
296
+
297
+ # Launch
298
+ if __name__ == "__main__":
299
+ app = create_app()
300
+ app.launch()
301
+
302
+ # =============================================================================
303
+ # DEPLOYMENT FILES - COPY THESE EXACTLY
304
+ # =============================================================================
305
+
306
+ REQUIREMENTS_TXT = """torch
307
+ transformers
308
+ gradio
309
+ accelerate
310
+ sentencepiece
311
+ numpy"""
312
+
313
+ README_MD = """---
314
+ title: AI Document Summarizer
315
+ emoji: πŸ“„
316
+ colorFrom: blue
317
+ colorTo: green
318
+ sdk: gradio
319
+ sdk_version: 4.0.0
320
+ app_file: app.py
321
+ pinned: false
322
+ ---
323
+
324
+ # AI Document Summarizer
325
+
326
+ Transform long documents into concise summaries using AI.
327
+
328
+ ## Features
329
+ - Multiple transformer models
330
+ - Real-time processing
331
+ - Sample documents included
332
+ - Error handling and troubleshooting
333
+
334
+ Built for Hugging Face Spaces deployment."""
335
+
336
+ print(f"""
337
+ πŸš€ BULLETPROOF DEPLOYMENT GUIDE
338
+
339
+ πŸ“ CREATE EXACTLY THESE 3 FILES:
340
+
341
+ 1️⃣ requirements.txt:
342
+ {REQUIREMENTS_TXT}
343
+
344
+ 2️⃣ README.md:
345
+ {README_MD}
346
+
347
+ 3️⃣ app.py:
348
+ [Copy the entire code above]
349
+
350
+ 🎯 DEPLOYMENT STEPS:
351
+ 1. Go to hf.co/spaces
352
+ 2. Create new Space
353
+ 3. Choose Gradio SDK
354
+ 4. Upload these 3 files EXACTLY as shown
355
+ 5. Wait for build to complete
356
+
357
+ βœ… This version will:
358
+ - Show clear error messages if dependencies fail
359
+ - Provide troubleshooting instructions
360
+ - Work with minimal requirements
361
+ - Handle all edge cases gracefully
362
+
363
+ ❌ Common issues fixed:
364
+ - Torch import errors
365
+ - Transformers import errors
366
+ - Version conflicts
367
+ - HF Spaces compatibility
368
+ """)