Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import tempfile
|
| 4 |
+
import json
|
| 5 |
+
from crewai_flashcard import generate_flashcards
|
| 6 |
+
|
| 7 |
+
st.title("📚 Flashcard Generator from PDF")
|
| 8 |
+
|
| 9 |
+
st.write("Upload a PDF, specify a page range (e.g. '1' or '1-5'), and choose the number of flashcards to generate.")
|
| 10 |
+
|
| 11 |
+
# PDF uploader
|
| 12 |
+
uploaded_file = st.file_uploader("Upload PDF file", type="pdf")
|
| 13 |
+
page_range = st.text_input("Enter page range to extract (e.g. '1-5' or '1'):")
|
| 14 |
+
flashcard_count = st.number_input("Number of flashcards to generate:", min_value=1, max_value=20, value=5, step=1)
|
| 15 |
+
|
| 16 |
+
if uploaded_file is not None and page_range:
|
| 17 |
+
# Save the uploaded file to a temporary file
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 19 |
+
tmp.write(uploaded_file.read())
|
| 20 |
+
pdf_file_path = tmp.name
|
| 21 |
+
|
| 22 |
+
st.success("PDF uploaded successfully!")
|
| 23 |
+
|
| 24 |
+
if st.button("Generate Flashcards"):
|
| 25 |
+
with st.spinner("Generating flashcards..."):
|
| 26 |
+
# This function call will run the CrewAI crew,
|
| 27 |
+
# which outputs flashcards.json to the working directory.
|
| 28 |
+
generate_flashcards(pdf_file_path, page_range, flashcard_count)
|
| 29 |
+
|
| 30 |
+
# Read the flashcards.json file generated by the flashcard task
|
| 31 |
+
try:
|
| 32 |
+
with open("flashcards.json", "r") as f:
|
| 33 |
+
flashcards = json.load(f)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Error reading flashcards.json: {e}")
|
| 36 |
+
flashcards = []
|
| 37 |
+
|
| 38 |
+
st.subheader("Generated Flashcards")
|
| 39 |
+
if flashcards:
|
| 40 |
+
for idx, card in enumerate(flashcards):
|
| 41 |
+
st.markdown(f"**Flashcard {idx+1}:**")
|
| 42 |
+
st.write(f"**Question:** {card.get('question', '')}")
|
| 43 |
+
st.write(f"**Answer:** {card.get('answer', '')}")
|
| 44 |
+
else:
|
| 45 |
+
st.write("No flashcards generated.")
|