Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,79 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from config import (
|
| 3 |
-
process_pdf,
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
)
|
| 6 |
-
import tempfile
|
| 7 |
-
import os
|
| 8 |
-
import uuid
|
| 9 |
-
|
| 10 |
-
st.set_page_config(page_title="π¦ BatAnki Pro", layout="wide")
|
| 11 |
-
st.title("π¦ BatAnki β AI Flashcard Engine for Learners")
|
| 12 |
|
| 13 |
-
st.
|
| 14 |
-
|
| 15 |
-
deck_name = st.sidebar.text_input("π Deck name", value="BatAnki_Deck")
|
| 16 |
-
model_choice = st.sidebar.selectbox("π§ AI Model", ["t5-base", "microsoft/BioGPT"])
|
| 17 |
-
generate_types = st.sidebar.multiselect("Card Types", ["Q&A", "Cloze", "MCQ", "Reverse"], default=["Q&A"])
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
uploaded_file = st.sidebar.file_uploader("Upload file", type=["pdf", "png", "jpg", "jpeg", "mp3", "wav", "txt", "docx"])
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
file_path = None
|
| 30 |
|
| 31 |
if uploaded_file:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
if
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.success("β
Text extracted!")
|
| 54 |
-
st.session_state["extracted_text"] = extracted_text
|
| 55 |
-
st.session_state["deck_name"] = deck_name
|
| 56 |
-
st.session_state["generate_types"] = generate_types
|
| 57 |
-
st.session_state["model"] = model_choice
|
| 58 |
|
| 59 |
-
if
|
| 60 |
-
st.
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
types=st.session_state["generate_types"]
|
| 69 |
-
)
|
| 70 |
-
st.session_state["cards"] = cards
|
| 71 |
-
st.success(f"β
Generated {len(cards)} cards")
|
| 72 |
-
|
| 73 |
-
if "cards" in st.session_state:
|
| 74 |
-
st.subheader("π§Ύ Flashcard Preview & Tagging")
|
| 75 |
-
tagged_cards = []
|
| 76 |
-
for idx, card in enumerate(st.session_state["cards"], 1):
|
| 77 |
-
st.markdown(f"**Card {idx}**")
|
| 78 |
-
question = st.text_input(f"Q{idx}", card["question"])
|
| 79 |
-
answer = st.text_area(f"A{idx}", card["answer"])
|
| 80 |
-
tag = st.text_input(f"π·οΈ Tag for Card {idx}", value=card.get("tag", ""))
|
| 81 |
-
tagged_cards.append({"question": question, "answer": answer, "tag": tag})
|
| 82 |
st.markdown("---")
|
| 83 |
-
st.session_state["cards"] = tagged_cards
|
| 84 |
|
| 85 |
-
st.subheader("π¦ Export")
|
| 86 |
col1, col2 = st.columns(2)
|
| 87 |
-
|
| 88 |
with col1:
|
| 89 |
-
if st.button("
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.download_button("β¬οΈ Download APKG", f, f"{st.session_state['deck_name']}.apkg")
|
| 93 |
-
|
| 94 |
with col2:
|
| 95 |
-
if st.button("
|
| 96 |
-
path =
|
| 97 |
with open(path, "rb") as f:
|
| 98 |
-
st.download_button("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import fitz
|
| 5 |
from config import (
|
| 6 |
+
process_pdf,
|
| 7 |
+
process_text,
|
| 8 |
+
process_image,
|
| 9 |
+
process_audio,
|
| 10 |
+
process_youtube,
|
| 11 |
+
generate_flashcards,
|
| 12 |
+
export_to_apkg,
|
| 13 |
+
export_to_csv,
|
| 14 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
st.set_page_config(page_title="π¦ BatAnki AI", layout="wide")
|
| 17 |
+
st.title("π BatAnki β AI Flashcard Generator")
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
st.sidebar.markdown("π **Input Options**")
|
| 20 |
+
uploaded_file = st.sidebar.file_uploader("Upload a file", type=["pdf", "txt", "docx", "jpg", "png", "mp3", "wav"])
|
| 21 |
+
youtube_url = st.sidebar.text_input("Or paste YouTube URL")
|
| 22 |
|
| 23 |
+
deck_name = st.text_input("Deck Name", value="BatAnkiDeck")
|
| 24 |
+
types_selected = st.multiselect("Card Types", ["Q&A", "Cloze", "MCQ", "Reverse"], default=["Q&A"])
|
| 25 |
+
max_cards = st.slider("Max Cards", 5, 1000, 50)
|
|
|
|
| 26 |
|
| 27 |
+
input_text = ""
|
| 28 |
+
cards = []
|
|
|
|
| 29 |
|
| 30 |
if uploaded_file:
|
| 31 |
+
suffix = uploaded_file.name.split(".")[-1]
|
| 32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{suffix}") as tmp_file:
|
| 33 |
+
tmp_file.write(uploaded_file.read())
|
| 34 |
+
tmp_path = tmp_file.name
|
| 35 |
|
| 36 |
+
if suffix == "pdf":
|
| 37 |
+
doc = fitz.open(tmp_path)
|
| 38 |
+
st.info("PDF preview:")
|
| 39 |
+
page_number = st.number_input("Page", 1, len(doc), 1)
|
| 40 |
+
page = doc[page_number - 1]
|
| 41 |
+
pix = page.get_pixmap()
|
| 42 |
+
st.image(pix.tobytes("png"), caption=f"Page {page_number}")
|
| 43 |
+
text = page.get_text()
|
| 44 |
+
input_text = text if text.strip() else process_pdf(tmp_path)
|
| 45 |
+
if st.button("Generate Cards from This Page"):
|
| 46 |
+
cards = generate_flashcards(input_text, types_selected, max_cards)
|
| 47 |
+
elif suffix in ["jpg", "png"]:
|
| 48 |
+
input_text = process_image(tmp_path)
|
| 49 |
+
st.image(tmp_path)
|
| 50 |
+
elif suffix in ["mp3", "wav"]:
|
| 51 |
+
input_text = process_audio(tmp_path)
|
| 52 |
+
elif suffix in ["txt", "docx"]:
|
| 53 |
+
input_text = process_text(tmp_path)
|
| 54 |
|
| 55 |
+
elif youtube_url:
|
| 56 |
+
st.info("Processing YouTube audio...")
|
| 57 |
+
input_text = process_youtube(youtube_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if input_text and not cards:
|
| 60 |
+
if st.button("Generate Cards"):
|
| 61 |
+
cards = generate_flashcards(input_text, types_selected, max_cards)
|
| 62 |
|
| 63 |
+
if cards:
|
| 64 |
+
st.subheader("Generated Flashcards")
|
| 65 |
+
for i, card in enumerate(cards):
|
| 66 |
+
st.markdown(f"**{i+1}. {card['question']}**")
|
| 67 |
+
st.markdown(f"*Answer:* {card['answer']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
st.markdown("---")
|
|
|
|
| 69 |
|
|
|
|
| 70 |
col1, col2 = st.columns(2)
|
|
|
|
| 71 |
with col1:
|
| 72 |
+
if st.button("Export to CSV"):
|
| 73 |
+
export_to_csv(cards)
|
| 74 |
+
st.success("Exported CSV")
|
|
|
|
|
|
|
| 75 |
with col2:
|
| 76 |
+
if st.button("Export to Anki (.apkg)"):
|
| 77 |
+
path = export_to_apkg(cards, deck_name)
|
| 78 |
with open(path, "rb") as f:
|
| 79 |
+
st.download_button("Download .apkg", f, file_name=path)
|