File size: 3,456 Bytes
1d8ed3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
import streamlit as st
from app.utils import debug_log
from app.pages.utils_chapter.chapter_extraction import *
from app.backend.raw_text_processing import *
from app.backend.get_requests import generate_questions_from_chapter, generate_questions_from_chapter_edgecase
from app.backend.text_processing import chapters_chunking
from app.backend.chunks_processing import get_chapter_context


def get_chapter_titles(chapters):
    """Generate list of chapter titles from chapters dict."""
    if chapters and isinstance(chapters[0], dict) and 'chapter_title' in chapters[0]:
        return [f"Chapter {idx}: {ch['chapter_title']}" for idx, ch in enumerate(chapters, start=1)]
    return []

def select_chapter(chapter_titles):
    """Show a multiselect for chapters, max 1 selection."""
    return st.multiselect(
        "Select a Chapter:",
        chapter_titles,
        max_selections=1,
        help="Choose one chapter to generate questions from."
    )

def select_num_questions():
    """Number input for question count."""
    return st.number_input(
        "Number of questions to generate (max 5)",
        min_value=1,
        max_value=5,
        step=1,
        format="%d",
        help="Choose how many questions to generate."
    )

def chapter_question_form():
    with st.form("chapter_question_form"):
        st.subheader("Chapter Selection")
        breaks(1)
        chapters = st.session_state.get('chapters_dict', [])
        chapter_titles = get_chapter_titles(chapters)

        options = select_chapter(chapter_titles)
        col1, _ = st.columns([2, 6])
        with col1:
            num_questions = select_num_questions()

        breaks(1)
        submitted = st.form_submit_button("Generate Questions")

        if submitted:
            if not options:
                st.warning("Please select a chapter before submitting.")
                return

            selected_chapter = options[0]
            selected_index = chapter_titles.index(selected_chapter)

            # Save selections in session state
            st.session_state.selected_chapter_title = selected_chapter
            st.session_state.selected_chapter_idx = selected_index
            st.session_state.num_questions = num_questions

            debug_log(f"Selected chapter: {selected_chapter}")
            debug_log(f"Selected index: {selected_index}")
            debug_log(f"Generating {num_questions} questions...")

            # Get chapter context (assumed to populate st.session_state['chapter_selected_chunks'])
            get_chapter_context(
                st.session_state['chapters_chunked'],
                selected_index,
                num_questions
            )

            debug_log(f"Selected chapter chunks: {len(st.session_state.get('chapter_selected_chunks', []))}")

            # Now trigger question generation with spinner
            chunks = st.session_state.get('chapter_selected_chunks', [])
            with st.spinner("✨ Thinking..."):
                if len(chunks) >= num_questions:
                    generated_questions = generate_questions_from_chapter(chunks, num_questions)
                else:
                    st.info("Using edge-case strategy: fewer chunks than questions.")
                    generated_questions = generate_questions_from_chapter_edgecase(chunks, num_questions)

                st.session_state['questions_ready_chapter'] = True 
                return generated_questions