Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import json
|
| 4 |
+
import random
|
| 5 |
+
from crewai_flashcard import generate_flashcards
|
| 6 |
+
|
| 7 |
+
# --- Set up the Streamlit app ---
|
| 8 |
+
st.title("Study Companion: Flashcard Generator & Practice")
|
| 9 |
+
|
| 10 |
+
# Create two tabs: one to generate flashcards from a PDF, and one to practice them.
|
| 11 |
+
tab_gen, tab_practice = st.tabs(["Generate Flashcards", "Practice Flashcards"])
|
| 12 |
+
|
| 13 |
+
# Initialize session state for flashcards and flashcard practice
|
| 14 |
+
if "flashcards" not in st.session_state:
|
| 15 |
+
st.session_state.flashcards = [] # List of generated flashcards (JSON objects)
|
| 16 |
+
if "current_card" not in st.session_state:
|
| 17 |
+
st.session_state.current_card = 0
|
| 18 |
+
if "score" not in st.session_state:
|
| 19 |
+
st.session_state.score = 0
|
| 20 |
+
if "show_answer" not in st.session_state:
|
| 21 |
+
st.session_state.show_answer = False
|
| 22 |
+
|
| 23 |
+
# --- Tab 1: Generate Flashcards from PDF ---
|
| 24 |
+
with tab_gen:
|
| 25 |
+
st.header("Generate Flashcards from PDF")
|
| 26 |
+
uploaded_file = st.file_uploader("Upload PDF file", type="pdf")
|
| 27 |
+
page_range = st.text_input("Enter page range to extract (e.g., '1-5' or '1'):")
|
| 28 |
+
flashcard_count = st.number_input("Number of flashcards to generate:", min_value=1, max_value=20, value=5, step=1)
|
| 29 |
+
|
| 30 |
+
if uploaded_file is not None and page_range:
|
| 31 |
+
# Save the uploaded file to a temporary location
|
| 32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 33 |
+
tmp.write(uploaded_file.read())
|
| 34 |
+
pdf_file_path = tmp.name
|
| 35 |
+
|
| 36 |
+
st.success("PDF uploaded successfully!")
|
| 37 |
+
|
| 38 |
+
if st.button("Generate Flashcards"):
|
| 39 |
+
with st.spinner("Generating flashcards..."):
|
| 40 |
+
# This function runs the CrewAI system and writes flashcards to JSON output
|
| 41 |
+
flashcards_json = generate_flashcards(pdf_file_path, page_range, flashcard_count)
|
| 42 |
+
try:
|
| 43 |
+
# Parse the JSON output and save it in session state
|
| 44 |
+
st.session_state.flashcards = json.loads(flashcards_json)
|
| 45 |
+
st.success("Flashcards generated successfully!")
|
| 46 |
+
# Reset practice state
|
| 47 |
+
st.session_state.current_card = 0
|
| 48 |
+
st.session_state.score = 0
|
| 49 |
+
st.session_state.show_answer = False
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"Error parsing flashcards: {e}")
|
| 52 |
+
|
| 53 |
+
# --- Tab 2: Practice Flashcards ---
|
| 54 |
+
with tab_practice:
|
| 55 |
+
st.header("Practice Flashcards")
|
| 56 |
+
if st.session_state.flashcards:
|
| 57 |
+
# Optionally, randomize flashcard order on first run
|
| 58 |
+
if "shuffled" not in st.session_state:
|
| 59 |
+
random.shuffle(st.session_state.flashcards)
|
| 60 |
+
st.session_state.shuffled = True
|
| 61 |
+
|
| 62 |
+
current_card = st.session_state.flashcards[st.session_state.current_card]
|
| 63 |
+
st.write(f"**Question:** {current_card.get('question', 'No question provided')}")
|
| 64 |
+
|
| 65 |
+
# Button to reveal the answer
|
| 66 |
+
if st.button("Show Answer"):
|
| 67 |
+
st.session_state.show_answer = True
|
| 68 |
+
|
| 69 |
+
# If answer is revealed, show answer and feedback options
|
| 70 |
+
if st.session_state.show_answer:
|
| 71 |
+
st.write(f"**Answer:** {current_card.get('answer', 'No answer provided')}")
|
| 72 |
+
col1, col2 = st.columns(2)
|
| 73 |
+
with col1:
|
| 74 |
+
if st.button("Correct"):
|
| 75 |
+
st.session_state.score += 1
|
| 76 |
+
st.success("Correct!")
|
| 77 |
+
with col2:
|
| 78 |
+
if st.button("Wrong"):
|
| 79 |
+
st.error("Incorrect!")
|
| 80 |
+
|
| 81 |
+
if st.button("Next Card"):
|
| 82 |
+
st.session_state.current_card = (st.session_state.current_card + 1) % len(st.session_state.flashcards)
|
| 83 |
+
st.session_state.show_answer = False
|
| 84 |
+
st.experimental_rerun()
|
| 85 |
+
|
| 86 |
+
st.write(f"**Current Score:** {st.session_state.score}/{len(st.session_state.flashcards)}")
|
| 87 |
+
else:
|
| 88 |
+
st.info("No flashcards available. Please generate flashcards first.")
|