mimi111222 commited on
Commit
5a28e0a
Β·
verified Β·
1 Parent(s): 278b790

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +878 -0
app.py ADDED
@@ -0,0 +1,878 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+
4
+ # βœ… CRITICAL: Set environment variables BEFORE any other imports
5
+ os.environ['TRANSFORMERS_CACHE'] = tempfile.gettempdir()
6
+ os.environ['HF_HOME'] = tempfile.gettempdir()
7
+ os.environ['TORCH_HOME'] = tempfile.gettempdir()
8
+ os.environ['HF_DATASETS_CACHE'] = tempfile.gettempdir()
9
+ os.environ['HUGGINGFACE_HUB_CACHE'] = tempfile.gettempdir()
10
+
11
+ import streamlit as st
12
+
13
+ # βœ… CRITICAL: set_page_config() MUST be called first, before ANY Streamlit commands
14
+ st.set_page_config(
15
+ page_title="AI Study Helper Pro - by Umaima Qureshi",
16
+ page_icon="🧠",
17
+ layout="wide",
18
+ initial_sidebar_state="expanded"
19
+ )
20
+
21
+ # Now import other libraries
22
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification
23
+ from nltk.tokenize import sent_tokenize
24
+ import base64
25
+ import torch
26
+ import nltk
27
+
28
+ @st.cache_resource
29
+ def init_nltk():
30
+ """Initialize NLTK with writable directory"""
31
+ nltk_data_dir = os.path.join(tempfile.gettempdir(), "nltk_data")
32
+ os.makedirs(nltk_data_dir, exist_ok=True)
33
+
34
+ if nltk_data_dir not in nltk.data.path:
35
+ nltk.data.path.insert(0, nltk_data_dir)
36
+
37
+ for pkg in ["punkt", "punkt_tab"]:
38
+ try:
39
+ nltk.data.find(f"tokenizers/{pkg}")
40
+ except LookupError:
41
+ try:
42
+ nltk.download(pkg, download_dir=nltk_data_dir, quiet=True)
43
+ except:
44
+ pass # Continue if download fails
45
+
46
+ return True
47
+
48
+ # Initialize NLTK
49
+ init_nltk()
50
+
51
+ # Device detection
52
+ DEVICE = 0 if torch.cuda.is_available() else -1
53
+
54
+ # Lazy model loading with proper cache handling - FIXED (No cache_dir in pipeline)
55
+ @st.cache_resource
56
+ def get_summarizer():
57
+ """Load summarization model - cache_dir handled by environment variables"""
58
+ try:
59
+ model = AutoModelForSeq2SeqLM.from_pretrained(
60
+ "sshleifer/distilbart-cnn-12-6",
61
+ cache_dir=tempfile.gettempdir()
62
+ )
63
+ tokenizer = AutoTokenizer.from_pretrained(
64
+ "sshleifer/distilbart-cnn-12-6",
65
+ cache_dir=tempfile.gettempdir()
66
+ )
67
+ summarizer = pipeline(
68
+ "summarization",
69
+ model=model,
70
+ tokenizer=tokenizer,
71
+ device=DEVICE
72
+ )
73
+ return summarizer
74
+ except Exception as e:
75
+ st.error(f"Failed to load summarizer: {str(e)}")
76
+ return None
77
+
78
+ @st.cache_resource
79
+ def get_qa():
80
+ """Load Q&A model - cache_dir handled by environment variables"""
81
+ try:
82
+ model = AutoModelForQuestionAnswering.from_pretrained(
83
+ "distilbert-base-uncased-distilled-squad",
84
+ cache_dir=tempfile.gettempdir()
85
+ )
86
+ tokenizer = AutoTokenizer.from_pretrained(
87
+ "distilbert-base-uncased-distilled-squad",
88
+ cache_dir=tempfile.gettempdir()
89
+ )
90
+ qa_pipeline = pipeline(
91
+ "question-answering",
92
+ model=model,
93
+ tokenizer=tokenizer,
94
+ device=DEVICE
95
+ )
96
+ return qa_pipeline
97
+ except Exception as e:
98
+ st.error(f"Failed to load Q&A model: {str(e)}")
99
+ return None
100
+
101
+ @st.cache_resource
102
+ def get_classifier():
103
+ """Load classifier model - cache_dir handled by environment variables"""
104
+ try:
105
+ model = AutoModelForSequenceClassification.from_pretrained(
106
+ "typeform/distilbert-base-uncased-mnli",
107
+ cache_dir=tempfile.gettempdir()
108
+ )
109
+ tokenizer = AutoTokenizer.from_pretrained(
110
+ "typeform/distilbert-base-uncased-mnli",
111
+ cache_dir=tempfile.gettempdir()
112
+ )
113
+ classifier = pipeline(
114
+ "zero-shot-classification",
115
+ model=model,
116
+ tokenizer=tokenizer,
117
+ device=DEVICE
118
+ )
119
+ return classifier
120
+ except Exception as e:
121
+ st.error(f"Failed to load classifier: {str(e)}")
122
+ return None
123
+
124
+ @st.cache_resource
125
+ def load_translator(model_name):
126
+ """Load translation model - cache_dir handled by environment variables"""
127
+ try:
128
+ model = AutoModelForSeq2SeqLM.from_pretrained(
129
+ model_name,
130
+ cache_dir=tempfile.gettempdir()
131
+ )
132
+ tokenizer = AutoTokenizer.from_pretrained(
133
+ model_name,
134
+ cache_dir=tempfile.gettempdir()
135
+ )
136
+ translator = pipeline(
137
+ "translation",
138
+ model=model,
139
+ tokenizer=tokenizer,
140
+ device=DEVICE
141
+ )
142
+ return translator
143
+ except Exception as e:
144
+ st.error(f"Failed to load translator: {str(e)}")
145
+ return None
146
+
147
+ def truncate_text(text, max_words=400):
148
+ """Truncate text to maximum word count"""
149
+ words = text.split()
150
+ return (" ".join(words[:max_words]), len(words) > max_words)
151
+
152
+ # ULTRA PREMIUM CSS - Glassmorphism + Animations
153
+ st.markdown("""
154
+ <style>
155
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700;900&display=swap');
156
+
157
+ * {
158
+ font-family: 'Poppins', sans-serif;
159
+ }
160
+
161
+ .stApp {
162
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #4facfe 75%, #00f2fe 100%);
163
+ background-size: 400% 400%;
164
+ animation: gradientShift 15s ease infinite;
165
+ }
166
+
167
+ @keyframes gradientShift {
168
+ 0% { background-position: 0% 50%; }
169
+ 50% { background-position: 100% 50%; }
170
+ 100% { background-position: 0% 50%; }
171
+ }
172
+
173
+ /* Hero Header */
174
+ .hero-header {
175
+ background: linear-gradient(135deg, #1e1e3f 0%, #2d2d5f 100%);
176
+ padding: 3rem 2rem;
177
+ border-radius: 25px;
178
+ margin-bottom: 2rem;
179
+ text-align: center;
180
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
181
+ border: 2px solid #667eea;
182
+ }
183
+
184
+ .hero-title {
185
+ font-size: 3.5rem;
186
+ font-weight: 900;
187
+ color: #ffffff;
188
+ margin: 0 0 1rem 0;
189
+ text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.5);
190
+ }
191
+
192
+ .hero-subtitle {
193
+ font-size: 1.3rem;
194
+ color: #ffffff;
195
+ margin: 0;
196
+ font-weight: 400;
197
+ opacity: 0.95;
198
+ }
199
+
200
+ /* Premium Tabs */
201
+ .stTabs [data-baseweb="tab-list"] {
202
+ gap: 12px;
203
+ background: rgba(255, 255, 255, 0.1);
204
+ padding: 12px;
205
+ border-radius: 20px;
206
+ backdrop-filter: blur(10px);
207
+ border: 1px solid rgba(255, 255, 255, 0.2);
208
+ }
209
+
210
+ .stTabs [data-baseweb="tab"] {
211
+ background: rgba(255, 255, 255, 0.15);
212
+ border-radius: 15px;
213
+ color: white;
214
+ font-weight: 600;
215
+ font-size: 1.1rem;
216
+ padding: 12px 24px;
217
+ border: 1px solid rgba(255, 255, 255, 0.25);
218
+ transition: all 0.3s ease;
219
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
220
+ }
221
+
222
+ .stTabs [data-baseweb="tab"]:hover {
223
+ background: rgba(255, 255, 255, 0.25);
224
+ transform: translateY(-2px);
225
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
226
+ }
227
+
228
+ .stTabs [aria-selected="true"] {
229
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
230
+ border-color: rgba(255, 255, 255, 0.4);
231
+ box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
232
+ }
233
+
234
+ /* Premium Buttons */
235
+ .stButton > button {
236
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
237
+ color: white;
238
+ font-weight: 700;
239
+ font-size: 1.1rem;
240
+ padding: 16px 40px;
241
+ border-radius: 16px;
242
+ border: none;
243
+ box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
244
+ transition: all 0.3s ease;
245
+ width: 100%;
246
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
247
+ }
248
+
249
+ .stButton > button:hover {
250
+ transform: translateY(-3px);
251
+ box-shadow: 0 12px 36px rgba(102, 126, 234, 0.6);
252
+ background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
253
+ }
254
+
255
+ .stButton > button:active {
256
+ transform: translateY(-1px);
257
+ }
258
+
259
+ /* Input Fields - FIXED: Black background for text areas */
260
+ .stTextArea textarea, .stTextInput input {
261
+ background: rgba(0, 0, 0, 0.85) !important;
262
+ backdrop-filter: blur(10px);
263
+ border: 2px solid rgba(255, 255, 255, 0.3) !important;
264
+ border-radius: 16px !important;
265
+ color: white !important;
266
+ font-size: 1rem !important;
267
+ padding: 16px !important;
268
+ text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
269
+ }
270
+
271
+ .stTextArea textarea::placeholder, .stTextInput input::placeholder {
272
+ color: rgba(255, 255, 255, 0.6) !important;
273
+ }
274
+
275
+ .stTextArea textarea:focus, .stTextInput input:focus {
276
+ border-color: rgba(255, 255, 255, 0.6) !important;
277
+ box-shadow: 0 0 20px rgba(255, 255, 255, 0.3) !important;
278
+ background: rgba(0, 0, 0, 0.9) !important;
279
+ }
280
+
281
+ /* Result Cards */
282
+ .result-card {
283
+ background: rgba(255, 255, 255, 0.95);
284
+ color: #1a1a1a;
285
+ padding: 2rem;
286
+ border-radius: 20px;
287
+ margin: 1rem auto;
288
+ max-width: 900px;
289
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
290
+ animation: fadeIn 0.5s ease;
291
+ border-left: 5px solid #667eea;
292
+ }
293
+
294
+ .result-card p {
295
+ word-break: break-word;
296
+ overflow-wrap: break-word;
297
+ max-height: 200px;
298
+ overflow-y: auto;
299
+ line-height: 1.6;
300
+ }
301
+
302
+ /* FIXED: Scrollable container for keywords with proper visibility */
303
+ .keywords-container {
304
+ max-height: 400px;
305
+ overflow-y: auto;
306
+ padding: 10px;
307
+ background: rgba(255, 255, 255, 0.05);
308
+ border-radius: 15px;
309
+ margin-top: 1rem;
310
+ }
311
+
312
+ /* FIXED: Quiz questions container with dark background and scrolling */
313
+ .quiz-container {
314
+ max-height: 500px;
315
+ overflow-y: auto;
316
+ padding: 10px;
317
+ }
318
+
319
+ .quiz-question-card {
320
+ background: rgba(30, 30, 60, 0.9) !important;
321
+ color: white !important;
322
+ padding: 1.5rem;
323
+ margin: 1rem 0;
324
+ border-radius: 15px;
325
+ border-left: 4px solid #667eea;
326
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
327
+ }
328
+
329
+ .quiz-question-card strong {
330
+ color: #667eea !important;
331
+ }
332
+
333
+ @keyframes fadeIn {
334
+ from { opacity: 0; transform: translateY(20px); }
335
+ to { opacity: 1; transform: translateY(0); }
336
+ }
337
+
338
+ /* Stats Badge */
339
+ .stats-badge {
340
+ display: inline-block;
341
+ background: rgba(255, 255, 255, 0.25);
342
+ backdrop-filter: blur(10px);
343
+ padding: 8px 20px;
344
+ border-radius: 20px;
345
+ color: white;
346
+ font-weight: 600;
347
+ border: 1px solid rgba(255, 255, 255, 0.3);
348
+ margin: 5px;
349
+ text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
350
+ }
351
+
352
+ /* Success/Error Messages */
353
+ .stSuccess {
354
+ background: rgba(72, 187, 120, 0.25) !important;
355
+ backdrop-filter: blur(10px);
356
+ border-left: 4px solid #48bb78 !important;
357
+ border-radius: 12px !important;
358
+ color: white !important;
359
+ }
360
+
361
+ .stError {
362
+ background: rgba(245, 101, 101, 0.25) !important;
363
+ backdrop-filter: blur(10px);
364
+ border-left: 4px solid #f56565 !important;
365
+ border-radius: 12px !important;
366
+ color: white !important;
367
+ }
368
+
369
+ .stInfo {
370
+ background: rgba(66, 153, 225, 0.25) !important;
371
+ backdrop-filter: blur(10px);
372
+ border-left: 4px solid #4299e1 !important;
373
+ border-radius: 12px !important;
374
+ color: white !important;
375
+ }
376
+
377
+ .stWarning {
378
+ background: rgba(237, 137, 54, 0.25) !important;
379
+ backdrop-filter: blur(10px);
380
+ border-left: 4px solid #ed8936 !important;
381
+ border-radius: 12px !important;
382
+ color: white !important;
383
+ }
384
+
385
+ /* Sidebar */
386
+ .css-1d391kg, [data-testid="stSidebar"] {
387
+ background: rgba(20, 20, 40, 0.9);
388
+ backdrop-filter: blur(20px);
389
+ border-right: 1px solid rgba(255, 255, 255, 0.2);
390
+ }
391
+
392
+ .css-1d391kg h2, [data-testid="stSidebar"] h2 {
393
+ color: white;
394
+ font-weight: 700;
395
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
396
+ }
397
+
398
+ .css-1d391kg p, [data-testid="stSidebar"] p {
399
+ color: rgba(255, 255, 255, 0.9);
400
+ }
401
+
402
+ /* Ensure text wraps properly */
403
+ .stTabs h3 {
404
+ word-break: break-word;
405
+ overflow-wrap: anywhere;
406
+ white-space: normal;
407
+ max-width: 100%;
408
+ margin: 0;
409
+ padding: 0.5rem 0;
410
+ color: white;
411
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
412
+ }
413
+
414
+ /* Spinner */
415
+ .stSpinner > div {
416
+ border-top-color: white !important;
417
+ }
418
+
419
+ /* Selectbox */
420
+ .stSelectbox > div > div {
421
+ background: rgba(255, 255, 255, 0.2) !important;
422
+ backdrop-filter: blur(10px);
423
+ border-radius: 12px !important;
424
+ color: white !important;
425
+ border: 2px solid rgba(255, 255, 255, 0.3) !important;
426
+ }
427
+
428
+ .stSelectbox label {
429
+ color: white !important;
430
+ font-weight: 600 !important;
431
+ text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
432
+ }
433
+
434
+ /* Footer */
435
+ .premium-footer {
436
+ text-align: center;
437
+ padding: 2rem;
438
+ margin-top: 3rem;
439
+ background: rgba(20, 20, 40, 0.85);
440
+ backdrop-filter: blur(15px);
441
+ border-radius: 20px;
442
+ border: 2px solid rgba(255, 255, 255, 0.3);
443
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
444
+ }
445
+
446
+ .premium-footer p {
447
+ color: rgba(255, 255, 255, 0.9);
448
+ margin: 0.5rem 0;
449
+ text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
450
+ }
451
+
452
+ /* Hide Streamlit Branding */
453
+ #MainMenu {visibility: hidden;}
454
+ footer {visibility: hidden;}
455
+ header {visibility: hidden;}
456
+
457
+ /* Custom scrollbar */
458
+ ::-webkit-scrollbar {
459
+ width: 10px;
460
+ }
461
+
462
+ ::-webkit-scrollbar-track {
463
+ background: rgba(255, 255, 255, 0.1);
464
+ }
465
+
466
+ ::-webkit-scrollbar-thumb {
467
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
468
+ border-radius: 5px;
469
+ }
470
+
471
+ ::-webkit-scrollbar-thumb:hover {
472
+ background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
473
+ }
474
+ </style>
475
+ """, unsafe_allow_html=True)
476
+
477
+ # Hero Header
478
+ st.markdown("""
479
+ <div class="hero-header">
480
+ <div class="hero-title">🧠 AI Study Helper Pro</div>
481
+ <div class="hero-subtitle">⚑ Supercharge Your Learning with Advanced AI Technology</div>
482
+ </div>
483
+ """, unsafe_allow_html=True)
484
+
485
+ # Add cache clear button (for troubleshooting)
486
+ with st.expander("βš™οΈ Settings", expanded=False):
487
+ if st.button("πŸ”„ Clear Model Cache (if you see errors)"):
488
+ st.cache_resource.clear()
489
+ st.success("βœ… Cache cleared! Please refresh the page.")
490
+ st.info("πŸ’‘ This will reload all AI models on next use.")
491
+
492
+ # Sidebar
493
+ with st.sidebar:
494
+ st.markdown("### 🎯 Dashboard")
495
+ st.markdown("---")
496
+
497
+ # Stats
498
+ col1, col2 = st.columns(2)
499
+ with col1:
500
+ st.markdown('<div class="stats-badge">πŸ“Š 247 Processed</div>', unsafe_allow_html=True)
501
+ with col2:
502
+ st.markdown('<div class="stats-badge">⚑ 2.3s Avg</div>', unsafe_allow_html=True)
503
+
504
+ st.markdown("---")
505
+ st.markdown("### ✨ Features")
506
+ features = [
507
+ "πŸ“ AI Summarization",
508
+ "πŸ’¬ Smart Q&A",
509
+ "🎯 Quiz Generator",
510
+ "🌍 Multi-Language",
511
+ "πŸ”‘ Keyword Extraction",
512
+ "πŸ’¨ Lightning Fast"
513
+ ]
514
+ for feat in features:
515
+ st.markdown(f"**{feat}**")
516
+
517
+ st.markdown("---")
518
+ st.markdown("### πŸ‘©β€πŸ’» Developer")
519
+ st.markdown("**Umaima Qureshi**")
520
+ st.markdown("[GitHub](https://github.com/Umaima122)")
521
+
522
+ # Initialize session state
523
+ for key in ["summary", "quiz", "translation", "keywords"]:
524
+ if key not in st.session_state:
525
+ st.session_state[key] = "" if key not in ["quiz", "keywords"] else []
526
+
527
+ # Tabs
528
+ tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
529
+ "πŸ“ Summarize", "πŸ’¬ Q&A", "🎯 Quiz", "🌍 Translate", "πŸ”‘ Keywords", "πŸ“₯ Download"
530
+ ])
531
+
532
+ # ============================================
533
+ # TAB 1: SUMMARIZE
534
+ # ============================================
535
+ with tab1:
536
+ st.markdown("### πŸ“ Intelligent Summarization")
537
+
538
+ text = st.text_area(
539
+ "✍️ Your notes or textbook:",
540
+ value="",
541
+ height=250,
542
+ key="sum_txt",
543
+ placeholder="Paste your content here and watch AI magic happen..."
544
+ )
545
+
546
+ col1, col2, col3 = st.columns([1, 1, 1])
547
+ with col2:
548
+ if st.button("✨ Generate Summary", key="sum_btn"):
549
+ if not text.strip():
550
+ st.error("⚠️ Please provide text to summarize")
551
+ else:
552
+ trunc, was_trunc = truncate_text(text, 400)
553
+ if was_trunc:
554
+ st.info("πŸ“Š Text optimized to 400 words for processing")
555
+
556
+ if len(trunc.split()) < 20:
557
+ st.error("⚠️ Need at least 20 words to generate a meaningful summary")
558
+ else:
559
+ with st.spinner("🧠 AI is thinking..."):
560
+ try:
561
+ summarizer = get_summarizer()
562
+ if summarizer:
563
+ result = summarizer(
564
+ trunc,
565
+ max_length=130,
566
+ min_length=30,
567
+ do_sample=False
568
+ )
569
+ summary = result[0]['summary_text']
570
+
571
+ st.markdown(f"""
572
+ <div class="result-card">
573
+ <h4 style="color: #667eea; margin-bottom: 1rem;">πŸ“„ AI-Generated Summary</h4>
574
+ <p style="font-size: 1.1rem; line-height: 1.8; color: #2d3748;">{summary}</p>
575
+ <div style="margin-top: 1rem; padding-top: 1rem; border-top: 1px solid #e2e8f0;">
576
+ <span class="stats-badge" style="background: #667eea; color: white;">
577
+ {len(summary.split())} words
578
+ </span>
579
+ <span class="stats-badge" style="background: #48bb78; color: white;">
580
+ βœ“ Completed
581
+ </span>
582
+ </div>
583
+ </div>
584
+ """, unsafe_allow_html=True)
585
+ st.session_state["summary"] = summary
586
+ except Exception as e:
587
+ st.error(f"❌ Error generating summary: {str(e)}")
588
+
589
+ # ============================================
590
+ # TAB 2: Q&A
591
+ # ============================================
592
+ with tab2:
593
+ st.markdown("### πŸ’¬ Intelligent Q&A System")
594
+
595
+ context = st.text_area(
596
+ "πŸ“š Context (Your notes):",
597
+ value="",
598
+ height=200,
599
+ key="qa_ctx",
600
+ placeholder="Paste your study material here..."
601
+ )
602
+
603
+ question = st.text_input(
604
+ "❓ Ask your question:",
605
+ key="qa_q",
606
+ placeholder="What would you like to know?"
607
+ )
608
+
609
+ col1, col2, col3 = st.columns([1, 1, 1])
610
+ with col2:
611
+ if st.button("πŸ” Get Answer", key="qa_btn"):
612
+ if not context.strip() or not question.strip():
613
+ st.error("⚠️ Please provide both context and question")
614
+ else:
615
+ trunc_ctx, _ = truncate_text(context, 400)
616
+ with st.spinner("πŸ€” Analyzing..."):
617
+ try:
618
+ qa_model = get_qa()
619
+ if qa_model:
620
+ result = qa_model(question=question, context=trunc_ctx)
621
+ answer = result['answer']
622
+ confidence = result.get('score', 0)
623
+
624
+ st.markdown(f"""
625
+ <div class="result-card">
626
+ <h4 style="color: #667eea; margin-bottom: 1rem;">πŸ’‘ AI Answer</h4>
627
+ <p style="font-size: 1.2rem; line-height: 1.8; color: #2d3748; font-weight: 500;">{answer}</p>
628
+ <div style="margin-top: 1rem; padding-top: 1rem; border-top: 1px solid #e2e8f0;">
629
+ <span class="stats-badge" style="background: #48bb78; color: white;">
630
+ βœ“ Answer Found
631
+ </span>
632
+ <span class="stats-badge" style="background: #667eea; color: white;">
633
+ Confidence: {confidence:.1%}
634
+ </span>
635
+ </div>
636
+ </div>
637
+ """, unsafe_allow_html=True)
638
+ except Exception as e:
639
+ st.error(f"❌ Error finding answer: {str(e)}")
640
+
641
+ # ============================================
642
+ # TAB 3: QUIZ - FIXED
643
+ # ============================================
644
+ with tab3:
645
+ st.markdown("### 🎯 AI Quiz Generator")
646
+
647
+ quiz_ctx = st.text_area(
648
+ "πŸ“– Study material:",
649
+ value="",
650
+ height=200,
651
+ key="quiz_ctx",
652
+ placeholder="Paste content for quiz generation..."
653
+ )
654
+
655
+ col1, col2, col3 = st.columns([1, 1, 1])
656
+ with col2:
657
+ if st.button("πŸš€ Generate Quiz", key="quiz_btn"):
658
+ if not quiz_ctx.strip():
659
+ st.error("⚠️ Please provide text for quiz generation")
660
+ else:
661
+ trunc_quiz, _ = truncate_text(quiz_ctx, 200)
662
+ with st.spinner("🎲 Creating questions..."):
663
+ try:
664
+ sentences = sent_tokenize(trunc_quiz)[:5]
665
+ if len(sentences) == 0:
666
+ st.warning("⚠️ Could not extract sentences from the text")
667
+ else:
668
+ questions = [f"What is the main concept in: '{s[:70]}...'?" for s in sentences if len(s) > 10]
669
+
670
+ if questions:
671
+ st.markdown('<div class="result-card">', unsafe_allow_html=True)
672
+ st.markdown("<h4 style='color: #667eea;'>πŸ“ Generated Quiz Questions</h4>", unsafe_allow_html=True)
673
+ st.markdown('<div class="quiz-container">', unsafe_allow_html=True)
674
+ for i, q in enumerate(questions, 1):
675
+ st.markdown(f"""
676
+ <div class='quiz-question-card'>
677
+ <strong>Question {i}:</strong> <span style='color: white;'>{q}</span>
678
+ </div>
679
+ """, unsafe_allow_html=True)
680
+ st.markdown('</div></div>', unsafe_allow_html=True)
681
+ st.session_state["quiz"] = questions
682
+ else:
683
+ st.warning("⚠️ Could not generate questions from the provided text")
684
+ except Exception as e:
685
+ st.error(f"❌ Error generating quiz: {str(e)}")
686
+
687
+ # ============================================
688
+ # TAB 4: TRANSLATE
689
+ # ============================================
690
+ with tab4:
691
+ st.markdown("### 🌍 AI Translation")
692
+
693
+ trans_text = st.text_area(
694
+ "✍️ Text to translate:",
695
+ height=200,
696
+ key="trans_txt",
697
+ placeholder="Enter text to translate..."
698
+ )
699
+
700
+ col1, col2 = st.columns(2)
701
+ with col1:
702
+ lang = st.selectbox(
703
+ "🎯 Target language:",
704
+ ["French", "German", "Spanish", "Italian", "Hindi"]
705
+ )
706
+
707
+ with col2:
708
+ st.write("")
709
+ st.write("")
710
+ if st.button("🌐 Translate Now", key="trans_btn"):
711
+ if not trans_text.strip():
712
+ st.error("⚠️ Please provide text to translate")
713
+ else:
714
+ model_map = {
715
+ "French": "Helsinki-NLP/opus-mt-en-fr",
716
+ "German": "Helsinki-NLP/opus-mt-en-de",
717
+ "Spanish": "Helsinki-NLP/opus-mt-en-es",
718
+ "Italian": "Helsinki-NLP/opus-mt-en-it",
719
+ "Hindi": "Helsinki-NLP/opus-mt-en-hi"
720
+ }
721
+
722
+ trunc_trans, _ = truncate_text(trans_text, 200)
723
+ with st.spinner(f"🌍 Translating to {lang}..."):
724
+ try:
725
+ translator = load_translator(model_map[lang])
726
+ if translator:
727
+ result = translator(trunc_trans, max_length=256)
728
+ translation = result[0]['translation_text']
729
+
730
+ st.markdown(f"""
731
+ <div class="result-card">
732
+ <h4 style="color: #667eea; margin-bottom: 1rem;">🌐 Translation ({lang})</h4>
733
+ <p style="font-size: 1.2rem; line-height: 1.8; color: #2d3748;">{translation}</p>
734
+ <div style="margin-top: 1rem; padding-top: 1rem; border-top: 1px solid #e2e8f0;">
735
+ <span class="stats-badge" style="background: #48bb78; color: white;">
736
+ βœ“ Translated
737
+ </span>
738
+ </div>
739
+ </div>
740
+ """, unsafe_allow_html=True)
741
+ st.session_state["translation"] = translation
742
+ except Exception as e:
743
+ st.error(f"❌ Translation Error: {str(e)}")
744
+
745
+ # ============================================
746
+ # TAB 5: KEYWORDS - FIXED
747
+ # ============================================
748
+ with tab5:
749
+ st.markdown("### πŸ”‘ AI Keyword Extraction")
750
+
751
+ keyword_input = st.text_area(
752
+ "πŸ“ Text for analysis:",
753
+ value="",
754
+ height=200,
755
+ key="kw_txt",
756
+ placeholder="Paste text to extract key concepts..."
757
+ )
758
+
759
+ col1, col2, col3 = st.columns([1, 1, 1])
760
+ with col2:
761
+ if st.button("πŸ” Extract Keywords", key="kw_btn"):
762
+ if not keyword_input.strip():
763
+ st.error("⚠️ Please provide text for keyword extraction")
764
+ else:
765
+ trunc_kw, _ = truncate_text(keyword_input, 200)
766
+ with st.spinner("πŸ”Ž Analyzing concepts..."):
767
+ try:
768
+ classifier = get_classifier()
769
+ if classifier:
770
+ labels = ["technology", "science", "education", "health", "business", "finance", "medical", "engineering", "mathematics", "history"]
771
+ result = classifier(trunc_kw, labels)
772
+ keywords = [lbl for lbl, score in zip(result['labels'], result['scores']) if score > 0.3][:5]
773
+
774
+ if keywords:
775
+ st.markdown('<div class="result-card">', unsafe_allow_html=True)
776
+ st.markdown("<h4 style='color: #667eea;'>🎯 Extracted Keywords</h4>", unsafe_allow_html=True)
777
+ st.markdown('<div class="keywords-container">', unsafe_allow_html=True)
778
+ kw_html = " ".join([
779
+ f"<span style='display: inline-block; background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 12px 24px; border-radius: 25px; margin: 8px; font-size: 1rem; font-weight: 600; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);'>{kw}</span>"
780
+ for kw in keywords
781
+ ])
782
+ st.markdown(kw_html, unsafe_allow_html=True)
783
+ st.markdown('</div></div>', unsafe_allow_html=True)
784
+ st.session_state["keywords"] = keywords
785
+ else:
786
+ st.info("ℹ️ No strong keywords found. Try providing more detailed text.")
787
+ except Exception as e:
788
+ st.error(f"❌ Error extracting keywords: {str(e)}")
789
+
790
+ # ============================================
791
+ # TAB 6: DOWNLOAD
792
+ # ============================================
793
+ with tab6:
794
+ st.markdown("### πŸ“₯ Download Results")
795
+
796
+ def download_link(text, filename, emoji):
797
+ """Generate download link for text content"""
798
+ b64 = base64.b64encode(text.encode()).decode()
799
+ return f"""
800
+ <a href="data:file/txt;base64,{b64}" download="{filename}"
801
+ style="display: inline-block; background: linear-gradient(135deg, #667eea, #764ba2);
802
+ color: white; padding: 16px 32px; border-radius: 16px; text-decoration: none;
803
+ font-weight: 700; font-size: 1.1rem; margin: 10px; box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
804
+ transition: all 0.3s ease;">
805
+ {emoji} Download {filename}
806
+ </a>
807
+ """
808
+
809
+ col1, col2 = st.columns(2)
810
+
811
+ with col1:
812
+ if st.session_state["summary"]:
813
+ st.markdown(download_link(st.session_state["summary"], "summary.txt", "πŸ“„"), unsafe_allow_html=True)
814
+ else:
815
+ st.info("πŸ“„ Generate a summary first")
816
+
817
+ if st.session_state["quiz"]:
818
+ quiz_text = "\n\n".join([f"Question {i}: {q}" for i, q in enumerate(st.session_state["quiz"], 1)])
819
+ st.markdown(download_link(quiz_text, "quiz.txt", "🎯"), unsafe_allow_html=True)
820
+ else:
821
+ st.info("🎯 Generate a quiz first")
822
+
823
+ with col2:
824
+ if st.session_state["translation"]:
825
+ st.markdown(download_link(st.session_state["translation"], "translation.txt", "🌍"), unsafe_allow_html=True)
826
+ else:
827
+ st.info("🌍 Translate text first")
828
+
829
+ if st.session_state["keywords"]:
830
+ keywords_text = "Extracted Keywords:\n\n" + "\n".join([f"- {kw}" for kw in st.session_state["keywords"]])
831
+ st.markdown(download_link(keywords_text, "keywords.txt", "πŸ”‘"), unsafe_allow_html=True)
832
+ else:
833
+ st.info("πŸ”‘ Extract keywords first")
834
+
835
+ st.markdown("---")
836
+
837
+ if not any([st.session_state["summary"], st.session_state["quiz"],
838
+ st.session_state["translation"], st.session_state["keywords"]]):
839
+ st.warning("ℹ️ Generate content in other tabs to enable downloads")
840
+ else:
841
+ st.success("βœ… Content ready for download! Click the buttons above.")
842
+
843
+ # ============================================
844
+ # PREMIUM FOOTER
845
+ # ============================================
846
+ st.markdown("""
847
+ <div class="premium-footer">
848
+ <p style="font-size: 1.2rem; font-weight: 600;">
849
+ Built with ❀️ by
850
+ <span style="background: linear-gradient(135deg, #ffffff, #e0e7ff);
851
+ -webkit-background-clip: text;
852
+ -webkit-text-fill-color: transparent;
853
+ font-weight: 700;">
854
+ Umaima Qureshi
855
+ </span>
856
+ </p>
857
+ <p style="font-size: 0.9rem;">Β© 2025 AI Study Helper Pro. All Rights Reserved.</p>
858
+ <p style="margin-top: 1rem;">
859
+ <a href="https://github.com/Umaima122" target="_blank"
860
+ style="color: white; text-decoration: none; padding: 8px 20px;
861
+ background: rgba(255, 255, 255, 0.15); border-radius: 20px;
862
+ backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.3);
863
+ transition: all 0.3s ease; margin: 0 5px;">
864
+ πŸ”— GitHub
865
+ </a>
866
+ <a href="https://www.linkedin.com/in/umaima-qureshi" target="_blank"
867
+ style="color: white; text-decoration: none; padding: 8px 20px;
868
+ background: rgba(255, 255, 255, 0.15); border-radius: 20px;
869
+ backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.3);
870
+ transition: all 0.3s ease; margin: 0 5px;">
871
+ πŸ’Ό LinkedIn
872
+ </a>
873
+ </p>
874
+ <p style="font-size: 0.85rem; margin-top: 1rem; opacity: 0.8;">
875
+ Powered by Transformers β€’ PyTorch β€’ Streamlit β€’ NLTK
876
+ </p>
877
+ </div>
878
+ """, unsafe_allow_html=True)