# 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.")