File size: 6,221 Bytes
b4c755b
 
 
 
 
 
 
4616d03
b4c755b
 
c7b7427
8db561c
b4c755b
 
 
 
 
 
 
 
 
 
 
 
c7b7427
 
 
 
 
 
4616d03
c7b7427
 
 
 
b4c755b
c7b7427
 
 
 
 
4616d03
8b87e6e
c7b7427
 
 
 
 
 
 
 
 
 
b4c755b
 
8db561c
c7b7427
 
 
 
 
 
 
b4c755b
c7b7427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d72d7ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6cc8f6
d72d7ad
 
 
 
b6cc8f6
d72d7ad
 
 
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import streamlit as st
import tempfile
import json
import random
from crewai_flashcard import generate_flashcards

# --- Set up the Streamlit app ---
st.title("Study Companion: Flashcard Generator")

# Create two tabs: one to generate flashcards from a PDF, and one to practice them.
#tab_gen = st.tabs(["Generate Flashcards"])
#tab_gen, tab_practice = st.tabs(["Generate Flashcards", "Practice Flashcards"])

# Initialize session state for flashcards and flashcard practice
if "flashcards" not in st.session_state:
    st.session_state.flashcards = []  # List of generated flashcards (JSON objects)
if "current_card" not in st.session_state:
    st.session_state.current_card = 0
if "score" not in st.session_state:
    st.session_state.score = 0
if "show_answer" not in st.session_state:
    st.session_state.show_answer = False

# --- Tab 1: Generate Flashcards from PDF ---
#with tab_gen:
st.header("Generate Flashcards from PDF")
uploaded_file = st.file_uploader("Upload PDF file", type="pdf")
#page_range = st.text_input("Enter page range to extract (e.g., '1-5' or '1'):")
flashcard_count = st.number_input("Number of flashcards to generate:", min_value=1, max_value=5, value=3, step=1)

if uploaded_file is not None:# and page_range:
    # Save the uploaded file to a temporary location
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
        tmp.write(uploaded_file.read())
        pdf_file_path = tmp.name
    
    st.success("PDF uploaded successfully!")
    
    if st.button("Generate Flashcards"):
        with st.spinner("Generating flashcards..."):
            # This function runs the CrewAI system and writes flashcards to JSON output
            flashcards_json =  generate_flashcards(pdf_file_path, flashcard_count)#generate_flashcards(pdf_file_path, page_range, flashcard_count)
            #st.markdown(flashcards_json)
        try:
            # Parse the JSON output and save it in session state
            st.session_state.flashcards = json.loads(flashcards_json)
            st.success("Flashcards generated successfully!")
            # Reset practice state
            st.session_state.current_card = 0
            st.session_state.score = 0
            st.session_state.show_answer = False
        except Exception as e:
            st.error(f"Error parsing flashcards: {e}")

# --- Tab 2: Practice Flashcards ---
#with tab_practice:
st.header("Practice Flashcards")
if st.session_state.flashcards:
    total_cards = len(st.session_state.flashcards)
    # Check if we've completed all flashcards
    if st.session_state.current_card >= total_cards:
        st.success(f"You've completed all flashcards! Final Score: {st.session_state.score} / {total_cards}")
        st.info("Restart the practice session or generate new flashcards.")
    else:
        # Safe access: ensure current_card is valid
        current_card = st.session_state.flashcards[st.session_state.current_card]
        st.write(f"**Question:** {current_card.get('question', 'No question provided')}")
        
        # Answer reveal button
        if st.button("Show Answer"):
            st.session_state.show_answer = True
        
        # If answer is revealed, show it and provide feedback options
        if st.session_state.show_answer:
            st.write(f"**Answer:** {current_card.get('answer', 'No answer provided')}")
            col1, col2 = st.columns(2)
            with col1:
                if st.button("Correct"):
                    st.session_state.score += 1
                    st.success("Correct!")
            with col2:
                if st.button("Wrong"):
                    st.error("Incorrect!")
            
            if st.button("Next Card"):
                st.session_state.current_card += 1
                st.session_state.show_answer = False
                st.rerun()
        
        st.write(f"**Current Score:** {st.session_state.score}/{total_cards}")
else:
    st.info("No flashcards available. Please generate flashcards first.")


# # --- Tab 2: Practice Flashcards ---
# with tab_practice:
#     st.header("Practice Flashcards")
#     if st.session_state.flashcards:
#         # Optionally, randomize flashcard order on first run
#         if "shuffled" not in st.session_state:
#             random.shuffle(st.session_state.flashcards)
#             st.session_state.shuffled = True
        
#         current_card = st.session_state.flashcards[st.session_state.current_card]
#         st.write(f"**Question:** {current_card.get('question', 'No question provided')}")
        
#         # Button to reveal the answer
#         if st.button("Show Answer"):
#             st.session_state.show_answer = True
        
#         # If answer is revealed, show answer and feedback options
#         if st.session_state.show_answer:
#             st.write(f"**Answer:** {current_card.get('answer', 'No answer provided')}")
#             col1, col2 = st.columns(2)
#             with col1:
#                 if st.button("Correct"):
#                     st.session_state.score += 1
#                     st.success("Correct!")
#             with col2:
#                 if st.button("Wrong"):
#                     st.error("Incorrect!")

#             # Navigation: if this is the last card, show final score; otherwise, go to next card.
#                 if st.button("Next Card"):
#                     st.session_state.current_card += 1
#                     st.session_state.show_answer = False
#                     #st.experimental_rerun()
            
#             st.write(f"**Current Score:** {st.session_state.score}/{len(st.session_state.flashcards)}")
#     else:
#         st.info("No flashcards available. Please generate flashcards first.")
            
#     #         if st.button("Next Card"):
#     #             st.session_state.current_card = (st.session_state.current_card + 1) % len(st.session_state.flashcards)
#     #             st.session_state.show_answer = False
#     #             st.experimental_rerun()
        
#     #     st.write(f"**Current Score:** {st.session_state.score}/{len(st.session_state.flashcards)}")
#     # else:
#     #     st.info("No flashcards available. Please generate flashcards first.")