Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,98 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from config import
|
|
|
|
|
|
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
st.set_page_config(page_title="π¦ BatAnki", layout="wide")
|
|
|
|
| 7 |
|
| 8 |
-
st.title("
|
| 9 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
upload_type = st.sidebar.radio("Select input type:", ["PDF", "Image", "Audio"])
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
file_path = tmp_file.name
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
if extracted_text:
|
| 34 |
-
st.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
st.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from config import (
|
| 3 |
+
process_pdf, process_image, process_audio, process_text, process_youtube,
|
| 4 |
+
generate_flashcards, export_to_apkg, export_to_csv
|
| 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.sidebar.title("π Upload Source")
|
| 14 |
+
upload_type = st.sidebar.radio("Select input type", ["PDF", "Image", "Audio", "Text File", "YouTube Link"])
|
| 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 |
+
uploaded_file = None
|
| 20 |
+
yt_url = ""
|
|
|
|
| 21 |
|
| 22 |
+
if upload_type == "YouTube Link":
|
| 23 |
+
yt_url = st.sidebar.text_input("πΊ YouTube URL")
|
| 24 |
+
else:
|
| 25 |
+
uploaded_file = st.sidebar.file_uploader("Upload file", type=["pdf", "png", "jpg", "jpeg", "mp3", "wav", "txt", "docx"])
|
| 26 |
|
| 27 |
+
# Extract text from the file
|
| 28 |
+
extracted_text = ""
|
| 29 |
+
file_path = None
|
|
|
|
| 30 |
|
| 31 |
+
if uploaded_file:
|
| 32 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
| 33 |
+
tmp.write(uploaded_file.read())
|
| 34 |
+
file_path = tmp.name
|
| 35 |
|
| 36 |
+
if st.sidebar.button("π§ Extract Text"):
|
| 37 |
if upload_type == "PDF":
|
| 38 |
extracted_text = process_pdf(file_path)
|
| 39 |
elif upload_type == "Image":
|
| 40 |
extracted_text = process_image(file_path)
|
| 41 |
elif upload_type == "Audio":
|
| 42 |
extracted_text = process_audio(file_path)
|
| 43 |
+
elif upload_type == "Text File":
|
| 44 |
+
extracted_text = process_text(file_path)
|
| 45 |
+
elif upload_type == "YouTube Link" and yt_url:
|
| 46 |
+
extracted_text = process_youtube(yt_url)
|
| 47 |
else:
|
| 48 |
+
st.warning("Please upload or enter a valid input.")
|
| 49 |
+
|
| 50 |
+
if not extracted_text.strip():
|
| 51 |
+
st.error("No text extracted.")
|
| 52 |
+
else:
|
| 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 "extracted_text" in st.session_state:
|
| 60 |
+
st.subheader("π Extracted Text")
|
| 61 |
+
st.text_area("Extracted Content", st.session_state["extracted_text"][:10000], height=300)
|
| 62 |
+
|
| 63 |
+
if st.button("π Generate Flashcards"):
|
| 64 |
+
with st.spinner("Generating cards..."):
|
| 65 |
+
cards = generate_flashcards(
|
| 66 |
+
st.session_state["extracted_text"],
|
| 67 |
+
model_name=st.session_state["model"],
|
| 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("π₯ Export to Anki (.apkg)"):
|
| 90 |
+
path = export_to_apkg(st.session_state["cards"], st.session_state["deck_name"])
|
| 91 |
+
with open(path, "rb") as f:
|
| 92 |
+
st.download_button("β¬οΈ Download APKG", f, f"{st.session_state['deck_name']}.apkg")
|
| 93 |
+
|
| 94 |
+
with col2:
|
| 95 |
+
if st.button("π Export to CSV"):
|
| 96 |
+
path = export_to_csv(st.session_state["cards"], st.session_state["deck_name"])
|
| 97 |
+
with open(path, "rb") as f:
|
| 98 |
+
st.download_button("β¬οΈ Download CSV", f, f"{st.session_state['deck_name']}.csv")
|