Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from config import process_pdf, process_image, process_audio, generate_flashcards, export_to_apkg
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="π¦ BatAnki", layout="wide")
|
| 7 |
+
|
| 8 |
+
st.title("π¦ BatAnki β AI Flashcard Generator")
|
| 9 |
+
st.markdown("Built for med learners. Powered by AI. Inspired by Batman.")
|
| 10 |
+
|
| 11 |
+
# Sidebar
|
| 12 |
+
st.sidebar.title("π Upload Your Material")
|
| 13 |
+
upload_type = st.sidebar.radio("Select input type:", ["PDF", "Image", "Audio"])
|
| 14 |
+
|
| 15 |
+
uploaded_file = st.sidebar.file_uploader("Upload your file", type=["pdf", "png", "jpg", "jpeg", "mp3", "wav"])
|
| 16 |
+
|
| 17 |
+
if uploaded_file:
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 19 |
+
tmp_file.write(uploaded_file.read())
|
| 20 |
+
file_path = tmp_file.name
|
| 21 |
+
|
| 22 |
+
st.success("β
File uploaded successfully!")
|
| 23 |
+
|
| 24 |
+
if upload_type == "PDF":
|
| 25 |
+
extracted_text = process_pdf(file_path)
|
| 26 |
+
elif upload_type == "Image":
|
| 27 |
+
extracted_text = process_image(file_path)
|
| 28 |
+
elif upload_type == "Audio":
|
| 29 |
+
extracted_text = process_audio(file_path)
|
| 30 |
+
else:
|
| 31 |
+
extracted_text = ""
|
| 32 |
+
|
| 33 |
+
if extracted_text:
|
| 34 |
+
st.subheader("π Extracted Text Preview")
|
| 35 |
+
st.text_area("Text", extracted_text[:5000], height=300)
|
| 36 |
+
|
| 37 |
+
st.subheader("π§ Generate Flashcards")
|
| 38 |
+
if st.button("Generate"):
|
| 39 |
+
with st.spinner("Generating cards using AI..."):
|
| 40 |
+
cards = generate_flashcards(extracted_text)
|
| 41 |
+
st.success(f"β
Generated {len(cards)} cards!")
|
| 42 |
+
|
| 43 |
+
for i, card in enumerate(cards, 1):
|
| 44 |
+
st.markdown(f"**Card {i}**")
|
| 45 |
+
st.markdown(f"- **Q:** {card['question']}")
|
| 46 |
+
st.markdown(f"- **A:** {card['answer']}")
|
| 47 |
+
st.markdown("---")
|
| 48 |
+
|
| 49 |
+
st.subheader("π¦ Export Deck")
|
| 50 |
+
deck_name = st.text_input("Enter deck name", value="BatAnki_Deck")
|
| 51 |
+
if st.button("Export to APKG"):
|
| 52 |
+
apkg_path = export_to_apkg(cards, deck_name)
|
| 53 |
+
with open(apkg_path, "rb") as f:
|
| 54 |
+
st.download_button("β¬οΈ Download .apkg", f, file_name=f"{deck_name}.apkg")
|
| 55 |
+
|
| 56 |
+
else:
|
| 57 |
+
st.info("Click 'Generate' to create flashcards.")
|
| 58 |
+
|
| 59 |
+
os.remove(file_path)
|