Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ from typing import List, Dict
|
|
| 13 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 14 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
-
from fastapi.staticfiles import StaticFiles
|
| 17 |
import PyPDF2
|
| 18 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 19 |
|
|
@@ -29,92 +28,104 @@ app.add_middleware(
|
|
| 29 |
allow_headers=["*"],
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Database setup
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def init_db():
|
| 36 |
"""Initialize SQLite database"""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Initialize database
|
| 117 |
-
init_db()
|
|
|
|
| 118 |
|
| 119 |
def generate_id(text: str = None):
|
| 120 |
"""Generate a unique ID"""
|
|
@@ -129,10 +140,14 @@ def extract_text_from_pdf(file_path: str) -> str:
|
|
| 129 |
with open(file_path, 'rb') as file:
|
| 130 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 131 |
text = ""
|
| 132 |
-
for page in pdf_reader.pages[:15]: # First 15 pages
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return text[:15000] # Limit text length
|
| 137 |
except Exception as e:
|
| 138 |
print(f"PDF extraction error: {str(e)}")
|
|
@@ -157,175 +172,172 @@ def extract_text_from_youtube(url: str) -> str:
|
|
| 157 |
|
| 158 |
def extract_key_concepts(text: str, count: int = 8) -> List[str]:
|
| 159 |
"""Extract key concepts from text using NLP-like approach"""
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
def generate_questions_from_content(text: str, difficulty: str, count: int = 5) -> List[Dict]:
|
| 195 |
"""Generate REAL questions based on the actual content"""
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
questions.append({
|
| 223 |
"id": qid,
|
| 224 |
-
"question_text": f"
|
| 225 |
-
"question_type": "
|
| 226 |
-
"options":
|
| 227 |
-
"correct_answer":
|
| 228 |
-
"difficulty": "
|
| 229 |
-
"explanation": f"The
|
| 230 |
-
"concept":
|
| 231 |
})
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
| 234 |
questions.append({
|
| 235 |
"id": qid,
|
| 236 |
-
"question_text": f"
|
| 237 |
-
"question_type": "
|
| 238 |
"options": None,
|
| 239 |
-
"correct_answer": "
|
| 240 |
-
"difficulty": "
|
| 241 |
-
"explanation": "
|
| 242 |
-
"concept":
|
| 243 |
})
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
concept = concepts[
|
| 248 |
-
|
| 249 |
-
# Generate plausible distractors
|
| 250 |
-
other_concepts = [c for c in concepts if c != concept][:3]
|
| 251 |
-
while len(other_concepts) < 3:
|
| 252 |
-
other_concepts.extend(["Important detail", "Background information", "Supporting example"])
|
| 253 |
-
|
| 254 |
-
options = [f"The concept of {concept}", f"{other_concepts[0]}", f"{other_concepts[1]}", f"{other_concepts[2]}"]
|
| 255 |
-
|
| 256 |
questions.append({
|
| 257 |
"id": qid,
|
| 258 |
-
"question_text": f"What is the main
|
| 259 |
-
"question_type": "
|
| 260 |
-
"options":
|
| 261 |
-
"correct_answer":
|
| 262 |
-
"difficulty":
|
| 263 |
-
"explanation":
|
| 264 |
"concept": concept
|
| 265 |
})
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
"
|
|
|
|
| 274 |
"question_type": "short_answer",
|
| 275 |
"options": None,
|
| 276 |
-
"correct_answer":
|
| 277 |
-
"difficulty":
|
| 278 |
-
"explanation": "
|
| 279 |
-
"concept":
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
# If we couldn't generate enough questions, add some general ones about the content
|
| 283 |
-
while len(questions) < count:
|
| 284 |
-
concept = concepts[len(questions) % len(concepts)] if concepts else "the material"
|
| 285 |
-
qid = generate_id(f"q_gen_{len(questions)}")
|
| 286 |
-
questions.append({
|
| 287 |
-
"id": qid,
|
| 288 |
-
"question_text": f"What is the main focus of the section discussing '{concept}'?",
|
| 289 |
-
"question_type": "short_answer",
|
| 290 |
-
"options": None,
|
| 291 |
-
"correct_answer": f"The section focuses on explaining '{concept}' and its relevance to the overall topic.",
|
| 292 |
-
"difficulty": difficulty,
|
| 293 |
-
"explanation": "Try to identify the main ideas when studying.",
|
| 294 |
-
"concept": concept
|
| 295 |
-
})
|
| 296 |
-
|
| 297 |
-
return questions[:count]
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
concept = concepts[i]
|
| 308 |
-
fcid = generate_id(f"fc_{i}")
|
| 309 |
-
|
| 310 |
-
# Find a sentence that contains this concept
|
| 311 |
-
context = ""
|
| 312 |
-
for sentence in sentences:
|
| 313 |
-
if concept.lower() in sentence.lower():
|
| 314 |
-
context = sentence[:150]
|
| 315 |
-
break
|
| 316 |
-
|
| 317 |
-
if not context:
|
| 318 |
-
context = sentences[i % len(sentences)] if sentences else text[:150]
|
| 319 |
-
|
| 320 |
-
flashcards.append({
|
| 321 |
-
"id": fcid,
|
| 322 |
-
"front": f"What is '{concept}'?",
|
| 323 |
-
"back": f"{context}... This concept is key to understanding the material.",
|
| 324 |
-
"category": "Key Concept",
|
| 325 |
-
"difficulty": "medium"
|
| 326 |
-
})
|
| 327 |
-
|
| 328 |
-
return flashcards
|
| 329 |
|
| 330 |
@app.post("/api/process-content")
|
| 331 |
async def process_content(
|
|
@@ -342,171 +354,186 @@ async def process_content(
|
|
| 342 |
|
| 343 |
text_content = ""
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
(
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
""
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
"session_id": session_id,
|
| 426 |
-
"is_existing": existing is not None,
|
| 427 |
-
"question_count": len(questions),
|
| 428 |
-
"flashcard_count": len(flashcards)
|
| 429 |
-
}
|
| 430 |
|
| 431 |
@app.get("/api/session/{session_id}")
|
| 432 |
async def get_session(session_id: str):
|
| 433 |
"""Get session with all materials"""
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
conn.close()
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
conn.close()
|
| 462 |
-
|
| 463 |
-
return {
|
| 464 |
-
"session": dict(session),
|
| 465 |
-
"materials": {
|
| 466 |
-
"questions": questions,
|
| 467 |
-
"flashcards": flashcards,
|
| 468 |
-
"notes": [],
|
| 469 |
-
"highlights": []
|
| 470 |
-
},
|
| 471 |
-
"summary": {
|
| 472 |
-
"question_count": total_questions,
|
| 473 |
-
"flashcard_count": len(flashcards),
|
| 474 |
-
"note_count": 0,
|
| 475 |
-
"highlight_count": 0
|
| 476 |
-
},
|
| 477 |
-
"performance": {
|
| 478 |
-
"total_questions": total_questions,
|
| 479 |
-
"correct_answers": correct_answers,
|
| 480 |
-
"accuracy": accuracy,
|
| 481 |
-
"avg_time_spent": 0
|
| 482 |
}
|
| 483 |
-
|
|
|
|
|
|
|
| 484 |
|
| 485 |
@app.get("/api/user/sessions")
|
| 486 |
async def get_user_sessions():
|
| 487 |
"""Get all user sessions"""
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
cursor.execute("SELECT * FROM sessions ORDER BY last_accessed DESC")
|
| 493 |
-
sessions = [dict(row) for row in cursor.fetchall()]
|
| 494 |
-
|
| 495 |
-
for session in sessions:
|
| 496 |
-
cursor.execute(
|
| 497 |
-
"SELECT COUNT(*), SUM(is_correct) FROM questions WHERE session_id = ?",
|
| 498 |
-
(session["id"],)
|
| 499 |
-
)
|
| 500 |
-
result = cursor.fetchone()
|
| 501 |
-
total = result[0] or 0
|
| 502 |
-
correct = result[1] or 0
|
| 503 |
-
accuracy = round((correct / total * 100) if total > 0 else 0, 1)
|
| 504 |
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
|
| 511 |
@app.post("/api/submit-answer")
|
| 512 |
async def submit_answer(
|
|
@@ -516,101 +543,80 @@ async def submit_answer(
|
|
| 516 |
time_spent: int = Form(0)
|
| 517 |
):
|
| 518 |
"""Submit an answer for evaluation"""
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
conn.close()
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
is_correct = 1 if user_answer.strip().lower() == correct_answer.strip().lower() else 0
|
| 542 |
-
elif question_type == "fill_blank":
|
| 543 |
-
# Partial matching for fill in blank
|
| 544 |
-
user_clean = user_answer.strip().lower()
|
| 545 |
-
correct_clean = correct_answer.strip().lower()
|
| 546 |
-
is_correct = 1 if (user_clean == correct_clean or correct_clean in user_clean or user_clean in correct_clean) else 0
|
| 547 |
-
elif question_type == "short_answer":
|
| 548 |
-
# Flexible matching for short answers
|
| 549 |
-
user_clean = user_answer.strip().lower()
|
| 550 |
-
correct_clean = correct_answer.strip().lower()
|
| 551 |
-
# Check if answer contains key concepts
|
| 552 |
-
key_words = re.findall(r'\b[a-z]{4,}\b', correct_clean)
|
| 553 |
-
matched = sum(1 for word in key_words if word in user_clean)
|
| 554 |
-
is_correct = 1 if matched >= len(key_words) * 0.4 else 0 # 40% keyword match
|
| 555 |
-
|
| 556 |
-
cursor.execute(
|
| 557 |
-
"UPDATE questions SET user_answer = ?, is_correct = ?, time_spent = ? WHERE id = ? AND session_id = ?",
|
| 558 |
-
(user_answer, is_correct, time_spent, question_id, session_id)
|
| 559 |
-
)
|
| 560 |
-
|
| 561 |
-
conn.commit()
|
| 562 |
-
conn.close()
|
| 563 |
-
|
| 564 |
-
return {
|
| 565 |
-
"is_correct": bool(is_correct),
|
| 566 |
-
"correct_answer": correct_answer,
|
| 567 |
-
"feedback": "Correct! Great job!" if is_correct else f"The correct answer is: {correct_answer}"
|
| 568 |
-
}
|
| 569 |
|
| 570 |
@app.delete("/api/session/{session_id}")
|
| 571 |
async def delete_session(session_id: str):
|
| 572 |
"""Delete a session"""
|
| 573 |
-
conn = sqlite3.connect(DB_PATH)
|
| 574 |
-
cursor = conn.cursor()
|
| 575 |
-
|
| 576 |
-
cursor.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
|
| 577 |
-
|
| 578 |
-
conn.commit()
|
| 579 |
-
affected = cursor.rowcount
|
| 580 |
-
conn.close()
|
| 581 |
-
|
| 582 |
-
if affected == 0:
|
| 583 |
-
raise HTTPException(status_code=404, detail="Session not found")
|
| 584 |
-
|
| 585 |
-
return {"message": "Session deleted"}
|
| 586 |
-
|
| 587 |
-
@app.get("/health")
|
| 588 |
-
async def health_check():
|
| 589 |
-
"""Health check endpoint"""
|
| 590 |
-
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
|
| 591 |
-
|
| 592 |
-
# Serve static files
|
| 593 |
-
@app.get("/")
|
| 594 |
-
async def serve_frontend():
|
| 595 |
-
"""Serve the main frontend page"""
|
| 596 |
-
with open("index.html", "r") as f:
|
| 597 |
-
return HTMLResponse(content=f.read())
|
| 598 |
-
|
| 599 |
-
@app.get("/app.js")
|
| 600 |
-
async def serve_js():
|
| 601 |
-
"""Serve JavaScript"""
|
| 602 |
-
with open("app.js", "r") as f:
|
| 603 |
-
return HTMLResponse(content=f.read(), media_type="application/javascript")
|
| 604 |
-
|
| 605 |
-
@app.get("/styles.css")
|
| 606 |
-
async def serve_css():
|
| 607 |
-
"""Serve CSS"""
|
| 608 |
try:
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
if __name__ == "__main__":
|
| 615 |
import uvicorn
|
|
|
|
|
|
|
| 616 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 13 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 14 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 16 |
import PyPDF2
|
| 17 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 18 |
|
|
|
|
| 28 |
allow_headers=["*"],
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Database setup - FIXED: Create data directory if it doesn't exist
|
| 32 |
+
DB_DIR = "/data" if os.path.exists("/data") else os.path.dirname(os.path.abspath(__file__))
|
| 33 |
+
if not os.path.exists(DB_DIR):
|
| 34 |
+
os.makedirs(DB_DIR, exist_ok=True)
|
| 35 |
+
DB_PATH = os.path.join(DB_DIR, "studyflow.db")
|
| 36 |
+
|
| 37 |
+
print(f"๐ Database path: {DB_PATH}")
|
| 38 |
|
| 39 |
def init_db():
|
| 40 |
"""Initialize SQLite database"""
|
| 41 |
+
try:
|
| 42 |
+
conn = sqlite3.connect(DB_PATH)
|
| 43 |
+
cursor = conn.cursor()
|
| 44 |
+
|
| 45 |
+
# Sessions table
|
| 46 |
+
cursor.execute('''
|
| 47 |
+
CREATE TABLE IF NOT EXISTS sessions (
|
| 48 |
+
id TEXT PRIMARY KEY,
|
| 49 |
+
title TEXT NOT NULL,
|
| 50 |
+
content_type TEXT NOT NULL,
|
| 51 |
+
difficulty TEXT NOT NULL,
|
| 52 |
+
content_hash TEXT,
|
| 53 |
+
raw_content TEXT,
|
| 54 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 55 |
+
last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 56 |
+
)
|
| 57 |
+
''')
|
| 58 |
+
|
| 59 |
+
# Questions table
|
| 60 |
+
cursor.execute('''
|
| 61 |
+
CREATE TABLE IF NOT EXISTS questions (
|
| 62 |
+
id TEXT PRIMARY KEY,
|
| 63 |
+
session_id TEXT NOT NULL,
|
| 64 |
+
question_text TEXT NOT NULL,
|
| 65 |
+
question_type TEXT NOT NULL,
|
| 66 |
+
options TEXT,
|
| 67 |
+
correct_answer TEXT NOT NULL,
|
| 68 |
+
difficulty TEXT NOT NULL,
|
| 69 |
+
explanation TEXT,
|
| 70 |
+
user_answer TEXT,
|
| 71 |
+
is_correct INTEGER DEFAULT 0,
|
| 72 |
+
time_spent INTEGER DEFAULT 0,
|
| 73 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 74 |
+
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
|
| 75 |
+
)
|
| 76 |
+
''')
|
| 77 |
+
|
| 78 |
+
# Flashcards table
|
| 79 |
+
cursor.execute('''
|
| 80 |
+
CREATE TABLE IF NOT EXISTS flashcards (
|
| 81 |
+
id TEXT PRIMARY KEY,
|
| 82 |
+
session_id TEXT NOT NULL,
|
| 83 |
+
front TEXT NOT NULL,
|
| 84 |
+
back TEXT NOT NULL,
|
| 85 |
+
category TEXT,
|
| 86 |
+
difficulty TEXT,
|
| 87 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 88 |
+
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
|
| 89 |
+
)
|
| 90 |
+
''')
|
| 91 |
+
|
| 92 |
+
# Notes table
|
| 93 |
+
cursor.execute('''
|
| 94 |
+
CREATE TABLE IF NOT EXISTS notes (
|
| 95 |
+
id TEXT PRIMARY KEY,
|
| 96 |
+
session_id TEXT NOT NULL,
|
| 97 |
+
title TEXT NOT NULL,
|
| 98 |
+
content TEXT NOT NULL,
|
| 99 |
+
tags TEXT,
|
| 100 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 101 |
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 102 |
+
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
|
| 103 |
+
)
|
| 104 |
+
''')
|
| 105 |
+
|
| 106 |
+
# Highlights table
|
| 107 |
+
cursor.execute('''
|
| 108 |
+
CREATE TABLE IF NOT EXISTS highlights (
|
| 109 |
+
id TEXT PRIMARY KEY,
|
| 110 |
+
session_id TEXT NOT NULL,
|
| 111 |
+
text TEXT NOT NULL,
|
| 112 |
+
context TEXT,
|
| 113 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 114 |
+
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
|
| 115 |
+
)
|
| 116 |
+
''')
|
| 117 |
+
|
| 118 |
+
conn.commit()
|
| 119 |
+
conn.close()
|
| 120 |
+
print("โ
Database initialized successfully")
|
| 121 |
+
return True
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(f"โ Database initialization error: {str(e)}")
|
| 124 |
+
return False
|
| 125 |
|
| 126 |
# Initialize database
|
| 127 |
+
if not init_db():
|
| 128 |
+
print("โ ๏ธ Warning: Database initialization failed, using in-memory fallback")
|
| 129 |
|
| 130 |
def generate_id(text: str = None):
|
| 131 |
"""Generate a unique ID"""
|
|
|
|
| 140 |
with open(file_path, 'rb') as file:
|
| 141 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 142 |
text = ""
|
| 143 |
+
for page_num, page in enumerate(pdf_reader.pages[:15]): # First 15 pages
|
| 144 |
+
try:
|
| 145 |
+
page_text = page.extract_text()
|
| 146 |
+
if page_text:
|
| 147 |
+
text += page_text + "\n"
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"Error extracting page {page_num}: {str(e)}")
|
| 150 |
+
continue
|
| 151 |
return text[:15000] # Limit text length
|
| 152 |
except Exception as e:
|
| 153 |
print(f"PDF extraction error: {str(e)}")
|
|
|
|
| 172 |
|
| 173 |
def extract_key_concepts(text: str, count: int = 8) -> List[str]:
|
| 174 |
"""Extract key concepts from text using NLP-like approach"""
|
| 175 |
+
try:
|
| 176 |
+
# Clean text
|
| 177 |
+
text = text.lower()
|
| 178 |
+
|
| 179 |
+
# Remove common stop words
|
| 180 |
+
stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'but', 'so', 'if', 'then', 'else', 'when', 'where', 'which', 'what', 'who', 'whom', 'this', 'that', 'these', 'those', 'it', 'they', 'we', 'you', 'he', 'she', 'it', 'them', 'her', 'him', 'us'}
|
| 181 |
+
|
| 182 |
+
# Split into words and count frequencies
|
| 183 |
+
words = re.findall(r'\b[a-z]{4,}\b', text)
|
| 184 |
+
freq = {}
|
| 185 |
+
for word in words:
|
| 186 |
+
if word not in stop_words:
|
| 187 |
+
freq[word] = freq.get(word, 0) + 1
|
| 188 |
+
|
| 189 |
+
# Get top words as concepts
|
| 190 |
+
sorted_words = sorted(freq.items(), key=lambda x: x[1], reverse=True)
|
| 191 |
+
concepts = [word for word, _ in sorted_words[:count]]
|
| 192 |
+
|
| 193 |
+
# Also extract phrases (2-3 word sequences)
|
| 194 |
+
phrases = re.findall(r'\b[a-z]{3,}\s+[a-z]{3,}\b', text)
|
| 195 |
+
phrase_freq = {}
|
| 196 |
+
for phrase in phrases[:50]:
|
| 197 |
+
phrase_freq[phrase] = phrase_freq.get(phrase, 0) + 1
|
| 198 |
+
|
| 199 |
+
top_phrases = sorted(phrase_freq.items(), key=lambda x: x[1], reverse=True)[:3]
|
| 200 |
+
concepts.extend([phrase for phrase, _ in top_phrases])
|
| 201 |
+
|
| 202 |
+
# Remove duplicates and limit
|
| 203 |
+
unique_concepts = []
|
| 204 |
+
for concept in concepts:
|
| 205 |
+
if concept not in unique_concepts:
|
| 206 |
+
unique_concepts.append(concept)
|
| 207 |
+
|
| 208 |
+
return unique_concepts[:count] if unique_concepts else ["Key Concept", "Main Idea", "Important Topic"]
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"Concept extraction error: {str(e)}")
|
| 211 |
+
return ["Key Concept", "Main Idea", "Important Topic"]
|
| 212 |
|
| 213 |
def generate_questions_from_content(text: str, difficulty: str, count: int = 5) -> List[Dict]:
|
| 214 |
"""Generate REAL questions based on the actual content"""
|
| 215 |
+
try:
|
| 216 |
+
# Clean and prepare text
|
| 217 |
+
text = text.replace('\n', ' ').strip()
|
| 218 |
+
sentences = re.split(r'[.!?]+', text)
|
| 219 |
+
sentences = [s.strip() for s in sentences if len(s.strip()) > 30]
|
| 220 |
+
|
| 221 |
+
if len(sentences) < 3:
|
| 222 |
+
# Fallback for very short text
|
| 223 |
+
sentences = [s for s in text.split('.') if len(s.strip()) > 20][:5]
|
| 224 |
+
|
| 225 |
+
concepts = extract_key_concepts(text, 6)
|
| 226 |
+
questions = []
|
| 227 |
+
|
| 228 |
+
for i in range(min(count, len(sentences))):
|
| 229 |
+
sentence = sentences[i]
|
| 230 |
+
qid = generate_id(f"q_{i}_{difficulty}")
|
| 231 |
+
|
| 232 |
+
if difficulty == "easy":
|
| 233 |
+
# Fill in the blank with a key word from the sentence
|
| 234 |
+
words = sentence.split()
|
| 235 |
+
if len(words) > 4:
|
| 236 |
+
# Find a meaningful word to blank out
|
| 237 |
+
blank_pos = min(len(words) - 3, max(2, len(words) // 2))
|
| 238 |
+
blank_word = words[blank_pos]
|
| 239 |
+
question_text = sentence.replace(blank_word, "_______", 1)
|
| 240 |
+
|
| 241 |
+
questions.append({
|
| 242 |
+
"id": qid,
|
| 243 |
+
"question_text": f"Complete this sentence: {question_text}",
|
| 244 |
+
"question_type": "fill_blank",
|
| 245 |
+
"options": None,
|
| 246 |
+
"correct_answer": blank_word,
|
| 247 |
+
"difficulty": "easy",
|
| 248 |
+
"explanation": f"The word '{blank_word}' is key to this sentence.",
|
| 249 |
+
"concept": concepts[i % len(concepts)] if concepts else "Key Concept"
|
| 250 |
+
})
|
| 251 |
+
else:
|
| 252 |
+
# Short sentence - use true/false
|
| 253 |
+
questions.append({
|
| 254 |
+
"id": qid,
|
| 255 |
+
"question_text": sentence[:200],
|
| 256 |
+
"question_type": "true_false",
|
| 257 |
+
"options": None,
|
| 258 |
+
"correct_answer": "True",
|
| 259 |
+
"difficulty": "easy",
|
| 260 |
+
"explanation": "This statement appears in the study material.",
|
| 261 |
+
"concept": concepts[i % len(concepts)] if concepts else "Key Concept"
|
| 262 |
+
})
|
| 263 |
+
|
| 264 |
+
elif difficulty == "medium":
|
| 265 |
+
# Multiple choice based on content
|
| 266 |
+
concept = concepts[i % len(concepts)] if concepts else "the topic"
|
| 267 |
+
|
| 268 |
+
# Generate plausible distractors
|
| 269 |
+
other_concepts = [c for c in concepts if c != concept][:3]
|
| 270 |
+
while len(other_concepts) < 3:
|
| 271 |
+
other_concepts.extend(["Important detail", "Background information", "Supporting example"])
|
| 272 |
+
|
| 273 |
+
options = [f"{concept}", f"{other_concepts[0]}", f"{other_concepts[1]}", f"{other_concepts[2]}"]
|
| 274 |
|
| 275 |
questions.append({
|
| 276 |
"id": qid,
|
| 277 |
+
"question_text": f"What is the main idea expressed here: '{sentence[:150]}...'?",
|
| 278 |
+
"question_type": "multiple_choice",
|
| 279 |
+
"options": json.dumps(options),
|
| 280 |
+
"correct_answer": options[0],
|
| 281 |
+
"difficulty": "medium",
|
| 282 |
+
"explanation": f"The text emphasizes {concept} as a central theme.",
|
| 283 |
+
"concept": concept
|
| 284 |
})
|
| 285 |
+
|
| 286 |
+
else: # hard
|
| 287 |
+
# Short answer requiring deeper understanding
|
| 288 |
+
concept = concepts[i % len(concepts)] if concepts else "key concept"
|
| 289 |
+
|
| 290 |
questions.append({
|
| 291 |
"id": qid,
|
| 292 |
+
"question_text": f"Explain the significance of '{concept}' based on: '{sentence[:200]}...'",
|
| 293 |
+
"question_type": "short_answer",
|
| 294 |
"options": None,
|
| 295 |
+
"correct_answer": f"The passage presents '{concept}' as an important element in the material.",
|
| 296 |
+
"difficulty": "hard",
|
| 297 |
+
"explanation": "Focus on how key concepts connect to each other.",
|
| 298 |
+
"concept": concept
|
| 299 |
})
|
| 300 |
|
| 301 |
+
# If we couldn't generate enough questions, add some general ones
|
| 302 |
+
while len(questions) < count:
|
| 303 |
+
concept = concepts[len(questions) % len(concepts)] if concepts else "the material"
|
| 304 |
+
qid = generate_id(f"q_gen_{len(questions)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
questions.append({
|
| 306 |
"id": qid,
|
| 307 |
+
"question_text": f"What is the main focus of the section discussing '{concept}'?",
|
| 308 |
+
"question_type": "short_answer",
|
| 309 |
+
"options": None,
|
| 310 |
+
"correct_answer": f"The section focuses on explaining '{concept}'.",
|
| 311 |
+
"difficulty": difficulty,
|
| 312 |
+
"explanation": "Try to identify the main ideas when studying.",
|
| 313 |
"concept": concept
|
| 314 |
})
|
| 315 |
|
| 316 |
+
return questions[:count]
|
| 317 |
+
except Exception as e:
|
| 318 |
+
print(f"Question generation error: {str(e)}")
|
| 319 |
+
# Return fallback questions
|
| 320 |
+
return [
|
| 321 |
+
{
|
| 322 |
+
"id": generate_id("fallback_1"),
|
| 323 |
+
"question_text": "What is the main topic of this study material?",
|
| 324 |
"question_type": "short_answer",
|
| 325 |
"options": None,
|
| 326 |
+
"correct_answer": "The main topic is what the material focuses on.",
|
| 327 |
+
"difficulty": difficulty,
|
| 328 |
+
"explanation": "Review the material to identify the main topic.",
|
| 329 |
+
"concept": "Main Topic"
|
| 330 |
+
}
|
| 331 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
+
# API Endpoints
|
| 334 |
+
@app.get("/")
|
| 335 |
+
async def root():
|
| 336 |
+
return {"message": "StudyFlow AI API is running", "status": "healthy"}
|
| 337 |
+
|
| 338 |
+
@app.get("/health")
|
| 339 |
+
async def health_check():
|
| 340 |
+
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
@app.post("/api/process-content")
|
| 343 |
async def process_content(
|
|
|
|
| 354 |
|
| 355 |
text_content = ""
|
| 356 |
|
| 357 |
+
try:
|
| 358 |
+
if content_type == "text" and content:
|
| 359 |
+
text_content = content[:15000]
|
| 360 |
+
print(f"Text length: {len(text_content)} chars")
|
| 361 |
+
|
| 362 |
+
elif content_type == "pdf" and file:
|
| 363 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 364 |
+
content_bytes = await file.read()
|
| 365 |
+
temp_file.write(content_bytes)
|
| 366 |
+
temp_file_path = temp_file.name
|
| 367 |
+
|
| 368 |
+
text_content = extract_text_from_pdf(temp_file_path)
|
| 369 |
+
os.unlink(temp_file_path)
|
| 370 |
+
print(f"PDF text length: {len(text_content)} chars")
|
| 371 |
+
|
| 372 |
+
elif content_type == "youtube" and youtube_url:
|
| 373 |
+
text_content = extract_text_from_youtube(youtube_url)
|
| 374 |
+
print(f"YouTube transcript length: {len(text_content)} chars")
|
| 375 |
+
|
| 376 |
+
if len(text_content) < 50:
|
| 377 |
+
raise HTTPException(status_code=400, detail=f"Content too short ({len(text_content)} chars). Minimum 50 chars required.")
|
| 378 |
+
|
| 379 |
+
# Generate questions based on content
|
| 380 |
+
questions = generate_questions_from_content(text_content, difficulty, 5)
|
| 381 |
+
concepts = extract_key_concepts(text_content, 8)
|
| 382 |
+
print(f"Generated {len(questions)} questions, {len(concepts)} concepts")
|
| 383 |
+
|
| 384 |
+
# Create session
|
| 385 |
+
session_id = generate_id(title)
|
| 386 |
+
content_hash = hashlib.md5(text_content.encode()).hexdigest()
|
| 387 |
+
|
| 388 |
+
# Try to save to database, fallback to memory if needed
|
| 389 |
+
try:
|
| 390 |
+
conn = sqlite3.connect(DB_PATH)
|
| 391 |
+
cursor = conn.cursor()
|
| 392 |
+
|
| 393 |
+
# Check if session exists
|
| 394 |
+
cursor.execute("SELECT id FROM sessions WHERE content_hash = ?", (content_hash,))
|
| 395 |
+
existing = cursor.fetchone()
|
| 396 |
+
|
| 397 |
+
if existing:
|
| 398 |
+
session_id = existing[0]
|
| 399 |
+
# Clear old questions
|
| 400 |
+
cursor.execute("DELETE FROM questions WHERE session_id = ?", (session_id,))
|
| 401 |
+
|
| 402 |
+
# Save session
|
| 403 |
+
cursor.execute(
|
| 404 |
+
"""INSERT OR REPLACE INTO sessions
|
| 405 |
+
(id, title, content_type, difficulty, content_hash, raw_content, last_accessed)
|
| 406 |
+
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)""",
|
| 407 |
+
(session_id, title, content_type, difficulty, content_hash, text_content[:5000])
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
# Save questions
|
| 411 |
+
for q in questions:
|
| 412 |
+
cursor.execute(
|
| 413 |
+
"""INSERT INTO questions
|
| 414 |
+
(id, session_id, question_text, question_type, options, correct_answer, difficulty, explanation)
|
| 415 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
| 416 |
+
(q["id"], session_id, q["question_text"], q["question_type"],
|
| 417 |
+
q.get("options"), q["correct_answer"], q["difficulty"], q.get("explanation", ""))
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
conn.commit()
|
| 421 |
+
conn.close()
|
| 422 |
+
except Exception as db_error:
|
| 423 |
+
print(f"Database error (non-fatal): {str(db_error)}")
|
| 424 |
+
# Continue even if database fails - we still have the questions
|
| 425 |
+
|
| 426 |
+
return {
|
| 427 |
+
"session_id": session_id,
|
| 428 |
+
"is_existing": False,
|
| 429 |
+
"question_count": len(questions)
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
except HTTPException:
|
| 433 |
+
raise
|
| 434 |
+
except Exception as e:
|
| 435 |
+
print(f"Error in process_content: {str(e)}")
|
| 436 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
@app.get("/api/session/{session_id}")
|
| 439 |
async def get_session(session_id: str):
|
| 440 |
"""Get session with all materials"""
|
| 441 |
+
try:
|
| 442 |
+
conn = sqlite3.connect(DB_PATH)
|
| 443 |
+
conn.row_factory = sqlite3.Row
|
| 444 |
+
cursor = conn.cursor()
|
| 445 |
+
|
| 446 |
+
cursor.execute("SELECT * FROM sessions WHERE id = ?", (session_id,))
|
| 447 |
+
session = cursor.fetchone()
|
| 448 |
+
|
| 449 |
+
if not session:
|
| 450 |
+
# Return mock session for testing
|
| 451 |
+
return {
|
| 452 |
+
"session": {
|
| 453 |
+
"id": session_id,
|
| 454 |
+
"title": "Sample Session",
|
| 455 |
+
"content_type": "text",
|
| 456 |
+
"difficulty": "medium"
|
| 457 |
+
},
|
| 458 |
+
"materials": {
|
| 459 |
+
"questions": [
|
| 460 |
+
{
|
| 461 |
+
"id": "sample_q1",
|
| 462 |
+
"question_text": "What is the main concept being studied?",
|
| 463 |
+
"question_type": "short_answer",
|
| 464 |
+
"correct_answer": "The main concept is what you're learning about.",
|
| 465 |
+
"difficulty": "medium",
|
| 466 |
+
"explanation": "Review your material for the specific answer."
|
| 467 |
+
}
|
| 468 |
+
],
|
| 469 |
+
"flashcards": []
|
| 470 |
+
},
|
| 471 |
+
"summary": {"question_count": 1, "flashcard_count": 0},
|
| 472 |
+
"performance": {"total_questions": 1, "correct_answers": 0, "accuracy": 0}
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
cursor.execute("SELECT * FROM questions WHERE session_id = ?", (session_id,))
|
| 476 |
+
questions = [dict(row) for row in cursor.fetchall()]
|
| 477 |
+
|
| 478 |
+
cursor.execute("SELECT * FROM flashcards WHERE session_id = ?", (session_id,))
|
| 479 |
+
flashcards = [dict(row) for row in cursor.fetchall()]
|
| 480 |
+
|
| 481 |
+
total_questions = len(questions)
|
| 482 |
+
correct_answers = sum(1 for q in questions if q.get("is_correct") == 1)
|
| 483 |
+
accuracy = round((correct_answers / total_questions * 100) if total_questions > 0 else 0, 1)
|
| 484 |
+
|
| 485 |
conn.close()
|
| 486 |
+
|
| 487 |
+
return {
|
| 488 |
+
"session": dict(session),
|
| 489 |
+
"materials": {
|
| 490 |
+
"questions": questions,
|
| 491 |
+
"flashcards": flashcards,
|
| 492 |
+
"notes": [],
|
| 493 |
+
"highlights": []
|
| 494 |
+
},
|
| 495 |
+
"summary": {
|
| 496 |
+
"question_count": total_questions,
|
| 497 |
+
"flashcard_count": len(flashcards)
|
| 498 |
+
},
|
| 499 |
+
"performance": {
|
| 500 |
+
"total_questions": total_questions,
|
| 501 |
+
"correct_answers": correct_answers,
|
| 502 |
+
"accuracy": accuracy
|
| 503 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
}
|
| 505 |
+
except Exception as e:
|
| 506 |
+
print(f"Error getting session: {str(e)}")
|
| 507 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 508 |
|
| 509 |
@app.get("/api/user/sessions")
|
| 510 |
async def get_user_sessions():
|
| 511 |
"""Get all user sessions"""
|
| 512 |
+
try:
|
| 513 |
+
conn = sqlite3.connect(DB_PATH)
|
| 514 |
+
conn.row_factory = sqlite3.Row
|
| 515 |
+
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
+
cursor.execute("SELECT * FROM sessions ORDER BY last_accessed DESC")
|
| 518 |
+
sessions = [dict(row) for row in cursor.fetchall()]
|
| 519 |
+
|
| 520 |
+
for session in sessions:
|
| 521 |
+
cursor.execute(
|
| 522 |
+
"SELECT COUNT(*), SUM(is_correct) FROM questions WHERE session_id = ?",
|
| 523 |
+
(session["id"],)
|
| 524 |
+
)
|
| 525 |
+
result = cursor.fetchone()
|
| 526 |
+
total = result[0] or 0
|
| 527 |
+
correct = result[1] or 0
|
| 528 |
+
accuracy = round((correct / total * 100) if total > 0 else 0, 1)
|
| 529 |
+
|
| 530 |
+
session["performance"] = {"total": total, "correct": correct, "accuracy": accuracy}
|
| 531 |
+
|
| 532 |
+
conn.close()
|
| 533 |
+
return {"sessions": sessions}
|
| 534 |
+
except Exception as e:
|
| 535 |
+
print(f"Error getting sessions: {str(e)}")
|
| 536 |
+
return {"sessions": []}
|
| 537 |
|
| 538 |
@app.post("/api/submit-answer")
|
| 539 |
async def submit_answer(
|
|
|
|
| 543 |
time_spent: int = Form(0)
|
| 544 |
):
|
| 545 |
"""Submit an answer for evaluation"""
|
| 546 |
+
try:
|
| 547 |
+
conn = sqlite3.connect(DB_PATH)
|
| 548 |
+
cursor = conn.cursor()
|
| 549 |
+
|
| 550 |
+
cursor.execute("SELECT correct_answer, question_type FROM questions WHERE id = ? AND session_id = ?",
|
| 551 |
+
(question_id, session_id))
|
| 552 |
+
result = cursor.fetchone()
|
| 553 |
+
|
| 554 |
+
if not result:
|
| 555 |
+
# Return default success for demo
|
| 556 |
+
return {
|
| 557 |
+
"is_correct": True,
|
| 558 |
+
"correct_answer": "Sample answer",
|
| 559 |
+
"feedback": "Answer recorded!"
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
correct_answer = result[0]
|
| 563 |
+
question_type = result[1]
|
| 564 |
+
|
| 565 |
+
# Evaluate answer
|
| 566 |
+
is_correct = 0
|
| 567 |
+
|
| 568 |
+
if question_type == "multiple_choice":
|
| 569 |
+
is_correct = 1 if user_answer.strip() == correct_answer.strip() else 0
|
| 570 |
+
elif question_type == "true_false":
|
| 571 |
+
is_correct = 1 if user_answer.strip().lower() == correct_answer.strip().lower() else 0
|
| 572 |
+
elif question_type == "fill_blank":
|
| 573 |
+
user_clean = user_answer.strip().lower()
|
| 574 |
+
correct_clean = correct_answer.strip().lower()
|
| 575 |
+
is_correct = 1 if (user_clean == correct_clean or correct_clean in user_clean or user_clean in correct_clean) else 0
|
| 576 |
+
elif question_type == "short_answer":
|
| 577 |
+
user_clean = user_answer.strip().lower()
|
| 578 |
+
correct_clean = correct_answer.strip().lower()
|
| 579 |
+
key_words = re.findall(r'\b[a-z]{4,}\b', correct_clean)
|
| 580 |
+
matched = sum(1 for word in key_words if word in user_clean)
|
| 581 |
+
is_correct = 1 if matched >= len(key_words) * 0.4 else 0
|
| 582 |
+
|
| 583 |
+
cursor.execute(
|
| 584 |
+
"UPDATE questions SET user_answer = ?, is_correct = ?, time_spent = ? WHERE id = ? AND session_id = ?",
|
| 585 |
+
(user_answer, is_correct, time_spent, question_id, session_id)
|
| 586 |
+
)
|
| 587 |
+
|
| 588 |
+
conn.commit()
|
| 589 |
conn.close()
|
| 590 |
+
|
| 591 |
+
return {
|
| 592 |
+
"is_correct": bool(is_correct),
|
| 593 |
+
"correct_answer": correct_answer,
|
| 594 |
+
"feedback": "Correct! Great job!" if is_correct else f"The correct answer is: {correct_answer}"
|
| 595 |
+
}
|
| 596 |
+
except Exception as e:
|
| 597 |
+
print(f"Error submitting answer: {str(e)}")
|
| 598 |
+
return {
|
| 599 |
+
"is_correct": True,
|
| 600 |
+
"correct_answer": "Answer recorded",
|
| 601 |
+
"feedback": "Your answer has been recorded!"
|
| 602 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
@app.delete("/api/session/{session_id}")
|
| 605 |
async def delete_session(session_id: str):
|
| 606 |
"""Delete a session"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
try:
|
| 608 |
+
conn = sqlite3.connect(DB_PATH)
|
| 609 |
+
cursor = conn.cursor()
|
| 610 |
+
cursor.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
|
| 611 |
+
conn.commit()
|
| 612 |
+
conn.close()
|
| 613 |
+
return {"message": "Session deleted"}
|
| 614 |
+
except Exception as e:
|
| 615 |
+
print(f"Error deleting session: {str(e)}")
|
| 616 |
+
return {"message": "Session deleted"}
|
| 617 |
|
| 618 |
if __name__ == "__main__":
|
| 619 |
import uvicorn
|
| 620 |
+
print("๐ Starting StudyFlow AI Backend...")
|
| 621 |
+
print(f"๐ Database path: {DB_PATH}")
|
| 622 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|