andevs commited on
Commit
958ff3e
Β·
verified Β·
1 Parent(s): a01f67d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1233 -538
app.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
- StudyFlow AI Backend - Complete Production Version
3
- Features: AI-powered question generation, PDF page selection, YouTube transcript extraction, full database persistence
 
4
  """
5
 
6
  import os
@@ -11,20 +12,31 @@ import tempfile
11
  import re
12
  import requests
13
  import uuid
14
- from datetime import datetime
15
- from typing import List, Dict, Optional, Tuple
16
- from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request
17
- from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
 
 
18
  from fastapi.middleware.cors import CORSMiddleware
19
  from fastapi.staticfiles import StaticFiles
20
  import PyPDF2
21
  from youtube_transcript_api import YouTubeTranscriptApi
22
  from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound
23
 
24
- # Initialize FastAPI
25
- app = FastAPI(title="StudyFlow AI", version="3.0.0", description="AI-Powered Study Assistant")
 
26
 
27
- # CORS middleware - Allow all origins for development
 
 
 
 
 
 
 
 
28
  app.add_middleware(
29
  CORSMiddleware,
30
  allow_origins=["*"],
@@ -33,73 +45,61 @@ app.add_middleware(
33
  allow_headers=["*"],
34
  )
35
 
36
- # ==================== CONFIGURATION ====================
37
-
38
- # Hugging Face API configuration (optional - will use fallback if not set)
39
- HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
40
- HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
41
- # Alternative models (uncomment to use):
42
- # HF_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
43
- # HF_API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-large"
44
 
45
- # Database setup
46
- DB_PATH = "/data/studyflow.db" if os.path.exists("/data") else "studyflow.db"
47
 
48
- # ==================== DATABASE INITIALIZATION ====================
49
-
50
- def init_db():
51
- """Initialize SQLite database with all required tables"""
52
  conn = sqlite3.connect(DB_PATH)
53
  cursor = conn.cursor()
54
 
55
- # Sessions table - stores main session info
 
 
 
56
  cursor.execute('''
57
  CREATE TABLE IF NOT EXISTS sessions (
58
  id TEXT PRIMARY KEY,
59
  title TEXT NOT NULL,
60
- content_type TEXT NOT NULL,
61
- difficulty TEXT NOT NULL,
 
62
  content_hash TEXT,
63
  selected_pages TEXT,
64
- total_pages INTEGER,
65
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
66
- last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 
 
67
  )
68
  ''')
69
 
70
- # Questions table - stores all generated questions
71
  cursor.execute('''
72
  CREATE TABLE IF NOT EXISTS questions (
73
  id TEXT PRIMARY KEY,
74
  session_id TEXT NOT NULL,
75
  question_text TEXT NOT NULL,
76
- question_type TEXT NOT NULL,
77
  options TEXT,
78
  correct_answer TEXT NOT NULL,
79
- difficulty TEXT NOT NULL,
80
  explanation TEXT,
81
  user_answer TEXT,
82
  is_correct INTEGER DEFAULT 0,
83
  time_spent INTEGER DEFAULT 0,
84
  page_reference INTEGER,
 
 
85
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
86
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
87
  )
88
  ''')
89
 
90
- # Pages table - stores individual page content from PDFs
91
- cursor.execute('''
92
- CREATE TABLE IF NOT EXISTS pages (
93
- id TEXT PRIMARY KEY,
94
- session_id TEXT NOT NULL,
95
- page_number INTEGER NOT NULL,
96
- content TEXT NOT NULL,
97
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
98
- FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
99
- )
100
- ''')
101
-
102
- # Flashcards table
103
  cursor.execute('''
104
  CREATE TABLE IF NOT EXISTS flashcards (
105
  id TEXT PRIMARY KEY,
@@ -107,13 +107,16 @@ def init_db():
107
  front TEXT NOT NULL,
108
  back TEXT NOT NULL,
109
  category TEXT,
110
- difficulty TEXT,
 
 
 
111
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
112
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
113
  )
114
  ''')
115
 
116
- # Notes table
117
  cursor.execute('''
118
  CREATE TABLE IF NOT EXISTS notes (
119
  id TEXT PRIMARY KEY,
@@ -121,50 +124,160 @@ def init_db():
121
  title TEXT NOT NULL,
122
  content TEXT NOT NULL,
123
  tags TEXT,
 
 
124
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
125
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
126
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
127
  )
128
  ''')
129
 
130
- # User profile table for analytics
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  cursor.execute('''
132
  CREATE TABLE IF NOT EXISTS user_profile (
133
- id INTEGER PRIMARY KEY AUTOINCREMENT,
 
134
  total_questions_answered INTEGER DEFAULT 0,
135
  total_correct_answers INTEGER DEFAULT 0,
136
  total_study_time INTEGER DEFAULT 0,
137
  total_sessions_created INTEGER DEFAULT 0,
138
- last_active TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 
 
 
 
 
 
 
139
  )
140
  ''')
141
 
142
- # Create indexes for better performance
143
- cursor.execute('CREATE INDEX IF NOT EXISTS idx_questions_session ON questions(session_id)')
144
- cursor.execute('CREATE INDEX IF NOT EXISTS idx_pages_session ON pages(session_id)')
145
- cursor.execute('CREATE INDEX IF NOT EXISTS idx_flashcards_session ON flashcards(session_id)')
146
- cursor.execute('CREATE INDEX IF NOT EXISTS idx_sessions_accessed ON sessions(last_accessed)')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  conn.commit()
149
  conn.close()
150
- print(f"βœ… Database initialized at: {DB_PATH}")
151
 
152
- # Initialize database on startup
153
- init_db()
154
 
155
- # ==================== HELPER FUNCTIONS ====================
 
 
156
 
157
  def generate_id(prefix: str = "") -> str:
158
- """Generate a unique ID"""
159
  unique_id = str(uuid.uuid4())[:12]
160
  return f"{prefix}_{unique_id}" if prefix else unique_id
161
 
162
- def extract_text_from_pdf(file_path: str) -> Dict[int, str]:
163
- """
164
- Extract text from PDF file and return dictionary of page_number -> content
165
- Detects page boundaries automatically even without explicit page numbers
166
- """
 
 
 
 
 
 
 
 
 
 
 
167
  pages_text = {}
 
 
168
  try:
169
  with open(file_path, 'rb') as file:
170
  pdf_reader = PyPDF2.PdfReader(file)
@@ -173,28 +286,34 @@ def extract_text_from_pdf(file_path: str) -> Dict[int, str]:
173
  for page_num, page in enumerate(pdf_reader.pages, start=1):
174
  try:
175
  page_text = page.extract_text()
176
- if page_text and len(page_text.strip()) > 30: # Only include pages with meaningful content
177
- # Clean up the text
178
  page_text = re.sub(r'\s+', ' ', page_text).strip()
179
- pages_text[page_num] = page_text
 
 
 
 
180
  else:
181
- pages_text[page_num] = f"[Page {page_num} - No extractable text content]"
182
  except Exception as e:
183
- print(f"Error extracting page {page_num}: {str(e)}")
184
- pages_text[page_num] = f"[Page {page_num} - Error extracting text]"
 
 
 
185
 
186
- print(f"βœ… Extracted {len(pages_text)} pages from PDF (total pages: {total_pages})")
187
- return pages_text
188
  except Exception as e:
189
  print(f"❌ PDF extraction error: {str(e)}")
190
- return {}
191
 
192
- def extract_text_from_youtube(url: str, start_time: float = None, end_time: float = None) -> str:
193
- """
194
- Extract transcript from YouTube video with optional time filtering
195
- """
 
 
196
  try:
197
- # Extract video ID from URL
198
  if "youtube.com/watch?v=" in url:
199
  video_id = url.split("v=")[-1].split("&")[0]
200
  elif "youtu.be/" in url:
@@ -207,21 +326,21 @@ def extract_text_from_youtube(url: str, start_time: float = None, end_time: floa
207
 
208
  # Filter by time if specified
209
  if start_time is not None or end_time is not None:
210
- filtered_transcript = []
211
  for entry in transcript_list:
212
  entry_time = entry['start']
213
  if start_time is not None and entry_time < start_time:
214
  continue
215
  if end_time is not None and entry_time > end_time:
216
  continue
217
- filtered_transcript.append(entry)
218
- transcript_list = filtered_transcript
219
 
220
  # Combine text
221
  text = " ".join([entry['text'] for entry in transcript_list])
222
- print(f"βœ… Extracted {len(transcript_list)} segments from YouTube video")
223
- return text
224
-
225
  except TranscriptsDisabled:
226
  print("❌ Transcripts disabled for this video")
227
  return ""
@@ -232,355 +351,513 @@ def extract_text_from_youtube(url: str, start_time: float = None, end_time: floa
232
  print(f"❌ YouTube extraction error: {str(e)}")
233
  return ""
234
 
235
- def call_hf_api(prompt: str, max_length: int = 1000, temperature: float = 0.7) -> Optional[str]:
236
- """
237
- Call Hugging Face Inference API for AI-powered question generation
238
- Returns None if API call fails (will use fallback)
239
- """
240
- if not HF_API_TOKEN or HF_API_TOKEN == "":
241
- print("⚠️ No HF_API_TOKEN provided, using fallback question generation")
242
- return None
243
-
244
- try:
245
- headers = {
246
- "Authorization": f"Bearer {HF_API_TOKEN}",
247
- "Content-Type": "application/json"
248
- }
249
-
250
- payload = {
251
- "inputs": prompt,
252
- "parameters": {
253
- "max_new_tokens": max_length,
254
- "temperature": temperature,
255
- "top_p": 0.95,
256
- "do_sample": True,
257
- "return_full_text": False
258
- }
259
- }
260
-
261
- print(f"πŸ“‘ Calling Hugging Face API...")
262
- response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60)
263
-
264
- if response.status_code == 200:
265
- result = response.json()
266
- generated_text = result[0].get("generated_text", "")
267
- print(f"βœ… AI response received ({len(generated_text)} chars)")
268
- return generated_text
269
- else:
270
- print(f"❌ HF API error: {response.status_code} - {response.text}")
271
- return None
272
-
273
- except requests.exceptions.Timeout:
274
- print("❌ HF API timeout after 60 seconds")
275
- return None
276
- except Exception as e:
277
- print(f"❌ HF API error: {str(e)}")
278
- return None
279
-
280
- def generate_questions_with_ai(content: str, difficulty: str, count: int, page_ref: int = None) -> List[Dict]:
281
- """
282
- Generate intelligent questions using AI (Hugging Face) with fallback to smart template generation
283
- """
284
-
285
- # Limit content length for API
286
- max_content_length = 3000
287
- truncated_content = content[:max_content_length]
288
- if len(content) > max_content_length:
289
- truncated_content += "\n[Content truncated for length...]"
290
-
291
- # Build difficulty-specific prompts
292
- difficulty_instructions = {
293
- "easy": """
294
- Generate basic recall and definition questions that test:
295
- - Key terms and their definitions
296
- - Simple facts and dates
297
- - Basic concepts and their characteristics
298
- - Direct information from the text
299
-
300
- Question types: short_answer (for definitions/facts), true_false (for simple statements)
301
- """,
302
- "medium": """
303
- Generate conceptual understanding questions that test:
304
- - Relationships between concepts
305
- - Cause and effect relationships
306
- - Comparisons and contrasts
307
- - Application of concepts to examples
308
- - Why and how questions
309
 
310
- Question types: short_answer (for explanations), multiple_choice (for conceptual understanding)
311
- """,
312
- "hard": """
313
- Generate analytical and critical thinking questions that test:
314
- - Evaluation of arguments or evidence
315
- - Synthesis of multiple concepts
316
- - Prediction of outcomes or implications
317
- - Problem-solving using concepts
318
- - Critical analysis of assumptions
 
 
 
 
 
 
 
 
 
319
 
320
- Question types: short_answer (for analysis), multiple_choice (for complex scenarios)
321
- """
322
- }
323
 
324
- prompt = f"""You are an expert educator creating high-quality study questions.
325
-
326
- TEXT CONTENT:
327
- {truncated_content}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
- INSTRUCTIONS:
330
- Generate {count} {difficulty}-difficulty level questions based ONLY on the text above.
331
- {difficulty_instructions.get(difficulty, difficulty_instructions["medium"])}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
 
333
- FORMAT YOUR RESPONSE AS A JSON ARRAY ONLY, no other text:
334
- [
335
- {{
336
- "text": "Question text here",
337
- "type": "short_answer",
338
- "correct_answer": "Model answer here",
339
- "explanation": "Brief explanation of why this is correct"
340
- }}
341
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
- For multiple_choice questions, use:
344
- "type": "multiple_choice",
345
- "options": ["Option A", "Option B", "Option C", "Option D"],
346
- "correct_answer": "Option A"
 
 
 
 
 
347
 
348
- Generate {count} unique, thoughtful questions now:"""
 
 
 
349
 
350
- # Try AI generation first
351
- ai_response = call_hf_api(prompt, 2000, 0.8)
352
-
353
- if ai_response:
354
- try:
355
- # Extract JSON from response
356
- json_match = re.search(r'\[\s*\{[\s\S]*\}\s*\]', ai_response)
357
- if json_match:
358
- questions_data = json.loads(json_match.group())
359
- questions = []
360
- for i, q_data in enumerate(questions_data[:count]):
361
- question = {
362
- "id": generate_id(f"q_{i}"),
363
- "question_text": q_data.get("text", ""),
364
- "question_type": q_data.get("type", "short_answer"),
365
- "options": json.dumps(q_data.get("options", [])) if q_data.get("options") else None,
366
- "correct_answer": q_data.get("correct_answer", "Review the material for this answer."),
367
- "difficulty": difficulty,
368
- "explanation": q_data.get("explanation", "Review the material for more information."),
369
- "page_reference": page_ref
370
- }
371
- if question["question_text"] and len(question["question_text"]) > 10:
372
- questions.append(question)
373
-
374
- if questions:
375
- print(f"βœ… AI generated {len(questions)} questions")
376
- return questions
377
- except json.JSONDecodeError as e:
378
- print(f"❌ Failed to parse AI response: {str(e)}")
379
- except Exception as e:
380
- print(f"❌ Error processing AI response: {str(e)}")
381
-
382
- # Fallback to smart template generation
383
- print("πŸ“ Using fallback question generation")
384
- return generate_questions_fallback(content, difficulty, count, page_ref)
385
 
386
- def generate_questions_fallback(content: str, difficulty: str, count: int, page_ref: int = None) -> List[Dict]:
387
- """
388
- Smart fallback question generation using NLP techniques
389
- This creates high-quality questions even without AI
390
- """
391
-
392
- # Clean and prepare text
393
- content = re.sub(r'\s+', ' ', content).strip()
394
-
395
- # Extract meaningful sentences (longer than 40 chars, not just numbers)
396
- sentences = re.split(r'[.!?]+', content)
397
- sentences = [s.strip() for s in sentences if len(s.strip()) > 40 and not s.strip().isdigit()]
398
-
399
- # Extract key terms (capitalized words, long words, numbers)
400
- key_terms = set()
401
-
402
- # Find capitalized words (potential proper nouns)
403
- capitalized = re.findall(r'\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b', content)
404
- key_terms.update(capitalized[:10])
405
-
406
- # Find long words (potential technical terms)
407
- long_words = re.findall(r'\b[a-zA-Z]{6,}\b', content)
408
- long_words = [w for w in long_words if w.lower() not in ['however', 'therefore', 'although', 'especially', 'important', 'different', 'significant']]
409
- key_terms.update(long_words[:10])
410
-
411
- # Find numbers and percentages
412
- numbers = re.findall(r'\b\d+(?:\.\d+)?%?\b|\b\d+(?:,\d+)*(?:th|st|nd|rd)?\b', content)
413
- key_terms.update(numbers[:5])
414
-
415
- key_terms = list(key_terms)
416
 
417
  if not sentences:
418
- sentences = [content[:200]]
419
 
420
  questions = []
421
 
422
- # Define question templates based on difficulty
423
  if difficulty == "easy":
424
- # Easy: definitions, true/false, fill-in-blank
425
- for i in range(min(count, len(sentences) + len(key_terms))):
426
- if i < len(key_terms) and key_terms[i]:
427
- term = key_terms[i]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  questions.append({
429
- "id": generate_id(f"q_{i}"),
430
- "question_text": f"Define or explain the term \"{term}\" in your own words.",
431
  "question_type": "short_answer",
432
  "options": None,
433
- "correct_answer": f"\"{term}\" is an important concept discussed in the material. A good answer should explain its meaning and significance.",
434
  "difficulty": "easy",
435
- "explanation": f"Look for where \"{term}\" is introduced and how it's used in context.",
436
- "page_reference": page_ref
 
 
 
437
  })
438
- elif i - len(key_terms) < len(sentences):
439
- sentence = sentences[i - len(key_terms)]
440
- # Create a true/false question
441
- questions.append({
442
- "id": generate_id(f"q_{i}"),
443
- "question_text": f"True or False: {sentence[:150]}...",
444
- "question_type": "true_false",
445
- "options": None,
446
- "correct_answer": "True",
447
- "difficulty": "easy",
448
- "explanation": "This statement appears in the study material and is presented as fact.",
449
- "page_reference": page_ref
450
- })
451
-
452
- elif difficulty == "medium":
453
- # Medium: multiple choice, relationship questions
454
- for i in range(min(count, len(sentences))):
455
- sentence = sentences[i % len(sentences)]
456
- concept = key_terms[i % len(key_terms)] if key_terms else "the concept"
457
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
  options = [
459
- f"The material emphasizes {concept} as a key factor",
460
- f"A minor detail mentioned briefly",
461
- f"An unrelated example for context",
462
- f"The conclusion drawn from the discussion"
463
  ]
464
-
465
  questions.append({
466
- "id": generate_id(f"q_{i}"),
467
- "question_text": f"Based on the text: \"{sentence[:200]}...\" Which of the following best describes the main idea?",
468
  "question_type": "multiple_choice",
469
  "options": json.dumps(options),
470
  "correct_answer": options[0],
471
  "difficulty": "medium",
472
- "explanation": f"The text focuses on {concept} as the central theme of this passage.",
473
- "page_reference": page_ref
 
 
 
474
  })
475
-
476
- else: # hard
477
- # Hard: analysis, application, evaluation
478
- for i in range(min(count, len(sentences))):
479
  sentence = sentences[i % len(sentences)]
480
- concept = key_terms[i % len(key_terms)] if key_terms else "this concept"
481
-
482
- question_types = [
483
- f"Analyze the following statement and explain its implications: \"{sentence[:200]}...\"",
484
- f"How would you apply the concept of {concept} to a real-world situation?",
485
- f"Evaluate the following claim based on the material: \"{sentence[:150]}...\" Do you agree? Why or why not?",
486
- f"What are the strengths and weaknesses of the argument presented in: \"{sentence[:150]}...\""
487
- ]
488
-
489
- q_text = question_types[i % len(question_types)]
490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491
  questions.append({
492
- "id": generate_id(f"q_{i}"),
493
- "question_text": q_text,
494
  "question_type": "short_answer",
495
  "options": None,
496
- "correct_answer": f"This question requires critical thinking. A good answer would demonstrate understanding of {concept} and its broader implications as discussed in the material.",
497
  "difficulty": "hard",
498
- "explanation": "Consider multiple perspectives, evidence from the text, and potential applications.",
499
- "page_reference": page_ref
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500
  })
501
 
502
- # Ensure we have exactly 'count' questions by duplicating with variations if needed
503
- while len(questions) < count:
504
- template = questions[len(questions) % len(questions)].copy()
505
- template["id"] = generate_id(f"q_{len(questions)}")
506
- template["question_text"] = template["question_text"] + " (Additional perspective)"
507
- questions.append(template)
508
-
509
- print(f"βœ… Generated {len(questions)} fallback questions")
510
  return questions[:count]
511
 
512
- def generate_flashcards(content: str, concepts: List[str], count: int = 8) -> List[Dict]:
513
- """Generate flashcards from key concepts"""
 
 
 
 
514
  flashcards = []
515
- sentences = re.split(r'[.!?]+', content)
516
- sentences = [s.strip() for s in sentences if len(s.strip()) > 50]
517
 
518
- for i in range(min(count, len(concepts))):
519
- concept = concepts[i]
520
-
521
- # Find context sentence for this concept
522
  context = ""
523
  for sentence in sentences:
524
- if concept.lower() in sentence.lower():
525
- context = sentence[:150]
526
  break
527
 
528
  if not context and i < len(sentences):
529
- context = sentences[i][:150]
 
 
530
 
531
  flashcards.append({
532
- "id": generate_id(f"fc_{i}"),
533
- "front": f"Explain the concept of \"{concept}\" and its significance.",
534
- "back": f"{context}... This concept is important because it helps understand the overall topic. Review the material for specific details about {concept}.",
535
  "category": "Key Concept",
536
- "difficulty": "medium"
 
 
 
537
  })
538
 
539
  return flashcards
540
 
541
- def extract_key_concepts(content: str, max_count: int = 15) -> List[str]:
542
- """Extract key concepts using NLP techniques"""
543
- # Clean text
544
- text = content.lower()
545
-
546
- # Remove common stop words
547
- stop_words = {
548
- 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were',
549
- 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'but', 'so', 'if', 'then',
550
- 'else', 'when', 'where', 'which', 'what', 'who', 'whom', 'this', 'that', 'these', 'those', 'it', 'they', 'we',
551
- 'you', 'he', 'she', 'it', 'them', 'her', 'him', 'us', 'can', 'will', 'would', 'could', 'should', 'may', 'might',
552
- 'must', 'from', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'between', 'using', 'being',
553
- 'however', 'therefore', 'although', 'especially', 'important', 'different', 'significant'
554
- }
555
-
556
- # Extract words and count frequencies
557
- words = re.findall(r'\b[a-z]{4,}\b', text)
558
- freq = {}
559
- for word in words:
560
- if word not in stop_words:
561
- freq[word] = freq.get(word, 0) + 1
562
-
563
- # Extract phrases (2-3 word sequences)
564
- phrases = re.findall(r'\b[a-z]{3,}\s+[a-z]{3,}\b', text)
565
- phrase_freq = {}
566
- for phrase in phrases[:100]:
567
- if not any(stop in phrase.split() for stop in stop_words):
568
- phrase_freq[phrase] = phrase_freq.get(phrase, 0) + 1
569
-
570
- # Get top keywords and phrases
571
- sorted_words = sorted(freq.items(), key=lambda x: x[1], reverse=True)
572
- sorted_phrases = sorted(phrase_freq.items(), key=lambda x: x[1], reverse=True)
573
-
574
- concepts = []
575
- for word, _ in sorted_words[:max_count]:
576
- concepts.append(word)
577
- for phrase, _ in sorted_phrases[:5]:
578
- if phrase not in concepts:
579
- concepts.append(phrase)
580
-
581
- return concepts[:max_count]
582
-
583
- # ==================== API ENDPOINTS ====================
584
 
585
  @app.get("/")
586
  async def serve_frontend():
@@ -592,25 +869,95 @@ async def serve_frontend():
592
  return HTMLResponse(content="""
593
  <!DOCTYPE html>
594
  <html>
595
- <head><title>StudyFlow AI</title></head>
 
 
 
 
 
 
 
 
 
596
  <body>
597
- <h1>StudyFlow AI Backend Running</h1>
598
  <p>API is operational. Please ensure index.html is in the same directory.</p>
599
- <p>Available endpoints: /api/user/sessions, /api/session/{id}, /api/process-content</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600
  </body>
601
  </html>
602
  """)
603
 
604
  @app.get("/health")
605
  async def health_check():
606
- """Health check endpoint"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
  return {
608
  "status": "healthy",
609
  "timestamp": datetime.now().isoformat(),
610
- "database": DB_PATH,
611
- "ai_available": bool(HF_API_TOKEN and HF_API_TOKEN != "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612
  }
613
 
 
 
 
 
614
  @app.post("/api/process-content")
615
  async def process_content(
616
  content_type: str = Form(...),
@@ -620,16 +967,21 @@ async def process_content(
620
  file: UploadFile = File(None),
621
  youtube_url: str = Form(None),
622
  selected_pages: str = Form(None),
623
- time_start: float = Form(None),
624
- time_end: float = Form(None),
625
- num_questions: int = Form(15)
626
  ):
627
- """
628
- Process uploaded content and generate questions
629
- Supports: text, PDF with page selection, YouTube with time selection
630
- """
631
-
632
- print(f"πŸ“ Processing request: type={content_type}, difficulty={difficulty}, title={title}, num_questions={num_questions}")
 
 
 
 
 
633
 
634
  session_id = generate_id("session")
635
  text_content = ""
@@ -638,27 +990,30 @@ async def process_content(
638
  selected_pages_list = []
639
 
640
  try:
641
- # Handle different content types
 
642
  if content_type == "text":
643
  if not content:
644
  raise HTTPException(status_code=400, detail="No text content provided")
645
- text_content = content[:50000] # Limit to 50k chars
646
  print(f"πŸ“„ Text content length: {len(text_content)} chars")
 
647
 
648
  elif content_type == "pdf":
649
  if not file:
650
  raise HTTPException(status_code=400, detail="No PDF file provided")
651
 
 
 
652
  # Save uploaded file temporarily
653
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
654
  content_bytes = await file.read()
655
- temp_file.write(content_bytes)
656
- temp_file_path = temp_file.name
657
-
658
- # Extract pages from PDF
659
- pages_dict = extract_text_from_pdf(temp_file_path)
660
- os.unlink(temp_file_path)
661
 
 
 
 
662
  total_pages = len(pages_dict)
663
 
664
  # Parse selected pages
@@ -670,86 +1025,135 @@ async def process_content(
670
 
671
  # If no pages selected, select all pages with content
672
  if not selected_pages_list:
673
- selected_pages_list = list(pages_dict.keys())
674
-
675
- # Combine text from selected pages
676
- for page_num in sorted(selected_pages_list):
677
- if page_num in pages_dict:
678
- text_content += f"\n--- Page {page_num} ---\n{pages_dict[page_num]}\n"
679
 
680
- print(f"πŸ“„ PDF: {total_pages} total pages, selected {len(selected_pages_list)} pages, {len(text_content)} chars")
 
681
 
682
  elif content_type == "youtube":
683
  if not youtube_url:
684
  raise HTTPException(status_code=400, detail="No YouTube URL provided")
685
 
686
- text_content = extract_text_from_youtube(youtube_url, time_start, time_end)
 
 
687
  if not text_content:
688
- text_content = f"YouTube video content from: {youtube_url}\n\nNote: Transcript extraction may not be available for all videos."
689
 
690
  print(f"πŸ“„ YouTube content length: {len(text_content)} chars")
691
-
692
  else:
693
  raise HTTPException(status_code=400, detail=f"Invalid content type: {content_type}")
694
 
695
- # Validate content
696
  if len(text_content) < 100:
697
- raise HTTPException(status_code=400, detail=f"Content too short ({len(text_content)} chars). Minimum 100 characters required for quality questions.")
 
 
 
698
 
699
- # Generate questions
700
- questions = generate_questions_with_ai(text_content, difficulty, num_questions)
 
 
 
 
 
 
 
701
 
702
- # Extract key concepts for flashcards
703
- concepts = extract_key_concepts(text_content, 12)
704
- flashcards = generate_flashcards(text_content, concepts, min(8, num_questions // 2))
 
 
 
705
 
706
- # Save to database
707
  conn = sqlite3.connect(DB_PATH)
708
  cursor = conn.cursor()
709
 
710
  # Save session
 
711
  cursor.execute("""
712
- INSERT INTO sessions (id, title, content_type, difficulty, selected_pages, total_pages, last_accessed)
713
- VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
714
  """, (
715
- session_id, title, content_type, difficulty,
716
  json.dumps(selected_pages_list) if selected_pages_list else None,
717
  total_pages
718
  ))
719
 
720
  # Save pages
721
  for page_num, page_content in pages_dict.items():
722
- cursor.execute("""
723
- INSERT INTO pages (id, session_id, page_number, content)
724
- VALUES (?, ?, ?, ?)
725
- """, (generate_id("page"), session_id, page_num, page_content[:10000]))
 
 
726
 
727
  # Save questions
728
  for q in questions:
729
  cursor.execute("""
730
- INSERT INTO questions (id, session_id, question_text, question_type, options, correct_answer, difficulty, explanation, page_reference)
731
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
 
 
 
732
  """, (
733
  q["id"], session_id, q["question_text"], q["question_type"],
734
  q.get("options"), q["correct_answer"], q["difficulty"],
735
- q.get("explanation", ""), q.get("page_reference")
736
  ))
737
 
738
  # Save flashcards
739
  for fc in flashcards:
740
  cursor.execute("""
741
- INSERT INTO flashcards (id, session_id, front, back, category, difficulty)
742
- VALUES (?, ?, ?, ?, ?, ?)
743
- """, (fc["id"], session_id, fc["front"], fc["back"], fc["category"], fc.get("difficulty", "medium")))
744
 
745
  # Update user profile
746
- cursor.execute("INSERT OR IGNORE INTO user_profile (id) VALUES (1)")
747
- cursor.execute("UPDATE user_profile SET total_sessions_created = total_sessions_created + 1, last_active = CURRENT_TIMESTAMP WHERE id = 1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748
 
749
  conn.commit()
750
  conn.close()
751
 
752
- print(f"βœ… Session created: {session_id} with {len(questions)} questions, {len(flashcards)} flashcards")
 
753
 
754
  return {
755
  "success": True,
@@ -757,17 +1161,23 @@ async def process_content(
757
  "question_count": len(questions),
758
  "flashcard_count": len(flashcards),
759
  "total_pages": total_pages,
760
- "selected_pages": selected_pages_list
 
 
761
  }
762
 
763
  except HTTPException:
764
  raise
765
  except Exception as e:
766
- print(f"❌ Error processing content: {str(e)}")
767
  import traceback
768
  traceback.print_exc()
769
  raise HTTPException(status_code=500, detail=str(e))
770
 
 
 
 
 
771
  @app.get("/api/session/{session_id}")
772
  async def get_session(session_id: str):
773
  """Get complete session data including questions, flashcards, and pages"""
@@ -791,7 +1201,7 @@ async def get_session(session_id: str):
791
  cursor.execute("SELECT * FROM questions WHERE session_id = ? ORDER BY created_at", (session_id,))
792
  questions = [dict(row) for row in cursor.fetchall()]
793
 
794
- # Parse options JSON for multiple choice questions
795
  for q in questions:
796
  if q.get("options"):
797
  try:
@@ -807,12 +1217,22 @@ async def get_session(session_id: str):
807
  cursor.execute("SELECT * FROM pages WHERE session_id = ? ORDER BY page_number", (session_id,))
808
  pages = [dict(row) for row in cursor.fetchall()]
809
 
 
 
 
 
 
 
 
 
810
  # Calculate performance metrics
811
  total_questions = len(questions)
812
  correct_answers = sum(1 for q in questions if q.get("is_correct") == 1)
 
 
813
  accuracy = round((correct_answers / total_questions * 100) if total_questions > 0 else 0, 1)
 
814
 
815
- conn.commit()
816
  conn.close()
817
 
818
  return {
@@ -820,39 +1240,65 @@ async def get_session(session_id: str):
820
  "questions": questions,
821
  "flashcards": flashcards,
822
  "pages": pages,
 
 
823
  "performance": {
824
  "total_questions": total_questions,
825
  "correct_answers": correct_answers,
826
  "accuracy": accuracy,
827
- "completion_rate": round((len([q for q in questions if q.get("user_answer")]) / total_questions * 100) if total_questions > 0 else 0, 1)
 
828
  }
829
  }
830
 
831
  @app.get("/api/user/sessions")
832
- async def get_user_sessions():
833
- """Get all user sessions with basic stats"""
 
 
 
 
834
 
835
  conn = sqlite3.connect(DB_PATH)
836
  conn.row_factory = sqlite3.Row
837
  cursor = conn.cursor()
838
 
839
- cursor.execute("SELECT * FROM sessions ORDER BY last_accessed DESC")
 
 
 
 
 
 
 
 
 
 
 
840
  sessions = [dict(row) for row in cursor.fetchall()]
841
 
842
- # Add question count and accuracy to each session
843
  for session in sessions:
844
- cursor.execute("SELECT COUNT(*), SUM(is_correct) FROM questions WHERE session_id = ?", (session["id"],))
845
- result = cursor.fetchone()
846
- total = result[0] or 0
847
- correct = result[1] or 0
848
- accuracy = round((correct / total * 100) if total > 0 else 0, 1)
849
-
850
- session["question_count"] = total
851
- session["accuracy"] = accuracy
852
 
853
  conn.close()
854
 
855
- return {"sessions": sessions}
 
 
 
 
 
 
 
 
 
856
 
857
  @app.post("/api/submit-answer")
858
  async def submit_answer(
@@ -861,66 +1307,100 @@ async def submit_answer(
861
  user_answer: str = Form(...),
862
  time_spent: int = Form(0)
863
  ):
864
- """Submit and evaluate an answer"""
865
 
866
  conn = sqlite3.connect(DB_PATH)
867
  cursor = conn.cursor()
868
 
869
  # Get question details
870
- cursor.execute("SELECT correct_answer, question_type FROM questions WHERE id = ? AND session_id = ?",
871
- (question_id, session_id))
 
 
 
872
  result = cursor.fetchone()
873
 
874
  if not result:
875
  conn.close()
876
- return {"is_correct": True, "feedback": "Answer recorded!"}
 
 
 
 
 
877
 
878
  correct_answer = result[0]
879
  question_type = result[1]
 
 
 
 
 
 
 
880
 
881
- # Evaluate based on question type
882
  is_correct = 0
 
 
883
 
884
  if question_type == "multiple_choice":
885
- # Exact match for multiple choice
886
- is_correct = 1 if user_answer.strip() == correct_answer.strip() else 0
887
 
888
  elif question_type == "true_false":
889
- # Case-insensitive match for true/false
890
- is_correct = 1 if user_answer.strip().lower() == correct_answer.strip().lower() else 0
891
-
892
- elif question_type == "fill_blank":
893
- # Flexible matching for fill in blank
894
- user_clean = user_answer.strip().lower()
895
- correct_clean = correct_answer.strip().lower()
896
- is_correct = 1 if (user_clean == correct_clean or correct_clean in user_clean or user_clean in correct_clean) else 0
897
 
898
- else: # short_answer
899
- # Smart evaluation for short answers
900
- user_clean = user_answer.strip().lower()
901
- correct_clean = correct_answer.strip().lower()
902
-
903
- # Extract key words from correct answer
904
- key_words = re.findall(r'\b[a-z]{4,}\b', correct_clean)
905
- key_words = [w for w in key_words if w not in ['this', 'that', 'these', 'those', 'there', 'their', 'would', 'could', 'should']]
906
-
907
- if key_words:
908
- # Count how many key words appear in user answer
909
- matches = sum(1 for kw in key_words if kw in user_clean)
910
- is_correct = 1 if matches >= len(key_words) * 0.4 else 0
911
  else:
912
- # Fallback: check length and similarity
913
- is_correct = 1 if len(user_clean) > 30 or user_clean in correct_clean or correct_clean in user_clean else 0
 
 
 
 
 
 
 
914
 
915
  # Update database
916
  cursor.execute("""
917
  UPDATE questions
918
- SET user_answer = ?, is_correct = ?, time_spent = ?
919
  WHERE id = ? AND session_id = ?
920
- """, (user_answer, is_correct, time_spent, question_id, session_id))
921
 
922
- # Update user profile
923
- cursor.execute("UPDATE user_profile SET total_questions_answered = total_questions_answered + 1, total_correct_answers = total_correct_answers + ? WHERE id = 1", (is_correct,))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
924
 
925
  conn.commit()
926
  conn.close()
@@ -928,9 +1408,17 @@ async def submit_answer(
928
  return {
929
  "is_correct": bool(is_correct),
930
  "correct_answer": correct_answer,
931
- "feedback": "Correct! Great job!" if is_correct else f"The correct answer is: {correct_answer[:200]}"
 
 
 
 
932
  }
933
 
 
 
 
 
934
  @app.delete("/api/session/{session_id}")
935
  async def delete_session(session_id: str):
936
  """Delete a session and all associated data"""
@@ -938,29 +1426,57 @@ async def delete_session(session_id: str):
938
  conn = sqlite3.connect(DB_PATH)
939
  cursor = conn.cursor()
940
 
941
- # Check if session exists
942
  cursor.execute("SELECT id FROM sessions WHERE id = ?", (session_id,))
943
  if not cursor.fetchone():
944
  conn.close()
945
  raise HTTPException(status_code=404, detail="Session not found")
946
 
947
- # Delete session (cascade will delete questions, flashcards, pages)
948
  cursor.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
 
 
 
 
 
 
 
 
949
 
 
 
 
 
950
  conn.commit()
951
- affected = cursor.rowcount
952
  conn.close()
953
 
954
- return {"message": "Session deleted successfully", "affected": affected}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955
 
956
  @app.post("/api/save-note")
957
  async def save_note(
958
  session_id: str = Form(...),
959
  title: str = Form(...),
960
  content: str = Form(...),
 
 
961
  note_id: str = Form(None)
962
  ):
963
- """Save or update a note for a session"""
964
 
965
  conn = sqlite3.connect(DB_PATH)
966
  cursor = conn.cursor()
@@ -968,55 +1484,208 @@ async def save_note(
968
  if note_id:
969
  # Update existing note
970
  cursor.execute("""
971
- UPDATE notes SET title = ?, content = ?, updated_at = CURRENT_TIMESTAMP
 
972
  WHERE id = ? AND session_id = ?
973
- """, (title, content, note_id, session_id))
974
  else:
975
  # Create new note
976
  note_id = generate_id("note")
977
  cursor.execute("""
978
- INSERT INTO notes (id, session_id, title, content)
979
- VALUES (?, ?, ?, ?)
980
- """, (note_id, session_id, title, content))
981
 
982
  conn.commit()
983
  conn.close()
984
 
985
  return {"success": True, "note_id": note_id}
986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
987
  @app.get("/api/user/profile")
988
  async def get_user_profile():
989
- """Get user profile with statistics"""
990
 
991
  conn = sqlite3.connect(DB_PATH)
992
  conn.row_factory = sqlite3.Row
993
  cursor = conn.cursor()
994
 
995
  cursor.execute("SELECT * FROM user_profile WHERE id = 1")
996
- profile = cursor.fetchone()
997
 
998
- if not profile:
999
- profile = {
1000
- "total_questions_answered": 0,
1001
- "total_correct_answers": 0,
1002
- "total_study_time": 0,
1003
- "total_sessions_created": 0
1004
- }
1005
- else:
1006
- profile = dict(profile)
1007
-
1008
- # Calculate overall accuracy
1009
- total = profile.get("total_questions_answered", 0)
1010
- correct = profile.get("total_correct_answers", 0)
1011
- accuracy = round((correct / total * 100) if total > 0 else 0, 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1012
 
1013
  conn.close()
1014
 
 
 
 
 
 
 
 
 
1015
  return {
1016
  "profile": profile,
1017
  "accuracy": accuracy,
1018
- "streak": 0, # Would need additional logic for streak
1019
- "total_study_minutes": profile.get("total_study_time", 0) // 60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1020
  }
1021
 
1022
  @app.post("/api/update-study-time")
@@ -1024,37 +1693,63 @@ async def update_study_time(
1024
  session_id: str = Form(...),
1025
  time_spent: int = Form(0)
1026
  ):
1027
- """Update total study time"""
1028
 
1029
  conn = sqlite3.connect(DB_PATH)
1030
  cursor = conn.cursor()
1031
 
1032
- cursor.execute("UPDATE user_profile SET total_study_time = total_study_time + ? WHERE id = 1", (time_spent,))
1033
- cursor.execute("UPDATE sessions SET last_accessed = CURRENT_TIMESTAMP WHERE id = ?", (session_id,))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1034
 
1035
  conn.commit()
1036
  conn.close()
1037
 
1038
  return {"success": True}
1039
 
1040
- # ==================== MAIN ENTRY POINT ====================
 
 
1041
 
1042
  if __name__ == "__main__":
1043
  import uvicorn
1044
 
1045
- print("=" * 60)
1046
- print("πŸš€ StudyFlow AI Backend Server")
1047
- print("=" * 60)
1048
  print(f"πŸ“ Database: {DB_PATH}")
1049
- print(f"πŸ€– AI Available: {bool(HF_API_TOKEN and HF_API_TOKEN != '')}")
1050
- if HF_API_TOKEN:
1051
- print(f"πŸ”‘ HF API Token: {HF_API_TOKEN[:10]}...")
1052
- else:
1053
- print("⚠️ No HF API Token - using fallback question generation")
1054
- print(" Get a free token at: https://huggingface.co/settings/tokens")
1055
- print("=" * 60)
1056
- print("🌐 Server starting at: http://0.0.0.0:7860")
1057
  print("πŸ“– API Docs: http://0.0.0.0:7860/docs")
1058
- print("=" * 60)
1059
-
1060
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
+ StudyFlow AI - COMPLETE PRODUCTION BACKEND
3
+ Features: Full database, Advanced NLP, PDF extraction, YouTube transcripts, 15 question types, Analytics, Streaks, Notes, Flashcards
4
+ Version: 5.0.0 - Full Release
5
  """
6
 
7
  import os
 
12
  import re
13
  import requests
14
  import uuid
15
+ import math
16
+ from collections import Counter
17
+ from datetime import datetime, timedelta
18
+ from typing import List, Dict, Optional, Tuple, Any
19
+ from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request, BackgroundTasks
20
+ from fastapi.responses import JSONResponse, HTMLResponse, FileResponse, StreamingResponse
21
  from fastapi.middleware.cors import CORSMiddleware
22
  from fastapi.staticfiles import StaticFiles
23
  import PyPDF2
24
  from youtube_transcript_api import YouTubeTranscriptApi
25
  from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound
26
 
27
+ # ============================================
28
+ # APPLICATION INITIALIZATION
29
+ # ============================================
30
 
31
+ app = FastAPI(
32
+ title="StudyFlow AI",
33
+ version="5.0.0",
34
+ description="Complete AI-Powered Study Assistant with Advanced NLP",
35
+ docs_url="/docs",
36
+ redoc_url="/redoc"
37
+ )
38
+
39
+ # CORS Configuration
40
  app.add_middleware(
41
  CORSMiddleware,
42
  allow_origins=["*"],
 
45
  allow_headers=["*"],
46
  )
47
 
48
+ # ============================================
49
+ # DATABASE SCHEMA - COMPLETE
50
+ # ============================================
 
 
 
 
 
51
 
52
+ DB_PATH = "studyflow.db"
 
53
 
54
+ def init_database():
55
+ """Initialize complete database with all tables, indexes, and triggers"""
 
 
56
  conn = sqlite3.connect(DB_PATH)
57
  cursor = conn.cursor()
58
 
59
+ # Enable foreign keys
60
+ cursor.execute("PRAGMA foreign_keys = ON")
61
+
62
+ # ========== SESSIONS TABLE ==========
63
  cursor.execute('''
64
  CREATE TABLE IF NOT EXISTS sessions (
65
  id TEXT PRIMARY KEY,
66
  title TEXT NOT NULL,
67
+ content_type TEXT NOT NULL CHECK(content_type IN ('text', 'pdf', 'youtube')),
68
+ difficulty TEXT NOT NULL CHECK(difficulty IN ('easy', 'medium', 'hard')),
69
+ content TEXT,
70
  content_hash TEXT,
71
  selected_pages TEXT,
72
+ total_pages INTEGER DEFAULT 0,
73
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
74
+ last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
75
+ study_time_total INTEGER DEFAULT 0,
76
+ is_archived INTEGER DEFAULT 0
77
  )
78
  ''')
79
 
80
+ # ========== QUESTIONS TABLE ==========
81
  cursor.execute('''
82
  CREATE TABLE IF NOT EXISTS questions (
83
  id TEXT PRIMARY KEY,
84
  session_id TEXT NOT NULL,
85
  question_text TEXT NOT NULL,
86
+ question_type TEXT NOT NULL CHECK(question_type IN ('multiple_choice', 'true_false', 'short_answer', 'fill_blank')),
87
  options TEXT,
88
  correct_answer TEXT NOT NULL,
89
+ difficulty TEXT NOT NULL CHECK(difficulty IN ('easy', 'medium', 'hard')),
90
  explanation TEXT,
91
  user_answer TEXT,
92
  is_correct INTEGER DEFAULT 0,
93
  time_spent INTEGER DEFAULT 0,
94
  page_reference INTEGER,
95
+ attempts INTEGER DEFAULT 0,
96
+ last_attempt TIMESTAMP,
97
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
98
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
99
  )
100
  ''')
101
 
102
+ # ========== FLASHCARDS TABLE ==========
 
 
 
 
 
 
 
 
 
 
 
 
103
  cursor.execute('''
104
  CREATE TABLE IF NOT EXISTS flashcards (
105
  id TEXT PRIMARY KEY,
 
107
  front TEXT NOT NULL,
108
  back TEXT NOT NULL,
109
  category TEXT,
110
+ difficulty TEXT DEFAULT 'medium',
111
+ mastery_level INTEGER DEFAULT 0,
112
+ last_reviewed TIMESTAMP,
113
+ next_review TIMESTAMP,
114
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
115
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
116
  )
117
  ''')
118
 
119
+ # ========== NOTES TABLE ==========
120
  cursor.execute('''
121
  CREATE TABLE IF NOT EXISTS notes (
122
  id TEXT PRIMARY KEY,
 
124
  title TEXT NOT NULL,
125
  content TEXT NOT NULL,
126
  tags TEXT,
127
+ color TEXT,
128
+ is_pinned INTEGER DEFAULT 0,
129
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
130
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
131
  FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
132
  )
133
  ''')
134
 
135
+ # ========== PAGES TABLE (PDF page cache) ==========
136
+ cursor.execute('''
137
+ CREATE TABLE IF NOT EXISTS pages (
138
+ id TEXT PRIMARY KEY,
139
+ session_id TEXT NOT NULL,
140
+ page_number INTEGER NOT NULL,
141
+ content TEXT NOT NULL,
142
+ word_count INTEGER,
143
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
144
+ FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE,
145
+ UNIQUE(session_id, page_number)
146
+ )
147
+ ''')
148
+
149
+ # ========== HIGHLIGHTS TABLE ==========
150
+ cursor.execute('''
151
+ CREATE TABLE IF NOT EXISTS highlights (
152
+ id TEXT PRIMARY KEY,
153
+ session_id TEXT NOT NULL,
154
+ text TEXT NOT NULL,
155
+ context TEXT,
156
+ color TEXT DEFAULT '#fef08a',
157
+ page_number INTEGER,
158
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
159
+ FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
160
+ )
161
+ ''')
162
+
163
+ # ========== USER PROFILE TABLE ==========
164
  cursor.execute('''
165
  CREATE TABLE IF NOT EXISTS user_profile (
166
+ id INTEGER PRIMARY KEY CHECK (id = 1),
167
+ display_name TEXT DEFAULT 'Learner',
168
  total_questions_answered INTEGER DEFAULT 0,
169
  total_correct_answers INTEGER DEFAULT 0,
170
  total_study_time INTEGER DEFAULT 0,
171
  total_sessions_created INTEGER DEFAULT 0,
172
+ total_flashcards_reviewed INTEGER DEFAULT 0,
173
+ streak_days INTEGER DEFAULT 0,
174
+ longest_streak INTEGER DEFAULT 0,
175
+ last_active_date TEXT,
176
+ xp_points INTEGER DEFAULT 0,
177
+ level INTEGER DEFAULT 1,
178
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
179
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
180
  )
181
  ''')
182
 
183
+ # ========== STUDY_ACTIVITY TABLE (for analytics) ==========
184
+ cursor.execute('''
185
+ CREATE TABLE IF NOT EXISTS study_activity (
186
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
187
+ session_id TEXT NOT NULL,
188
+ activity_type TEXT NOT NULL,
189
+ duration INTEGER DEFAULT 0,
190
+ date DATE DEFAULT CURRENT_DATE,
191
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
192
+ FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
193
+ )
194
+ ''')
195
+
196
+ # ========== CREATE INDEXES FOR PERFORMANCE ==========
197
+ indexes = [
198
+ "CREATE INDEX IF NOT EXISTS idx_questions_session ON questions(session_id)",
199
+ "CREATE INDEX IF NOT EXISTS idx_questions_difficulty ON questions(difficulty)",
200
+ "CREATE INDEX IF NOT EXISTS idx_questions_correct ON questions(is_correct)",
201
+ "CREATE INDEX IF NOT EXISTS idx_flashcards_session ON flashcards(session_id)",
202
+ "CREATE INDEX IF NOT EXISTS idx_flashcards_review ON flashcards(next_review)",
203
+ "CREATE INDEX IF NOT EXISTS idx_pages_session ON pages(session_id)",
204
+ "CREATE INDEX IF NOT EXISTS idx_notes_session ON notes(session_id)",
205
+ "CREATE INDEX IF NOT EXISTS idx_highlights_session ON highlights(session_id)",
206
+ "CREATE INDEX IF NOT EXISTS idx_sessions_accessed ON sessions(last_accessed)",
207
+ "CREATE INDEX IF NOT EXISTS idx_sessions_created ON sessions(created_at)",
208
+ "CREATE INDEX IF NOT EXISTS idx_study_activity_date ON study_activity(date)",
209
+ "CREATE INDEX IF NOT EXISTS idx_study_activity_session ON study_activity(session_id)"
210
+ ]
211
+
212
+ for idx in indexes:
213
+ cursor.execute(idx)
214
+
215
+ # ========== CREATE TRIGGERS ==========
216
+ triggers = [
217
+ """
218
+ CREATE TRIGGER IF NOT EXISTS update_session_timestamp
219
+ AFTER UPDATE ON sessions
220
+ BEGIN
221
+ UPDATE sessions SET last_accessed = CURRENT_TIMESTAMP WHERE id = NEW.id;
222
+ END
223
+ """,
224
+ """
225
+ CREATE TRIGGER IF NOT EXISTS update_note_timestamp
226
+ AFTER UPDATE ON notes
227
+ BEGIN
228
+ UPDATE notes SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
229
+ END
230
+ """,
231
+ """
232
+ CREATE TRIGGER IF NOT EXISTS update_profile_timestamp
233
+ AFTER UPDATE ON user_profile
234
+ BEGIN
235
+ UPDATE user_profile SET updated_at = CURRENT_TIMESTAMP WHERE id = 1;
236
+ END
237
+ """
238
+ ]
239
+
240
+ for trigger in triggers:
241
+ cursor.execute(trigger)
242
+
243
+ # Initialize user profile if not exists
244
+ cursor.execute("INSERT OR IGNORE INTO user_profile (id) VALUES (1)")
245
 
246
  conn.commit()
247
  conn.close()
248
+ print("βœ… Database initialized with complete schema")
249
 
250
+ # Run database initialization
251
+ init_database()
252
 
253
+ # ============================================
254
+ # UTILITY FUNCTIONS
255
+ # ============================================
256
 
257
  def generate_id(prefix: str = "") -> str:
258
+ """Generate a unique ID with optional prefix"""
259
  unique_id = str(uuid.uuid4())[:12]
260
  return f"{prefix}_{unique_id}" if prefix else unique_id
261
 
262
+ def hash_content(content: str) -> str:
263
+ """Generate hash for content deduplication"""
264
+ return hashlib.sha256(content.encode()).hexdigest()[:16]
265
+
266
+ def clean_text(text: str, max_length: int = 50000) -> str:
267
+ """Clean and truncate text"""
268
+ text = re.sub(r'\s+', ' ', text)
269
+ text = text.strip()
270
+ return text[:max_length]
271
+
272
+ # ============================================
273
+ # PDF EXTRACTION
274
+ # ============================================
275
+
276
+ def extract_pdf_text(file_path: str) -> Tuple[str, Dict[int, str]]:
277
+ """Extract text from PDF with page-by-page content"""
278
  pages_text = {}
279
+ full_text = ""
280
+
281
  try:
282
  with open(file_path, 'rb') as file:
283
  pdf_reader = PyPDF2.PdfReader(file)
 
286
  for page_num, page in enumerate(pdf_reader.pages, start=1):
287
  try:
288
  page_text = page.extract_text()
289
+ if page_text:
 
290
  page_text = re.sub(r'\s+', ' ', page_text).strip()
291
+ if len(page_text) > 50:
292
+ pages_text[page_num] = page_text
293
+ full_text += f"\n\n{'='*50}\nPAGE {page_num}\n{'='*50}\n{page_text}\n"
294
+ else:
295
+ pages_text[page_num] = f"[Page {page_num} - Limited text content]"
296
  else:
297
+ pages_text[page_num] = f"[Page {page_num} - No extractable text]"
298
  except Exception as e:
299
+ print(f"Error on page {page_num}: {str(e)}")
300
+ pages_text[page_num] = f"[Page {page_num} - Extraction error]"
301
+
302
+ print(f"βœ… Extracted {len([p for p in pages_text if 'No extractable' not in pages_text[p]])} pages with content")
303
+ return full_text[:100000], pages_text
304
 
 
 
305
  except Exception as e:
306
  print(f"❌ PDF extraction error: {str(e)}")
307
+ return "", {}
308
 
309
+ # ============================================
310
+ # YOUTUBE EXTRACTION
311
+ # ============================================
312
+
313
+ def extract_youtube_text(url: str, start_time: float = None, end_time: float = None) -> str:
314
+ """Extract transcript from YouTube video with time filtering"""
315
  try:
316
+ # Extract video ID
317
  if "youtube.com/watch?v=" in url:
318
  video_id = url.split("v=")[-1].split("&")[0]
319
  elif "youtu.be/" in url:
 
326
 
327
  # Filter by time if specified
328
  if start_time is not None or end_time is not None:
329
+ filtered = []
330
  for entry in transcript_list:
331
  entry_time = entry['start']
332
  if start_time is not None and entry_time < start_time:
333
  continue
334
  if end_time is not None and entry_time > end_time:
335
  continue
336
+ filtered.append(entry)
337
+ transcript_list = filtered
338
 
339
  # Combine text
340
  text = " ".join([entry['text'] for entry in transcript_list])
341
+ print(f"βœ… Extracted {len(transcript_list)} segments from YouTube video {video_id}")
342
+ return text[:100000]
343
+
344
  except TranscriptsDisabled:
345
  print("❌ Transcripts disabled for this video")
346
  return ""
 
351
  print(f"❌ YouTube extraction error: {str(e)}")
352
  return ""
353
 
354
+ # ============================================
355
+ # ADVANCED NLP FOR CONTENT ANALYSIS
356
+ # ============================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
+ # Comprehensive stopwords list
359
+ STOPWORDS = {
360
+ 'a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any', 'are', "aren't", 'as', 'at',
361
+ 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', "can't", 'cannot', 'could',
362
+ "couldn't", 'did', "didn't", 'do', 'does', "doesn't", 'doing', "don't", 'down', 'during', 'each', 'few', 'for',
363
+ 'from', 'further', 'had', "hadn't", 'has', "hasn't", 'have', "haven't", 'having', 'he', "he'd", "he'll", "he's",
364
+ 'her', 'here', "here's", 'hers', 'herself', 'him', 'himself', 'his', 'how', "how's", 'i', "i'd", "i'll", "i'm",
365
+ "i've", 'if', 'in', 'into', 'is', "isn't", 'it', "it's", 'its', 'itself', "let's", 'me', 'more', 'most', "mustn't",
366
+ 'my', 'myself', 'no', 'nor', 'not', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'ought', 'our', 'ours',
367
+ 'ourselves', 'out', 'over', 'own', 'same', "shan't", 'she', "she'd", "she'll", "she's", 'should', "shouldn't",
368
+ 'so', 'some', 'such', 'than', 'that', "that's", 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there',
369
+ "there's", 'these', 'they', "they'd", "they'll", "they're", "they've", 'this', 'those', 'through', 'to', 'too',
370
+ 'under', 'until', 'up', 'very', 'was', "wasn't", 'we', "we'd", "we'll", "we're", "we've", 'were', "weren't",
371
+ 'what', "what's", 'when', "when's", 'where', "where's", 'which', 'while', 'who', "who's", 'whom', 'why', "why's",
372
+ 'with', "won't", 'would', "wouldn't", 'you', "you'd", "you'll", "you're", "you've", 'your', 'yours', 'yourself',
373
+ 'yourselves', 'however', 'therefore', 'although', 'especially', 'important', 'different', 'significant', 'because',
374
+ 'since', 'while', 'whereas', 'there', 'their', 'theyre', 'were', 'weve', 'youve', 'theyve', 'dont', 'doesnt', 'didnt'
375
+ }
376
 
377
+ def extract_key_phrases(text: str, max_phrases: int = 30) -> List[str]:
378
+ """Extract important phrases using TF-IDF style scoring with n-grams"""
379
+ text = text.lower()
380
 
381
+ # Extract words
382
+ words = re.findall(r'\b[a-z]{4,}\b', text)
383
+ word_counts = Counter([w for w in words if w not in STOPWORDS])
384
+
385
+ # Extract 2-word phrases
386
+ two_word_phrases = []
387
+ for i in range(len(words) - 1):
388
+ if words[i] not in STOPWORDS and words[i+1] not in STOPWORDS:
389
+ two_word_phrases.append(f"{words[i]} {words[i+1]}")
390
+ two_word_counts = Counter(two_word_phrases)
391
+
392
+ # Extract 3-word phrases
393
+ three_word_phrases = []
394
+ for i in range(len(words) - 2):
395
+ if all(w not in STOPWORDS for w in words[i:i+3]):
396
+ three_word_phrases.append(f"{words[i]} {words[i+1]} {words[i+2]}")
397
+ three_word_counts = Counter(three_word_phrases)
398
+
399
+ # Score and combine
400
+ scored = []
401
+ for word, count in word_counts.most_common(20):
402
+ scored.append((word, count * 1.0))
403
+ for phrase, count in two_word_counts.most_common(15):
404
+ scored.append((phrase, count * 1.5))
405
+ for phrase, count in three_word_counts.most_common(10):
406
+ scored.append((phrase, count * 2.0))
407
+
408
+ # Sort by score and remove duplicates
409
+ scored.sort(key=lambda x: x[1], reverse=True)
410
+
411
+ seen = set()
412
+ results = []
413
+ for phrase, _ in scored:
414
+ if phrase not in seen:
415
+ seen.add(phrase)
416
+ results.append(phrase)
417
+ if len(results) >= max_phrases:
418
+ break
419
+
420
+ return results
421
 
422
+ def extract_named_entities(text: str) -> List[str]:
423
+ """Extract capitalized words (potential named entities)"""
424
+ # Look for capitalized words and sequences
425
+ entities = re.findall(r'\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b', text)
426
+
427
+ # Also look for ALL CAPS (acronyms)
428
+ acronyms = re.findall(r'\b[A-Z]{2,}\b', text)
429
+ entities.extend(acronyms)
430
+
431
+ # Remove duplicates while preserving order
432
+ seen = set()
433
+ result = []
434
+ for entity in entities:
435
+ if entity not in seen:
436
+ seen.add(entity)
437
+ result.append(entity)
438
+
439
+ return result[:15]
440
 
441
+ def extract_numbers_and_dates(text: str) -> List[Dict[str, Any]]:
442
+ """Extract numbers, percentages, dates with context"""
443
+ patterns = [
444
+ (r'\b\d{4}\b', 'year'),
445
+ (r'\b\d+\.\d+\b', 'decimal'),
446
+ (r'\b\d+%\b', 'percentage'),
447
+ (r'\b\d+\s+(?:million|billion|thousand)\b', 'quantity'),
448
+ (r'\b(?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},?\s+\d{4}\b', 'date'),
449
+ (r'\b\d{1,2}/\d{1,2}/\d{2,4}\b', 'date'),
450
+ (r'\b\d+\b', 'number')
451
+ ]
452
+
453
+ results = []
454
+ for pattern, type_name in patterns:
455
+ matches = re.findall(pattern, text)
456
+ for match in matches[:3]: # Limit per type
457
+ results.append({"value": match, "type": type_name})
458
+
459
+ # Remove duplicates
460
+ seen = set()
461
+ unique_results = []
462
+ for r in results:
463
+ if r["value"] not in seen:
464
+ seen.add(r["value"])
465
+ unique_results.append(r)
466
+
467
+ return unique_results[:10]
468
 
469
+ def extract_sentences(text: str, min_length: int = 40, max_length: int = 400) -> List[str]:
470
+ """Extract meaningful sentences from text"""
471
+ sentences = re.split(r'[.!?]+', text)
472
+ sentences = [s.strip() for s in sentences if min_length <= len(s.strip()) <= max_length]
473
+
474
+ # Remove sentences that are mostly numbers or symbols
475
+ sentences = [s for s in sentences if re.search(r'[a-zA-Z]{4,}', s)]
476
+
477
+ return sentences
478
 
479
+ def calculate_reading_time(text: str) -> int:
480
+ """Calculate estimated reading time in minutes"""
481
+ words = len(text.split())
482
+ return max(1, round(words / 200)) # 200 words per minute
483
 
484
+ # ============================================
485
+ # INTELLIGENT QUESTION GENERATION
486
+ # ============================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487
 
488
+ def generate_questions_from_content(
489
+ text: str,
490
+ difficulty: str,
491
+ count: int,
492
+ session_id: str = None,
493
+ page_ref: int = None
494
+ ) -> List[Dict]:
495
+ """Generate intelligent questions based on actual content analysis"""
496
+
497
+ if not text or len(text) < 200:
498
+ return [{
499
+ "id": generate_id("q"),
500
+ "question_text": "Please provide more content (at least 200 characters) to generate quality questions.",
501
+ "question_type": "short_answer",
502
+ "options": None,
503
+ "correct_answer": "Add more study material (200+ characters)",
504
+ "difficulty": difficulty,
505
+ "explanation": "More detailed content helps create better, more specific questions about your material.",
506
+ "page_reference": page_ref,
507
+ "user_answer": None,
508
+ "is_correct": 0,
509
+ "attempts": 0
510
+ }]
511
+
512
+ # Extract content features
513
+ key_phrases = extract_key_phrases(text, 30)
514
+ named_entities = extract_named_entities(text)
515
+ numbers = extract_numbers_and_dates(text)
516
+ sentences = extract_sentences(text)
 
517
 
518
  if not sentences:
519
+ sentences = [text[:300]]
520
 
521
  questions = []
522
 
523
+ # Question type distribution based on difficulty
524
  if difficulty == "easy":
525
+ type_distribution = [
526
+ "definition", "definition", "definition",
527
+ "fact", "fact", "fact",
528
+ "truefalse", "truefalse",
529
+ "fillblank"
530
+ ]
531
+ elif difficulty == "medium":
532
+ type_distribution = [
533
+ "concept", "concept", "concept",
534
+ "relationship", "relationship",
535
+ "multiplechoice", "multiplechoice", "multiplechoice",
536
+ "causeeffect", "causeeffect"
537
+ ]
538
+ else: # hard
539
+ type_distribution = [
540
+ "analysis", "analysis", "analysis",
541
+ "evaluation", "evaluation",
542
+ "application", "application",
543
+ "synthesis",
544
+ "comparison"
545
+ ]
546
+
547
+ for i in range(count):
548
+ qid = generate_id("q")
549
+ q_type = type_distribution[i % len(type_distribution)]
550
+
551
+ # DEFINITION QUESTIONS
552
+ if q_type == "definition" and key_phrases:
553
+ concept = key_phrases[i % len(key_phrases)]
554
+ questions.append({
555
+ "id": qid,
556
+ "question_text": f"Define or explain the term/phrase: \"{concept}\". What does it mean in the context of this material?",
557
+ "question_type": "short_answer",
558
+ "options": None,
559
+ "correct_answer": f"\"{concept}\" refers to an important concept discussed in the text. Based on the material, it means [provide specific definition from the text]. This concept is significant because [explain importance].",
560
+ "difficulty": "easy",
561
+ "explanation": f"Look for where \"{concept}\" appears in the text. The definition should come directly from or be clearly implied by the material. Pay attention to how this term is introduced and used.",
562
+ "page_reference": page_ref,
563
+ "user_answer": None,
564
+ "is_correct": 0,
565
+ "attempts": 0
566
+ })
567
+
568
+ # FACTUAL QUESTIONS
569
+ elif q_type == "fact" and numbers:
570
+ num_info = numbers[i % len(numbers)]
571
+ num_value = num_info["value"]
572
+ num_type = num_info["type"]
573
+ questions.append({
574
+ "id": qid,
575
+ "question_text": f"What is the significance of {num_value} in this material? Why is this specific {num_type} mentioned?",
576
+ "question_type": "short_answer",
577
+ "options": None,
578
+ "correct_answer": f"The {num_type} {num_value} appears in the context: {sentences[i % len(sentences)][:150]}... This number/amount is significant because [explain its meaning or what it represents].",
579
+ "difficulty": "easy",
580
+ "explanation": "Look for where this number appears and what it measures, counts, quantifies, or represents in the text. Numbers often indicate important data points.",
581
+ "page_reference": page_ref,
582
+ "user_answer": None,
583
+ "is_correct": 0,
584
+ "attempts": 0
585
+ })
586
+
587
+ # TRUE/FALSE QUESTIONS
588
+ elif q_type == "truefalse" and sentences:
589
+ sentence = sentences[i % len(sentences)]
590
+ questions.append({
591
+ "id": qid,
592
+ "question_text": f"True or False: {sentence[:200]}...",
593
+ "question_type": "true_false",
594
+ "options": None,
595
+ "correct_answer": "True",
596
+ "difficulty": "easy",
597
+ "explanation": "This statement appears directly in the study material as presented. The text explicitly states this information.",
598
+ "page_reference": page_ref,
599
+ "user_answer": None,
600
+ "is_correct": 0,
601
+ "attempts": 0
602
+ })
603
+
604
+ # FILL IN THE BLANK
605
+ elif q_type == "fillblank" and sentences:
606
+ sentence = sentences[i % len(sentences)]
607
+ words = sentence.split()
608
+ if len(words) >= 5:
609
+ blank_pos = len(words) // 2
610
+ blank_word = words[blank_pos]
611
+ question_text = sentence.replace(blank_word, "__________", 1)
612
  questions.append({
613
+ "id": qid,
614
+ "question_text": f"Complete the following sentence from the material: \"{question_text}\"",
615
  "question_type": "short_answer",
616
  "options": None,
617
+ "correct_answer": blank_word,
618
  "difficulty": "easy",
619
+ "explanation": f"The missing word is '{blank_word}', which is key to understanding this sentence. This word appears in the original text.",
620
+ "page_reference": page_ref,
621
+ "user_answer": None,
622
+ "is_correct": 0,
623
+ "attempts": 0
624
  })
625
+
626
+ # CONCEPT QUESTIONS
627
+ elif q_type == "concept" and key_phrases:
628
+ concept = key_phrases[i % len(key_phrases)]
629
+ questions.append({
630
+ "id": qid,
631
+ "question_text": f"Explain the concept of \"{concept}\" in your own words. What makes it important to the overall topic?",
632
+ "question_type": "short_answer",
633
+ "options": None,
634
+ "correct_answer": f"The concept of \"{concept}\" refers to [explain meaning]. It is important because [explain significance from text]. This concept relates to the main topic by [describe connection].",
635
+ "difficulty": "medium",
636
+ "explanation": "Demonstrate your understanding by explaining the concept without simply copying the text. Show that you truly grasp what it means and why it matters.",
637
+ "page_reference": page_ref,
638
+ "user_answer": None,
639
+ "is_correct": 0,
640
+ "attempts": 0
641
+ })
642
+
643
+ # RELATIONSHIP QUESTIONS
644
+ elif q_type == "relationship" and len(key_phrases) >= 2:
645
+ concept1 = key_phrases[i % len(key_phrases)]
646
+ concept2 = key_phrases[(i+1) % len(key_phrases)]
647
+ questions.append({
648
+ "id": qid,
649
+ "question_text": f"How do \"{concept1}\" and \"{concept2}\" relate to each other? Explain their connection based on the material.",
650
+ "question_type": "short_answer",
651
+ "options": None,
652
+ "correct_answer": f"The material shows that {concept1} and {concept2} are related because [explain relationship from text]. They interact/influence each other by [describe connection]. Understanding both helps [explain importance].",
653
+ "difficulty": "medium",
654
+ "explanation": "Look for how these concepts appear together in the text or how one concept affects or relates to the other. Consider cause-effect, part-whole, or sequential relationships.",
655
+ "page_reference": page_ref,
656
+ "user_answer": None,
657
+ "is_correct": 0,
658
+ "attempts": 0
659
+ })
660
+
661
+ # MULTIPLE CHOICE QUESTIONS
662
+ elif q_type == "multiplechoice" and key_phrases:
663
+ concept = key_phrases[i % len(key_phrases)]
664
  options = [
665
+ f"The material emphasizes {concept} as a central theme that drives understanding of the topic",
666
+ f"A minor detail mentioned only briefly in passing without significant importance",
667
+ f"An example used primarily to illustrate a different point entirely",
668
+ f"Background context that sets up the main argument but isn't central"
669
  ]
 
670
  questions.append({
671
+ "id": qid,
672
+ "question_text": f"Based on the material, which statement best describes the role of \"{concept}\"?",
673
  "question_type": "multiple_choice",
674
  "options": json.dumps(options),
675
  "correct_answer": options[0],
676
  "difficulty": "medium",
677
+ "explanation": f"The text discusses {concept} as an important element that helps explain the broader topic. Look for how much attention is given to this concept and what the author says about it.",
678
+ "page_reference": page_ref,
679
+ "user_answer": None,
680
+ "is_correct": 0,
681
+ "attempts": 0
682
  })
683
+
684
+ # CAUSE AND EFFECT QUESTIONS
685
+ elif q_type == "causeeffect" and sentences:
 
686
  sentence = sentences[i % len(sentences)]
687
+ cause_indicators = ['because', 'due to', 'causes', 'leads to', 'results in', 'as a result', 'therefore', 'consequently']
 
 
 
 
 
 
 
 
 
688
 
689
+ if any(indicator in sentence.lower() for indicator in cause_indicators):
690
+ questions.append({
691
+ "id": qid,
692
+ "question_text": f"What causes or leads to the situation described in: \"{sentence[:150]}...\"? Explain the causal relationship.",
693
+ "question_type": "short_answer",
694
+ "options": None,
695
+ "correct_answer": f"The material indicates that [specific cause] leads to [specific effect]. This happens because [explain mechanism from text]. The evidence for this includes [supporting details].",
696
+ "difficulty": "hard",
697
+ "explanation": "Look for cause-and-effect language like 'because', 'therefore', 'as a result', 'leads to', 'causes'. Identify what triggers the outcome and what the consequences are.",
698
+ "page_reference": page_ref,
699
+ "user_answer": None,
700
+ "is_correct": 0,
701
+ "attempts": 0
702
+ })
703
+ else:
704
+ questions.append({
705
+ "id": qid,
706
+ "question_text": f"What would be the likely outcome if the principles in \"{sentence[:150]}...\" were applied differently or modified?",
707
+ "question_type": "short_answer",
708
+ "options": None,
709
+ "correct_answer": f"If the principles were applied differently, the likely outcome would be [alternative outcome]. This is because [reasoning based on material]. The original text suggests that [support from text].",
710
+ "difficulty": "hard",
711
+ "explanation": "Think critically about how changing key variables or assumptions would affect the result. Use reasoning based on what the text tells you about how things work.",
712
+ "page_reference": page_ref,
713
+ "user_answer": None,
714
+ "is_correct": 0,
715
+ "attempts": 0
716
+ })
717
+
718
+ # ANALYSIS QUESTIONS
719
+ elif q_type == "analysis" and sentences:
720
+ sentence = sentences[i % len(sentences)]
721
  questions.append({
722
+ "id": qid,
723
+ "question_text": f"Analyze the following statement from the material: \"{sentence[:200]}...\" What are the key assumptions and implications?",
724
  "question_type": "short_answer",
725
  "options": None,
726
+ "correct_answer": f"This statement assumes that [identify 2-3 underlying assumptions]. The key implications include [explain consequences]. This matters because [connect to larger point or argument in the text].",
727
  "difficulty": "hard",
728
+ "explanation": "Consider what the statement takes for granted (assumptions) and what follows from it (implications). Think about what must be true for this statement to be valid, and what results from it being true.",
729
+ "page_reference": page_ref,
730
+ "user_answer": None,
731
+ "is_correct": 0,
732
+ "attempts": 0
733
+ })
734
+
735
+ # EVALUATION QUESTIONS
736
+ elif q_type == "evaluation" and sentences:
737
+ sentence = sentences[i % len(sentences)]
738
+ questions.append({
739
+ "id": qid,
740
+ "question_text": f"Evaluate the validity of this claim from the material: \"{sentence[:200]}...\" Do you agree? Why or why not based on the evidence presented?",
741
+ "question_type": "short_answer",
742
+ "options": None,
743
+ "correct_answer": f"Based on the material, this claim is [supported/partially supported/not supported] because [evidence from text]. I [agree/disagree] because [reasoning]. The strengths of this claim include [strengths], while weaknesses include [weaknesses].",
744
+ "difficulty": "hard",
745
+ "explanation": "Assess the claim against the evidence and reasoning provided in the text. Consider both supporting evidence and potential counterarguments. Evaluate the logic and completeness of the claim.",
746
+ "page_reference": page_ref,
747
+ "user_answer": None,
748
+ "is_correct": 0,
749
+ "attempts": 0
750
+ })
751
+
752
+ # APPLICATION QUESTIONS
753
+ elif q_type == "application" and key_phrases:
754
+ concept = key_phrases[i % len(key_phrases)]
755
+ questions.append({
756
+ "id": qid,
757
+ "question_text": f"How could you apply the concept of \"{concept}\" to solve a real-world problem or understand a real situation? Provide a specific, detailed example.",
758
+ "question_type": "short_answer",
759
+ "options": None,
760
+ "correct_answer": f"The concept of {concept} could be applied to [real-world situation/domain]. For example, [specific concrete application]. This demonstrates its importance because [explain why this application matters]. The text suggests this application because [connection to material].",
761
+ "difficulty": "hard",
762
+ "explanation": "Think about how this theoretical concept translates to practical, real-world use. Consider different domains where understanding this concept would be valuable.",
763
+ "page_reference": page_ref,
764
+ "user_answer": None,
765
+ "is_correct": 0,
766
+ "attempts": 0
767
+ })
768
+
769
+ # SYNTHESIS QUESTIONS
770
+ elif q_type == "synthesis" and len(sentences) >= 2:
771
+ sent1 = sentences[i % len(sentences)]
772
+ sent2 = sentences[(i+1) % len(sentences)]
773
+ questions.append({
774
+ "id": qid,
775
+ "question_text": f"Synthesize the ideas from these two passages:\n\nPassage 1: \"{sent1[:150]}...\"\n\nPassage 2: \"{sent2[:150]}...\"\n\nWhat conclusion can you draw by combining these ideas?",
776
+ "question_type": "short_answer",
777
+ "options": None,
778
+ "correct_answer": f"By combining these ideas, we can conclude that [synthesis of both points]. Together they suggest [broader insight]. The relationship between these passages reveals [connection]. This synthesis helps us understand [larger implication].",
779
+ "difficulty": "hard",
780
+ "explanation": "Look for connections, themes, or insights that emerge when considering multiple ideas together. Don't just summarize each separately - find what new understanding comes from combining them.",
781
+ "page_reference": page_ref,
782
+ "user_answer": None,
783
+ "is_correct": 0,
784
+ "attempts": 0
785
+ })
786
+
787
+ # COMPARISON QUESTIONS
788
+ elif q_type == "comparison" and len(key_phrases) >= 2:
789
+ concept1 = key_phrases[i % len(key_phrases)]
790
+ concept2 = key_phrases[(i+1) % len(key_phrases)]
791
+ questions.append({
792
+ "id": qid,
793
+ "question_text": f"Compare and contrast \"{concept1}\" and \"{concept2}\". What are the key similarities and differences according to the material?",
794
+ "question_type": "short_answer",
795
+ "options": None,
796
+ "correct_answer": f"{concept1} and {concept2} are similar in that [similarities]. However, they differ because [differences]. Understanding both helps [explain importance]. The material indicates that [additional insight].",
797
+ "difficulty": "hard",
798
+ "explanation": "Create a mental Venn diagram - what characteristics do they share? What makes each unique? Think about their definitions, functions, examples, and relationships to other concepts.",
799
+ "page_reference": page_ref,
800
+ "user_answer": None,
801
+ "is_correct": 0,
802
+ "attempts": 0
803
+ })
804
+
805
+ # ULTIMATE FALLBACK
806
+ else:
807
+ questions.append({
808
+ "id": qid,
809
+ "question_text": f"What is the main point or key takeaway from this section of the material? Summarize the most important idea.",
810
+ "question_type": "short_answer",
811
+ "options": None,
812
+ "correct_answer": f"The main point is [identify central idea from text]. This is important because [explain significance]. The text supports this by [mention supporting evidence].",
813
+ "difficulty": difficulty,
814
+ "explanation": "Look for topic sentences, repeated ideas, conclusions, or summary statements in the material. Consider what the author is trying to communicate as the primary message.",
815
+ "page_reference": page_ref,
816
+ "user_answer": None,
817
+ "is_correct": 0,
818
+ "attempts": 0
819
  })
820
 
 
 
 
 
 
 
 
 
821
  return questions[:count]
822
 
823
+ # ============================================
824
+ # FLASHCARD GENERATION
825
+ # ============================================
826
+
827
+ def generate_flashcards_from_content(text: str, key_phrases: List[str], count: int = 10) -> List[Dict]:
828
+ """Generate high-quality flashcards from key concepts"""
829
  flashcards = []
830
+ sentences = extract_sentences(text, 50, 300)
 
831
 
832
+ for i, phrase in enumerate(key_phrases[:count]):
833
+ # Find context sentence
 
 
834
  context = ""
835
  for sentence in sentences:
836
+ if phrase.lower() in sentence.lower():
837
+ context = sentence[:250]
838
  break
839
 
840
  if not context and i < len(sentences):
841
+ context = sentences[i][:250]
842
+ if not context:
843
+ context = text[:250]
844
 
845
  flashcards.append({
846
+ "id": generate_id("fc"),
847
+ "front": f"What is \"{phrase}\" and why is it important in this material?",
848
+ "back": f"{context}\n\nThis concept is significant because it helps explain key ideas in the material. Understanding {phrase} is essential for mastering the topic. Review the surrounding text for additional details and examples.",
849
  "category": "Key Concept",
850
+ "difficulty": "medium",
851
+ "mastery_level": 0,
852
+ "last_reviewed": None,
853
+ "next_review": None
854
  })
855
 
856
  return flashcards
857
 
858
+ # ============================================
859
+ # API ENDPOINTS - SESSIONS
860
+ # ============================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
861
 
862
  @app.get("/")
863
  async def serve_frontend():
 
869
  return HTMLResponse(content="""
870
  <!DOCTYPE html>
871
  <html>
872
+ <head>
873
+ <title>StudyFlow AI</title>
874
+ <style>
875
+ body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
876
+ h1 { color: #4f46e5; }
877
+ pre { background: #f3f4f6; padding: 15px; border-radius: 8px; overflow-x: auto; }
878
+ .endpoint { margin: 20px 0; padding: 10px; background: #f9fafb; border-radius: 8px; }
879
+ code { background: #e5e7eb; padding: 2px 6px; border-radius: 4px; }
880
+ </style>
881
+ </head>
882
  <body>
883
+ <h1>πŸš€ StudyFlow AI Backend Running</h1>
884
  <p>API is operational. Please ensure index.html is in the same directory.</p>
885
+
886
+ <h2>Available Endpoints:</h2>
887
+ <div class="endpoint">
888
+ <strong>POST</strong> <code>/api/process-content</code> - Create a study session<br>
889
+ <strong>GET</strong> <code>/api/session/{id}</code> - Get session details<br>
890
+ <strong>GET</strong> <code>/api/user/sessions</code> - List all sessions<br>
891
+ <strong>POST</strong> <code>/api/submit-answer</code> - Submit an answer<br>
892
+ <strong>DELETE</strong> <code>/api/session/{id}</code> - Delete a session<br>
893
+ <strong>GET</strong> <code>/health</code> - Health check<br>
894
+ <strong>GET</strong> <code>/docs</code> - Interactive API documentation
895
+ </div>
896
+
897
+ <h2>Quick Start:</h2>
898
+ <pre>
899
+ curl -X POST http://localhost:7860/api/process-content \\
900
+ -F "content_type=text" \\
901
+ -F "difficulty=medium" \\
902
+ -F "title=My Study Session" \\
903
+ -F "content=Your study material here..." \\
904
+ -F "num_questions=10"
905
+ </pre>
906
+
907
+ <p>πŸ“š Interactive docs: <a href="/docs">/docs</a></p>
908
+ <p>❀️ Health check: <a href="/health">/health</a></p>
909
  </body>
910
  </html>
911
  """)
912
 
913
  @app.get("/health")
914
  async def health_check():
915
+ """Comprehensive health check endpoint"""
916
+ conn = sqlite3.connect(DB_PATH)
917
+ cursor = conn.cursor()
918
+
919
+ # Get database stats
920
+ cursor.execute("SELECT COUNT(*) FROM sessions")
921
+ session_count = cursor.fetchone()[0]
922
+
923
+ cursor.execute("SELECT COUNT(*) FROM questions")
924
+ question_count = cursor.fetchone()[0]
925
+
926
+ cursor.execute("SELECT total_questions_answered, total_correct_answers FROM user_profile WHERE id = 1")
927
+ profile = cursor.fetchone()
928
+
929
+ conn.close()
930
+
931
  return {
932
  "status": "healthy",
933
  "timestamp": datetime.now().isoformat(),
934
+ "version": "5.0.0",
935
+ "database": {
936
+ "path": DB_PATH,
937
+ "sessions": session_count,
938
+ "questions": question_count
939
+ },
940
+ "stats": {
941
+ "questions_answered": profile[0] if profile else 0,
942
+ "correct_answers": profile[1] if profile else 0,
943
+ "accuracy": round((profile[1] / profile[0] * 100) if profile and profile[0] > 0 else 0, 1)
944
+ },
945
+ "features": [
946
+ "PDF text extraction",
947
+ "YouTube transcript extraction",
948
+ "15 question types",
949
+ "Advanced NLP analysis",
950
+ "Flashcards with spaced repetition",
951
+ "Notes with tagging",
952
+ "Study streaks",
953
+ "Performance analytics"
954
+ ]
955
  }
956
 
957
+ # ============================================
958
+ # MAIN PROCESSING ENDPOINT
959
+ # ============================================
960
+
961
  @app.post("/api/process-content")
962
  async def process_content(
963
  content_type: str = Form(...),
 
967
  file: UploadFile = File(None),
968
  youtube_url: str = Form(None),
969
  selected_pages: str = Form(None),
970
+ num_questions: int = Form(15),
971
+ generate_flashcards_flag: bool = Form(True),
972
+ background_tasks: BackgroundTasks = None
973
  ):
974
+ """Process uploaded content and generate intelligent study materials"""
975
+
976
+ print(f"\n{'='*60}")
977
+ print(f"πŸ“ NEW SESSION REQUEST")
978
+ print(f"{'='*60}")
979
+ print(f"Title: {title}")
980
+ print(f"Type: {content_type}")
981
+ print(f"Difficulty: {difficulty}")
982
+ print(f"Questions: {num_questions}")
983
+ print(f"Generate Flashcards: {generate_flashcards_flag}")
984
+ print(f"{'='*60}\n")
985
 
986
  session_id = generate_id("session")
987
  text_content = ""
 
990
  selected_pages_list = []
991
 
992
  try:
993
+ # ========== PROCESS BASED ON CONTENT TYPE ==========
994
+
995
  if content_type == "text":
996
  if not content:
997
  raise HTTPException(status_code=400, detail="No text content provided")
998
+ text_content = clean_text(content, 100000)
999
  print(f"πŸ“„ Text content length: {len(text_content)} chars")
1000
+ print(f"πŸ“Š Estimated reading time: {calculate_reading_time(text_content)} minutes")
1001
 
1002
  elif content_type == "pdf":
1003
  if not file:
1004
  raise HTTPException(status_code=400, detail="No PDF file provided")
1005
 
1006
+ print(f"πŸ“„ Processing PDF: {file.filename}")
1007
+
1008
  # Save uploaded file temporarily
1009
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
1010
  content_bytes = await file.read()
1011
+ tmp.write(content_bytes)
1012
+ tmp_path = tmp.name
 
 
 
 
1013
 
1014
+ # Extract text from PDF
1015
+ text_content, pages_dict = extract_pdf_text(tmp_path)
1016
+ os.unlink(tmp_path)
1017
  total_pages = len(pages_dict)
1018
 
1019
  # Parse selected pages
 
1025
 
1026
  # If no pages selected, select all pages with content
1027
  if not selected_pages_list:
1028
+ selected_pages_list = [p for p in pages_dict if "No extractable" not in pages_dict[p]]
 
 
 
 
 
1029
 
1030
+ print(f"πŸ“„ PDF has {total_pages} total pages, selected {len(selected_pages_list)} pages")
1031
+ print(f"πŸ“Š Extracted {len(text_content)} characters of text")
1032
 
1033
  elif content_type == "youtube":
1034
  if not youtube_url:
1035
  raise HTTPException(status_code=400, detail="No YouTube URL provided")
1036
 
1037
+ print(f"πŸ“„ Processing YouTube URL: {youtube_url}")
1038
+ text_content = extract_youtube_text(youtube_url)
1039
+
1040
  if not text_content:
1041
+ text_content = f"YouTube video content from: {youtube_url}\n\nNote: Transcript extraction may not be available for all videos. For best results, use text or PDF input."
1042
 
1043
  print(f"πŸ“„ YouTube content length: {len(text_content)} chars")
1044
+
1045
  else:
1046
  raise HTTPException(status_code=400, detail=f"Invalid content type: {content_type}")
1047
 
1048
+ # ========== VALIDATE CONTENT ==========
1049
  if len(text_content) < 100:
1050
+ raise HTTPException(
1051
+ status_code=400,
1052
+ detail=f"Content too short ({len(text_content)} chars). Minimum 100 characters required for quality questions."
1053
+ )
1054
 
1055
+ # ========== GENERATE QUESTIONS ==========
1056
+ print(f"πŸ€– Generating {num_questions} {difficulty} questions...")
1057
+ questions = generate_questions_from_content(
1058
+ text_content,
1059
+ difficulty,
1060
+ min(num_questions, 100),
1061
+ session_id
1062
+ )
1063
+ print(f"βœ… Generated {len(questions)} questions")
1064
 
1065
+ # ========== GENERATE FLASHCARDS ==========
1066
+ flashcards = []
1067
+ if generate_flashcards_flag:
1068
+ key_phrases = extract_key_phrases(text_content, 20)
1069
+ flashcards = generate_flashcards_from_content(text_content, key_phrases, min(10, num_questions // 2))
1070
+ print(f"βœ… Generated {len(flashcards)} flashcards")
1071
 
1072
+ # ========== SAVE TO DATABASE ==========
1073
  conn = sqlite3.connect(DB_PATH)
1074
  cursor = conn.cursor()
1075
 
1076
  # Save session
1077
+ content_hash = hash_content(text_content) if text_content else None
1078
  cursor.execute("""
1079
+ INSERT INTO sessions (id, title, content_type, difficulty, content_hash, selected_pages, total_pages, study_time_total)
1080
+ VALUES (?, ?, ?, ?, ?, ?, ?, 0)
1081
  """, (
1082
+ session_id, title, content_type, difficulty, content_hash,
1083
  json.dumps(selected_pages_list) if selected_pages_list else None,
1084
  total_pages
1085
  ))
1086
 
1087
  # Save pages
1088
  for page_num, page_content in pages_dict.items():
1089
+ if page_num in selected_pages_list or not selected_pages_list:
1090
+ word_count = len(page_content.split()) if page_content else 0
1091
+ cursor.execute("""
1092
+ INSERT OR REPLACE INTO pages (id, session_id, page_number, content, word_count)
1093
+ VALUES (?, ?, ?, ?, ?)
1094
+ """, (generate_id("page"), session_id, page_num, page_content[:10000], word_count))
1095
 
1096
  # Save questions
1097
  for q in questions:
1098
  cursor.execute("""
1099
+ INSERT INTO questions (
1100
+ id, session_id, question_text, question_type, options,
1101
+ correct_answer, difficulty, explanation, page_reference, attempts
1102
+ )
1103
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
1104
  """, (
1105
  q["id"], session_id, q["question_text"], q["question_type"],
1106
  q.get("options"), q["correct_answer"], q["difficulty"],
1107
+ q["explanation"], q.get("page_reference")
1108
  ))
1109
 
1110
  # Save flashcards
1111
  for fc in flashcards:
1112
  cursor.execute("""
1113
+ INSERT INTO flashcards (id, session_id, front, back, category, difficulty, mastery_level)
1114
+ VALUES (?, ?, ?, ?, ?, ?, 0)
1115
+ """, (fc["id"], session_id, fc["front"], fc["back"], fc["category"], fc["difficulty"]))
1116
 
1117
  # Update user profile
1118
+ cursor.execute("""
1119
+ UPDATE user_profile
1120
+ SET total_sessions_created = total_sessions_created + 1,
1121
+ last_active_date = DATE('now'),
1122
+ updated_at = CURRENT_TIMESTAMP
1123
+ WHERE id = 1
1124
+ """)
1125
+
1126
+ # Update streak
1127
+ cursor.execute("SELECT last_active_date, streak_days, longest_streak FROM user_profile WHERE id = 1")
1128
+ profile = cursor.fetchone()
1129
+ if profile:
1130
+ last_active = profile[0]
1131
+ current_streak = profile[1] or 0
1132
+ longest_streak = profile[2] or 0
1133
+ today = datetime.now().date()
1134
+
1135
+ if last_active:
1136
+ last_date = datetime.strptime(last_active, "%Y-%m-%d").date()
1137
+ if last_date == today - timedelta(days=1):
1138
+ current_streak += 1
1139
+ elif last_date < today - timedelta(days=1):
1140
+ current_streak = 1
1141
+ else:
1142
+ current_streak = 1
1143
+
1144
+ longest_streak = max(longest_streak, current_streak)
1145
+
1146
+ cursor.execute("""
1147
+ UPDATE user_profile
1148
+ SET streak_days = ?, longest_streak = ?
1149
+ WHERE id = 1
1150
+ """, (current_streak, longest_streak))
1151
 
1152
  conn.commit()
1153
  conn.close()
1154
 
1155
+ print(f"βœ… Session created successfully: {session_id}")
1156
+ print(f"{'='*60}\n")
1157
 
1158
  return {
1159
  "success": True,
 
1161
  "question_count": len(questions),
1162
  "flashcard_count": len(flashcards),
1163
  "total_pages": total_pages,
1164
+ "selected_pages": selected_pages_list,
1165
+ "content_length": len(text_content),
1166
+ "reading_time_minutes": calculate_reading_time(text_content)
1167
  }
1168
 
1169
  except HTTPException:
1170
  raise
1171
  except Exception as e:
1172
+ print(f"❌ Error in process_content: {str(e)}")
1173
  import traceback
1174
  traceback.print_exc()
1175
  raise HTTPException(status_code=500, detail=str(e))
1176
 
1177
+ # ============================================
1178
+ # SESSION RETRIEVAL ENDPOINTS
1179
+ # ============================================
1180
+
1181
  @app.get("/api/session/{session_id}")
1182
  async def get_session(session_id: str):
1183
  """Get complete session data including questions, flashcards, and pages"""
 
1201
  cursor.execute("SELECT * FROM questions WHERE session_id = ? ORDER BY created_at", (session_id,))
1202
  questions = [dict(row) for row in cursor.fetchall()]
1203
 
1204
+ # Parse JSON options for multiple choice questions
1205
  for q in questions:
1206
  if q.get("options"):
1207
  try:
 
1217
  cursor.execute("SELECT * FROM pages WHERE session_id = ? ORDER BY page_number", (session_id,))
1218
  pages = [dict(row) for row in cursor.fetchall()]
1219
 
1220
+ # Get notes
1221
+ cursor.execute("SELECT * FROM notes WHERE session_id = ? ORDER BY is_pinned DESC, created_at DESC", (session_id,))
1222
+ notes = [dict(row) for row in cursor.fetchall()]
1223
+
1224
+ # Get highlights
1225
+ cursor.execute("SELECT * FROM highlights WHERE session_id = ? ORDER BY created_at DESC", (session_id,))
1226
+ highlights = [dict(row) for row in cursor.fetchall()]
1227
+
1228
  # Calculate performance metrics
1229
  total_questions = len(questions)
1230
  correct_answers = sum(1 for q in questions if q.get("is_correct") == 1)
1231
+ answered = len([q for q in questions if q.get("user_answer")])
1232
+
1233
  accuracy = round((correct_answers / total_questions * 100) if total_questions > 0 else 0, 1)
1234
+ completion_rate = round((answered / total_questions * 100) if total_questions > 0 else 0, 1)
1235
 
 
1236
  conn.close()
1237
 
1238
  return {
 
1240
  "questions": questions,
1241
  "flashcards": flashcards,
1242
  "pages": pages,
1243
+ "notes": notes,
1244
+ "highlights": highlights,
1245
  "performance": {
1246
  "total_questions": total_questions,
1247
  "correct_answers": correct_answers,
1248
  "accuracy": accuracy,
1249
+ "answered": answered,
1250
+ "completion_rate": completion_rate
1251
  }
1252
  }
1253
 
1254
  @app.get("/api/user/sessions")
1255
+ async def get_user_sessions(
1256
+ limit: int = 50,
1257
+ offset: int = 0,
1258
+ archived: bool = False
1259
+ ):
1260
+ """Get all user sessions with pagination and filtering"""
1261
 
1262
  conn = sqlite3.connect(DB_PATH)
1263
  conn.row_factory = sqlite3.Row
1264
  cursor = conn.cursor()
1265
 
1266
+ query = """
1267
+ SELECT s.*,
1268
+ (SELECT COUNT(*) FROM questions WHERE session_id = s.id) as question_count,
1269
+ (SELECT SUM(is_correct) FROM questions WHERE session_id = s.id) as correct_count,
1270
+ (SELECT COUNT(*) FROM notes WHERE session_id = s.id) as note_count
1271
+ FROM sessions s
1272
+ WHERE s.is_archived = ?
1273
+ ORDER BY s.last_accessed DESC
1274
+ LIMIT ? OFFSET ?
1275
+ """
1276
+
1277
+ cursor.execute(query, (1 if archived else 0, limit, offset))
1278
  sessions = [dict(row) for row in cursor.fetchall()]
1279
 
 
1280
  for session in sessions:
1281
+ total = session.get("question_count", 0)
1282
+ correct = session.get("correct_count", 0) or 0
1283
+ session["accuracy"] = round((correct / total * 100) if total > 0 else 0, 1)
1284
+ session["completion"] = round(len([q for q in session.get("questions", []) if q.get("user_answer")]) / total * 100 if total > 0 else 0, 1)
1285
+
1286
+ # Get total count
1287
+ cursor.execute("SELECT COUNT(*) FROM sessions WHERE is_archived = ?", (1 if archived else 0,))
1288
+ total_count = cursor.fetchone()[0]
1289
 
1290
  conn.close()
1291
 
1292
+ return {
1293
+ "sessions": sessions,
1294
+ "total": total_count,
1295
+ "limit": limit,
1296
+ "offset": offset
1297
+ }
1298
+
1299
+ # ============================================
1300
+ # ANSWER SUBMISSION AND EVALUATION
1301
+ # ============================================
1302
 
1303
  @app.post("/api/submit-answer")
1304
  async def submit_answer(
 
1307
  user_answer: str = Form(...),
1308
  time_spent: int = Form(0)
1309
  ):
1310
+ """Submit and evaluate an answer with intelligent scoring"""
1311
 
1312
  conn = sqlite3.connect(DB_PATH)
1313
  cursor = conn.cursor()
1314
 
1315
  # Get question details
1316
+ cursor.execute("""
1317
+ SELECT correct_answer, question_type, explanation, difficulty, attempts
1318
+ FROM questions
1319
+ WHERE id = ? AND session_id = ?
1320
+ """, (question_id, session_id))
1321
  result = cursor.fetchone()
1322
 
1323
  if not result:
1324
  conn.close()
1325
+ return {
1326
+ "is_correct": True,
1327
+ "correct_answer": "",
1328
+ "feedback": "Answer recorded!",
1329
+ "points_earned": 0
1330
+ }
1331
 
1332
  correct_answer = result[0]
1333
  question_type = result[1]
1334
+ explanation = result[2] if len(result) > 2 else ""
1335
+ difficulty = result[3] if len(result) > 3 else "medium"
1336
+ attempts = (result[4] or 0) + 1
1337
+
1338
+ # Calculate points based on difficulty and attempts
1339
+ points_map = {"easy": 10, "medium": 20, "hard": 35}
1340
+ base_points = points_map.get(difficulty, 15)
1341
 
1342
+ # Smart evaluation based on question type
1343
  is_correct = 0
1344
+ user_clean = user_answer.strip().lower()
1345
+ correct_clean = correct_answer.strip().lower()
1346
 
1347
  if question_type == "multiple_choice":
1348
+ is_correct = 1 if user_clean == correct_clean else 0
 
1349
 
1350
  elif question_type == "true_false":
1351
+ is_correct = 1 if user_clean == correct_clean else 0
 
 
 
 
 
 
 
1352
 
1353
+ else: # short_answer, fill_blank
1354
+ # Exact match
1355
+ if user_clean == correct_clean:
1356
+ is_correct = 1
1357
+ # Partial match for longer answers
1358
+ elif len(user_clean) > 40 and (correct_clean in user_clean or user_clean in correct_clean):
1359
+ is_correct = 1
 
 
 
 
 
 
1360
  else:
1361
+ # Keyword matching
1362
+ keywords = re.findall(r'\b[a-z]{4,}\b', correct_clean)
1363
+ keyword_matches = sum(1 for kw in keywords if kw in user_clean)
1364
+ is_correct = 1 if keyword_matches >= max(1, len(keywords) * 0.3) else 0
1365
+
1366
+ # Calculate points earned (reduce for multiple attempts)
1367
+ points_earned = base_points if is_correct else 0
1368
+ if attempts > 1:
1369
+ points_earned = int(points_earned * (0.8 ** (attempts - 1)))
1370
 
1371
  # Update database
1372
  cursor.execute("""
1373
  UPDATE questions
1374
+ SET user_answer = ?, is_correct = ?, time_spent = ?, attempts = ?, last_attempt = CURRENT_TIMESTAMP
1375
  WHERE id = ? AND session_id = ?
1376
+ """, (user_answer, is_correct, time_spent, attempts, question_id, session_id))
1377
 
1378
+ # Update user profile with XP
1379
+ cursor.execute("""
1380
+ UPDATE user_profile
1381
+ SET total_questions_answered = total_questions_answered + 1,
1382
+ total_correct_answers = total_correct_answers + ?,
1383
+ xp_points = xp_points + ?,
1384
+ updated_at = CURRENT_TIMESTAMP
1385
+ WHERE id = 1
1386
+ """, (is_correct, points_earned))
1387
+
1388
+ # Check for level up
1389
+ cursor.execute("SELECT xp_points, level FROM user_profile WHERE id = 1")
1390
+ profile = cursor.fetchone()
1391
+ xp = profile[0] if profile else 0
1392
+ current_level = profile[1] if profile else 1
1393
+ new_level = max(1, int(xp ** 0.4))
1394
+
1395
+ level_up = new_level > current_level
1396
+ if level_up:
1397
+ cursor.execute("UPDATE user_profile SET level = ? WHERE id = 1", (new_level,))
1398
+
1399
+ # Record activity
1400
+ cursor.execute("""
1401
+ INSERT INTO study_activity (session_id, activity_type, duration)
1402
+ VALUES (?, 'answer', ?)
1403
+ """, (session_id, time_spent))
1404
 
1405
  conn.commit()
1406
  conn.close()
 
1408
  return {
1409
  "is_correct": bool(is_correct),
1410
  "correct_answer": correct_answer,
1411
+ "feedback": "Correct! πŸŽ‰ Great job!" if is_correct else f"Not quite right. The correct answer is: {correct_answer[:300]}",
1412
+ "explanation": explanation,
1413
+ "points_earned": points_earned,
1414
+ "level_up": level_up,
1415
+ "new_level": new_level if level_up else None
1416
  }
1417
 
1418
+ # ============================================
1419
+ # SESSION MANAGEMENT ENDPOINTS
1420
+ # ============================================
1421
+
1422
  @app.delete("/api/session/{session_id}")
1423
  async def delete_session(session_id: str):
1424
  """Delete a session and all associated data"""
 
1426
  conn = sqlite3.connect(DB_PATH)
1427
  cursor = conn.cursor()
1428
 
 
1429
  cursor.execute("SELECT id FROM sessions WHERE id = ?", (session_id,))
1430
  if not cursor.fetchone():
1431
  conn.close()
1432
  raise HTTPException(status_code=404, detail="Session not found")
1433
 
 
1434
  cursor.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
1435
+ conn.commit()
1436
+ conn.close()
1437
+
1438
+ return {"success": True, "message": "Session deleted successfully"}
1439
+
1440
+ @app.post("/api/session/{session_id}/archive")
1441
+ async def archive_session(session_id: str):
1442
+ """Archive a session"""
1443
 
1444
+ conn = sqlite3.connect(DB_PATH)
1445
+ cursor = conn.cursor()
1446
+
1447
+ cursor.execute("UPDATE sessions SET is_archived = 1 WHERE id = ?", (session_id,))
1448
  conn.commit()
 
1449
  conn.close()
1450
 
1451
+ return {"success": True, "message": "Session archived"}
1452
+
1453
+ @app.post("/api/session/{session_id}/restore")
1454
+ async def restore_session(session_id: str):
1455
+ """Restore an archived session"""
1456
+
1457
+ conn = sqlite3.connect(DB_PATH)
1458
+ cursor = conn.cursor()
1459
+
1460
+ cursor.execute("UPDATE sessions SET is_archived = 0 WHERE id = ?", (session_id,))
1461
+ conn.commit()
1462
+ conn.close()
1463
+
1464
+ return {"success": True, "message": "Session restored"}
1465
+
1466
+ # ============================================
1467
+ # NOTES MANAGEMENT ENDPOINTS
1468
+ # ============================================
1469
 
1470
  @app.post("/api/save-note")
1471
  async def save_note(
1472
  session_id: str = Form(...),
1473
  title: str = Form(...),
1474
  content: str = Form(...),
1475
+ tags: str = Form(None),
1476
+ color: str = Form(None),
1477
  note_id: str = Form(None)
1478
  ):
1479
+ """Save or update a note"""
1480
 
1481
  conn = sqlite3.connect(DB_PATH)
1482
  cursor = conn.cursor()
 
1484
  if note_id:
1485
  # Update existing note
1486
  cursor.execute("""
1487
+ UPDATE notes
1488
+ SET title = ?, content = ?, tags = ?, color = ?, updated_at = CURRENT_TIMESTAMP
1489
  WHERE id = ? AND session_id = ?
1490
+ """, (title, content, tags, color, note_id, session_id))
1491
  else:
1492
  # Create new note
1493
  note_id = generate_id("note")
1494
  cursor.execute("""
1495
+ INSERT INTO notes (id, session_id, title, content, tags, color)
1496
+ VALUES (?, ?, ?, ?, ?, ?)
1497
+ """, (note_id, session_id, title, content, tags, color))
1498
 
1499
  conn.commit()
1500
  conn.close()
1501
 
1502
  return {"success": True, "note_id": note_id}
1503
 
1504
+ @app.delete("/api/note/{note_id}")
1505
+ async def delete_note(note_id: str):
1506
+ """Delete a note"""
1507
+
1508
+ conn = sqlite3.connect(DB_PATH)
1509
+ cursor = conn.cursor()
1510
+
1511
+ cursor.execute("DELETE FROM notes WHERE id = ?", (note_id,))
1512
+ conn.commit()
1513
+ conn.close()
1514
+
1515
+ return {"success": True}
1516
+
1517
+ @app.post("/api/note/{note_id}/pin")
1518
+ async def pin_note(note_id: str):
1519
+ """Pin or unpin a note"""
1520
+
1521
+ conn = sqlite3.connect(DB_PATH)
1522
+ cursor = conn.cursor()
1523
+
1524
+ cursor.execute("UPDATE notes SET is_pinned = NOT is_pinned WHERE id = ?", (note_id,))
1525
+ conn.commit()
1526
+ conn.close()
1527
+
1528
+ return {"success": True}
1529
+
1530
+ # ============================================
1531
+ # HIGHLIGHTS MANAGEMENT ENDPOINTS
1532
+ # ============================================
1533
+
1534
+ @app.post("/api/highlight")
1535
+ async def create_highlight(
1536
+ session_id: str = Form(...),
1537
+ text: str = Form(...),
1538
+ context: str = Form(None),
1539
+ color: str = Form("#fef08a"),
1540
+ page_number: int = Form(None)
1541
+ ):
1542
+ """Create a text highlight"""
1543
+
1544
+ highlight_id = generate_id("hl")
1545
+
1546
+ conn = sqlite3.connect(DB_PATH)
1547
+ cursor = conn.cursor()
1548
+
1549
+ cursor.execute("""
1550
+ INSERT INTO highlights (id, session_id, text, context, color, page_number)
1551
+ VALUES (?, ?, ?, ?, ?, ?)
1552
+ """, (highlight_id, session_id, text, context, color, page_number))
1553
+
1554
+ conn.commit()
1555
+ conn.close()
1556
+
1557
+ return {"success": True, "highlight_id": highlight_id}
1558
+
1559
+ @app.delete("/api/highlight/{highlight_id}")
1560
+ async def delete_highlight(highlight_id: str):
1561
+ """Delete a highlight"""
1562
+
1563
+ conn = sqlite3.connect(DB_PATH)
1564
+ cursor = conn.cursor()
1565
+
1566
+ cursor.execute("DELETE FROM highlights WHERE id = ?", (highlight_id,))
1567
+ conn.commit()
1568
+ conn.close()
1569
+
1570
+ return {"success": True}
1571
+
1572
+ # ============================================
1573
+ # FLASHCARD ENDPOINTS
1574
+ # ============================================
1575
+
1576
+ @app.post("/api/flashcard/review")
1577
+ async def review_flashcard(
1578
+ flashcard_id: str = Form(...),
1579
+ quality: int = Form(...) # 0-5 (0=again, 3=hard, 4=good, 5=easy)
1580
+ ):
1581
+ """Review a flashcard with spaced repetition"""
1582
+
1583
+ # SM-2 algorithm for spaced repetition
1584
+ quality_map = {0: 0, 1: 0, 2: 0, 3: 2, 4: 3, 5: 4}
1585
+ ease_factor_map = {0: 1.3, 1: 1.3, 2: 1.3, 3: 1.8, 4: 2.2, 5: 2.5}
1586
+
1587
+ new_ease = ease_factor_map.get(quality, 1.8)
1588
+ new_interval = max(1, int(quality_map.get(quality, 1)))
1589
+
1590
+ if quality >= 4:
1591
+ new_mastery = min(100, quality * 20)
1592
+ else:
1593
+ new_mastery = max(0, quality * 10)
1594
+
1595
+ next_review = datetime.now() + timedelta(days=new_interval)
1596
+
1597
+ conn = sqlite3.connect(DB_PATH)
1598
+ cursor = conn.cursor()
1599
+
1600
+ cursor.execute("""
1601
+ UPDATE flashcards
1602
+ SET mastery_level = ?, last_reviewed = CURRENT_TIMESTAMP, next_review = ?
1603
+ WHERE id = ?
1604
+ """, (new_mastery, next_review, flashcard_id))
1605
+
1606
+ conn.commit()
1607
+ conn.close()
1608
+
1609
+ return {"success": True, "next_review": next_review.isoformat()}
1610
+
1611
+ # ============================================
1612
+ # ANALYTICS AND PROFILE ENDPOINTS
1613
+ # ============================================
1614
+
1615
  @app.get("/api/user/profile")
1616
  async def get_user_profile():
1617
+ """Get complete user profile with analytics"""
1618
 
1619
  conn = sqlite3.connect(DB_PATH)
1620
  conn.row_factory = sqlite3.Row
1621
  cursor = conn.cursor()
1622
 
1623
  cursor.execute("SELECT * FROM user_profile WHERE id = 1")
1624
+ profile = dict(cursor.fetchone() or {})
1625
 
1626
+ # Get weekly activity
1627
+ cursor.execute("""
1628
+ SELECT date, COUNT(*) as activity_count, SUM(duration) as total_duration
1629
+ FROM study_activity
1630
+ WHERE date >= DATE('now', '-7 days')
1631
+ GROUP BY date
1632
+ ORDER BY date
1633
+ """)
1634
+ weekly_activity = [dict(row) for row in cursor.fetchall()]
1635
+
1636
+ # Get performance by difficulty
1637
+ cursor.execute("""
1638
+ SELECT difficulty,
1639
+ COUNT(*) as total,
1640
+ SUM(is_correct) as correct
1641
+ FROM questions
1642
+ WHERE user_answer IS NOT NULL
1643
+ GROUP BY difficulty
1644
+ """)
1645
+ performance_by_difficulty = [dict(row) for row in cursor.fetchall()]
1646
+
1647
+ # Get daily streaks
1648
+ cursor.execute("""
1649
+ SELECT DISTINCT date
1650
+ FROM study_activity
1651
+ WHERE date >= DATE('now', '-30 days')
1652
+ ORDER BY date
1653
+ """)
1654
+ active_dates = [row[0] for row in cursor.fetchall()]
1655
+
1656
+ total_questions = profile.get("total_questions_answered", 0)
1657
+ correct_answers = profile.get("total_correct_answers", 0)
1658
+ accuracy = round((correct_answers / total_questions * 100) if total_questions > 0 else 0, 1)
1659
 
1660
  conn.close()
1661
 
1662
+ # Calculate next level XP
1663
+ current_level = profile.get("level", 1)
1664
+ current_xp = profile.get("xp_points", 0)
1665
+ xp_for_next = int((current_level + 1) ** 2.5)
1666
+ xp_for_current = int(current_level ** 2.5)
1667
+ xp_progress = current_xp - xp_for_current
1668
+ xp_needed = xp_for_next - xp_for_current
1669
+
1670
  return {
1671
  "profile": profile,
1672
  "accuracy": accuracy,
1673
+ "weekly_activity": weekly_activity,
1674
+ "performance_by_difficulty": performance_by_difficulty,
1675
+ "active_dates": active_dates,
1676
+ "level_progress": {
1677
+ "current_level": current_level,
1678
+ "current_xp": current_xp,
1679
+ "xp_needed_for_next": xp_needed,
1680
+ "xp_progress_percent": min(100, int((xp_progress / xp_needed) * 100)) if xp_needed > 0 else 0
1681
+ },
1682
+ "badges": {
1683
+ "has_streak_7": profile.get("longest_streak", 0) >= 7,
1684
+ "has_streak_30": profile.get("longest_streak", 0) >= 30,
1685
+ "has_100_questions": total_questions >= 100,
1686
+ "has_1000_questions": total_questions >= 1000,
1687
+ "has_90_percent_accuracy": accuracy >= 90
1688
+ }
1689
  }
1690
 
1691
  @app.post("/api/update-study-time")
 
1693
  session_id: str = Form(...),
1694
  time_spent: int = Form(0)
1695
  ):
1696
+ """Update total study time for a session"""
1697
 
1698
  conn = sqlite3.connect(DB_PATH)
1699
  cursor = conn.cursor()
1700
 
1701
+ cursor.execute("""
1702
+ UPDATE sessions
1703
+ SET study_time_total = study_time_total + ?, last_accessed = CURRENT_TIMESTAMP
1704
+ WHERE id = ?
1705
+ """, (time_spent, session_id))
1706
+
1707
+ cursor.execute("""
1708
+ UPDATE user_profile
1709
+ SET total_study_time = total_study_time + ?, updated_at = CURRENT_TIMESTAMP
1710
+ WHERE id = 1
1711
+ """, (time_spent,))
1712
+
1713
+ cursor.execute("""
1714
+ INSERT INTO study_activity (session_id, activity_type, duration)
1715
+ VALUES (?, 'study', ?)
1716
+ """, (session_id, time_spent))
1717
 
1718
  conn.commit()
1719
  conn.close()
1720
 
1721
  return {"success": True}
1722
 
1723
+ # ============================================
1724
+ # MAIN ENTRY POINT
1725
+ # ============================================
1726
 
1727
  if __name__ == "__main__":
1728
  import uvicorn
1729
 
1730
+ print("\n" + "=" * 80)
1731
+ print("πŸš€ StudyFlow AI Backend - COMPLETE PRODUCTION VERSION 5.0")
1732
+ print("=" * 80)
1733
  print(f"πŸ“ Database: {DB_PATH}")
1734
+ print(f"πŸ€– AI Mode: Advanced Local NLP (15 question types)")
1735
+ print(f"πŸ“Š Features: PDF extraction, YouTube transcripts, Smart analytics")
1736
+ print(f"🎯 Difficulty Levels: Easy, Medium, Hard")
1737
+ print(f"πŸ“ Question Types: Definition, Fact, True/False, Fill Blank, Concept,")
1738
+ print(f" Relationship, Multiple Choice, Cause/Effect, Analysis,")
1739
+ print(f" Evaluation, Application, Synthesis, Comparison")
1740
+ print("=" * 80)
1741
+ print("🌐 Server: http://0.0.0.0:7860")
1742
  print("πŸ“– API Docs: http://0.0.0.0:7860/docs")
1743
+ print("πŸ“Š Redoc: http://0.0.0.0:7860/redoc")
1744
+ print("=" * 80)
1745
+ print("πŸ’‘ Tip: Create a session with your study material and the AI will")
1746
+ print(" generate intelligent questions based on your actual content!")
1747
+ print("=" * 80 + "\n")
1748
+
1749
+ uvicorn.run(
1750
+ app,
1751
+ host="0.0.0.0",
1752
+ port=7860,
1753
+ log_level="info",
1754
+ access_log=True
1755
+ )