File size: 2,247 Bytes
f842e3c
 
 
 
cc79fb0
f842e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc79fb0
 
 
 
 
 
 
 
 
f842e3c
cc79fb0
 
 
f842e3c
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import streamlit as st
import tempfile
import json
from pathlib import Path
from crewai_flashcard import generate_flashcards

st.title("📚 Flashcard Generator from PDF")

st.write("Upload a PDF, specify a page range (e.g. '1' or '1-5'), and choose the number of flashcards to generate.")

# PDF uploader
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=20, value=5, step=1)

if uploaded_file is not None and page_range:
    # Save the uploaded file to a temporary file
    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 call will run the CrewAI crew,
            # which outputs flashcards.json to the working directory.
            generate_flashcards(pdf_file_path, page_range, flashcard_count)
        
        # # Read the flashcards.json file generated by the flashcard task
        # try:
        #     with open("flashcards.json", "r") as f:
        #         flashcards = json.load(f)
        # except Exception as e:
        #     st.error(f"Error reading flashcards.json: {e}")
        #     flashcards = []

         # Alternative method: use pathlib to read the flashcards.json file
        try:
            flashcards_path = Path("flashcards.json")
            flashcards_data = flashcards_path.read_text(encoding="utf-8")
            flashcards = json.loads(flashcards_data)
        except Exception as e:
            st.error(f"Error reading flashcards.json: {e}")
            flashcards = []

        st.subheader("Generated Flashcards")
        if flashcards:
            for idx, card in enumerate(flashcards):
                st.markdown(f"**Flashcard {idx+1}:**")
                st.write(f"**Question:** {card.get('question', '')}")
                st.write(f"**Answer:** {card.get('answer', '')}")
        else:
            st.write("No flashcards generated.")