SurajJha21 commited on
Commit
5443d3f
·
verified ·
1 Parent(s): 010a747

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -43
app.py CHANGED
@@ -29,6 +29,10 @@ if "is_initialized" not in st.session_state:
29
  st.session_state.is_initialized = False
30
  if "index_dimensions" not in st.session_state:
31
  st.session_state.index_dimensions = 1024 # Set this based on your Pinecone index
 
 
 
 
32
 
33
  # Functions for PDF processing
34
  def extract_text_from_pdf(pdf_file):
@@ -277,6 +281,14 @@ def generate_assignment(pdf_content, assignment_type="short_answer", num_questio
277
  st.error(f"Error parsing assignment response: {str(e)}")
278
  return []
279
 
 
 
 
 
 
 
 
 
280
  # Streamlit UI
281
  def main():
282
  st.title("📚 PDF Learning Assistant")
@@ -355,6 +367,8 @@ GOOGLE_API_KEY=your_google_api_key
355
  with st.spinner("Generating quiz..."):
356
  quiz_data = generate_quiz(st.session_state.current_pdf_content, num_questions=quiz_questions)
357
  st.session_state.quiz_data = quiz_data
 
 
358
 
359
  # Assignment generation
360
  assignment_type = st.selectbox(
@@ -426,38 +440,41 @@ GOOGLE_API_KEY=your_google_api_key
426
  if "quiz_data" in st.session_state and st.session_state.quiz_data:
427
  quiz_data = st.session_state.quiz_data
428
 
429
- if "quiz_answers" not in st.session_state:
430
- st.session_state.quiz_answers = {}
431
- st.session_state.quiz_submitted = False
432
-
433
  if not st.session_state.quiz_submitted:
434
- for i, question in enumerate(quiz_data):
435
- st.subheader(f"Question {i+1}")
436
- st.write(question["question"])
437
-
438
- options = question["options"]
439
- option_labels = ["A", "B", "C", "D"]
440
-
441
- # Create radio buttons for options
442
- answer = st.radio(
443
- "Select your answer:",
444
- options=option_labels[:len(options)],
445
- key=f"q{i}",
446
- index=None
447
- )
448
-
449
- # Display options
450
- for j, option in enumerate(options):
451
- st.write(f"{option_labels[j]}: {option}")
 
 
 
 
 
 
 
 
452
 
453
- st.session_state.quiz_answers[i] = answer
454
- st.divider()
455
-
456
- if st.button("Submit Quiz"):
457
- st.session_state.quiz_submitted = True
458
- st.experimental_rerun()
459
  else:
460
- # Show results
461
  correct_count = 0
462
 
463
  for i, question in enumerate(quiz_data):
@@ -488,38 +505,48 @@ GOOGLE_API_KEY=your_google_api_key
488
  st.subheader(f"Your Score: {correct_count}/{len(quiz_data)}")
489
 
490
  if st.button("Retake Quiz"):
491
- st.session_state.quiz_submitted = False
492
- st.session_state.quiz_answers = {}
493
- st.experimental_rerun()
494
  else:
495
  st.info("Generate a quiz from the sidebar to see it here")
496
 
497
  # Tab 3: Assignment
 
498
  with tab3:
499
  st.header("Assignment")
500
 
501
  if "assignment_data" in st.session_state and st.session_state.assignment_data:
502
  assignment_data = st.session_state.assignment_data
503
 
504
- for i, question in enumerate(assignment_data):
505
- with st.expander(f"Question {i+1}", expanded=True):
 
 
506
  st.write(question["question"])
507
 
 
508
  if "hints" in question and question["hints"]:
509
- st.subheader("Hints")
510
- for hint in question["hints"]:
511
- st.write(f"- {hint}")
 
512
 
513
- # Input area for answers
514
  st.text_area("Your Answer:", key=f"assignment_q{i}", height=150)
515
-
516
- # Reveal key points button
517
- if st.button("Show Key Points", key=f"key_points_btn_{i}"):
518
- st.subheader("Key Points to Include")
 
 
 
 
 
 
519
  for point in question["key_points"]:
520
  st.write(f"- {point}")
521
  else:
522
  st.info("Generate an assignment from the sidebar to see it here")
 
523
 
524
  if __name__ == "__main__":
525
- main()
 
29
  st.session_state.is_initialized = False
30
  if "index_dimensions" not in st.session_state:
31
  st.session_state.index_dimensions = 1024 # Set this based on your Pinecone index
32
+ if "quiz_submitted" not in st.session_state:
33
+ st.session_state.quiz_submitted = False
34
+ if "quiz_answers" not in st.session_state:
35
+ st.session_state.quiz_answers = {}
36
 
37
  # Functions for PDF processing
38
  def extract_text_from_pdf(pdf_file):
 
281
  st.error(f"Error parsing assignment response: {str(e)}")
282
  return []
283
 
284
+ # Callback functions for quiz submission and reset
285
+ def submit_quiz():
286
+ st.session_state.quiz_submitted = True
287
+
288
+ def reset_quiz():
289
+ st.session_state.quiz_submitted = False
290
+ st.session_state.quiz_answers = {}
291
+
292
  # Streamlit UI
293
  def main():
294
  st.title("📚 PDF Learning Assistant")
 
367
  with st.spinner("Generating quiz..."):
368
  quiz_data = generate_quiz(st.session_state.current_pdf_content, num_questions=quiz_questions)
369
  st.session_state.quiz_data = quiz_data
370
+ # Reset quiz state when generating a new quiz
371
+ reset_quiz()
372
 
373
  # Assignment generation
374
  assignment_type = st.selectbox(
 
440
  if "quiz_data" in st.session_state and st.session_state.quiz_data:
441
  quiz_data = st.session_state.quiz_data
442
 
443
+ # Quiz display logic - static until submitted
 
 
 
444
  if not st.session_state.quiz_submitted:
445
+ # Quiz form
446
+ with st.form(key="quiz_form"):
447
+ for i, question in enumerate(quiz_data):
448
+ st.subheader(f"Question {i+1}")
449
+ st.write(question["question"])
450
+
451
+ options = question["options"]
452
+ option_labels = ["A", "B", "C", "D"]
453
+
454
+ # Create radio buttons for options
455
+ answer = st.radio(
456
+ "Select your answer:",
457
+ options=option_labels[:len(options)],
458
+ key=f"q{i}",
459
+ index=None
460
+ )
461
+
462
+ # Display options
463
+ for j, option in enumerate(options):
464
+ st.write(f"{option_labels[j]}: {option}")
465
+
466
+ # Store answer in session state
467
+ if answer:
468
+ st.session_state.quiz_answers[i] = answer
469
+
470
+ st.divider()
471
 
472
+ # Submit button inside the form
473
+ submit_button = st.form_submit_button("Submit Quiz")
474
+ if submit_button:
475
+ st.session_state.quiz_submitted = True
 
 
476
  else:
477
+ # Show results after submission
478
  correct_count = 0
479
 
480
  for i, question in enumerate(quiz_data):
 
505
  st.subheader(f"Your Score: {correct_count}/{len(quiz_data)}")
506
 
507
  if st.button("Retake Quiz"):
508
+ reset_quiz()
 
 
509
  else:
510
  st.info("Generate a quiz from the sidebar to see it here")
511
 
512
  # Tab 3: Assignment
513
+ # Tab 3: Assignment
514
  with tab3:
515
  st.header("Assignment")
516
 
517
  if "assignment_data" in st.session_state and st.session_state.assignment_data:
518
  assignment_data = st.session_state.assignment_data
519
 
520
+ # Create a form for the assignment
521
+ with st.form(key="assignment_form"):
522
+ for i, question in enumerate(assignment_data):
523
+ st.subheader(f"Question {i+1}")
524
  st.write(question["question"])
525
 
526
+ # Use a checkbox to toggle hints instead of a nested expander
527
  if "hints" in question and question["hints"]:
528
+ show_hints = st.checkbox(f"Show hints for Question {i+1}", key=f"hint_checkbox_{i}")
529
+ if show_hints:
530
+ for hint in question["hints"]:
531
+ st.write(f"- {hint}")
532
 
533
+ # Input area for the answer
534
  st.text_area("Your Answer:", key=f"assignment_q{i}", height=150)
535
+ st.divider()
536
+
537
+ # Add the submit button as a direct child of the form
538
+ submit_assignment = st.form_submit_button("Submit Assignment")
539
+
540
+ # Process form submission outside the form block
541
+ if submit_assignment:
542
+ st.success("Assignment submitted! Here are the key points for each question:")
543
+ for i, question in enumerate(assignment_data):
544
+ with st.expander(f"Key Points for Question {i+1}", expanded=True):
545
  for point in question["key_points"]:
546
  st.write(f"- {point}")
547
  else:
548
  st.info("Generate an assignment from the sidebar to see it here")
549
+
550
 
551
  if __name__ == "__main__":
552
+ main()