Studying from long notes or textbook material can be time-consuming, especially when you have to identify the important concepts yourself. The AI Flashcard Generator makes this process easier by turning a study topic or study material into a set of question-and-answer flashcards.
The application is built using Python, Streamlit, PyTorch, and Hugging Face Transformers. It uses Google's google/flan-t5-base model to generate the questions and answers. Users can choose the number of flashcards, select a difficulty level, and either enter a topic or paste their study material.
Table of Contents
Technologies Used
The project uses the following technologies:
- Python – Handles the application logic and AI workflow.
- Streamlit – Creates the interactive web interface.
- PyTorch – Runs the Transformer model on the CPU or GPU.
- Hugging Face Transformers – Loads the tokenizer and FLAN-T5 model.
- FLAN-T5 – Generates the question-and-answer content.
The model is defined as:
MODEL_NAME = "google/flan-t5-base"
Importing the Libraries
The application starts with:import re import streamlit as st import torch from transformers import AutoTokenizer, AutoModelForSeq2SeqLMStreamlit is used for the interface, while PyTorch manages model execution. The re library is used to parse the AI-generated text, and Hugging Face Transformers provides the tokenizer and sequence-to-sequence model.
Loading the FLAN-T5 Model
The model is loaded using:tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
he tokenizer converts the prompt into tokens that FLAN-T5 can process. The model then uses those tokens to generate the flashcards.
The application also checks whether CUDA is available:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
This allows the application to use a GPU when available and fall back to the CPU otherwise.
The @st.cache_resource decorator is used so that Streamlit does not repeatedly reload the model every time the application reruns.
Creating the AI Prompt
The main generation happens inside generate_cards().
The application creates a structured prompt that tells the model exactly what it needs to produce:
prompt = f"""Create exactly {count} study flashcards from the material below.
Difficulty: {difficulty}
IMPORTANT OUTPUT FORMAT:
Q1: question
A1: short answer
Q2: question
A2: short answer
...
"""
This is an example of prompt engineering. The model is given the required number of cards, difficulty level, output format, and rules for creating the questions and answers.
The prompt also instructs the model not to invent information that is not present in the study material.
Generating the Flashcards
The generate_ai_text() function sends the prompt to FLAN-T5.
First, the prompt is tokenized:
inputs = tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=512
)
The model then generates the response:
output = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
num_beams=5,
do_sample=False,
no_repeat_ngram_size=3,
repetition_penalty=1.1
)
Here, num_beams=5 uses beam search to improve the generated output. do_sample=False keeps the generation deterministic, while no_repeat_ngram_size=3 helps reduce repeated phrases. The repetition penalty also discourages the model from producing repetitive content.Parsing the AI Response
The model is instructed to return flashcards in this format:Q1: What is normalization? A1: Normalization is a database design technique used to reduce redundancy. Q2: Why is normalization important? A2: It helps organize data and reduce data duplication.
The parse_cards() function uses a regular expression to find these question-and-answer pairs.
It then stores them as Python tuples:
(question, answer)This allows the application to display each flashcard separately in the Streamlit interface.
Fallback Generation
AI models do not always follow formatting instructions perfectly. To make the application more reliable, the project includes a fallback_cards() function.
If the model generates fewer flashcards than requested, the application creates additional cards using predefined templates.
For example:
What is [topic]? Why is [topic] important? What is a key idea related to [topic]? Where can [topic] be applied?
This ensures that the application can still return the requested number of cards even when the model does not follow the expected format.
Choosing the Number and Difficulty
count = st.slider(
"Number of flashcards",
min_value=3,
max_value=20,
value=10
)
Users can also select:
["Easy", "Medium", "Hard"]
The selected difficulty is included in the AI prompt so the model can adjust the questions accordingly.
For example, when Hard is selected, the fallback system changes questions such as "What is..." into "Explain...".
Topic or Study Material
Study Topic Study MaterialWith Study Topic, the user can enter something like:
Database Management Systems - Normalization
With Study Material, the user can paste notes, textbook content, lecture material, or an article.
This makes the application useful both for quickly creating cards about a subject and for converting existing study material into revision cards.
Displaying and Downloading the Flashcards
After generation, each card is displayed using a Streamlit expander:
with st.expander(f"Card {index}: {question}"):
st.write(answer)
This keeps the interface clean because users can open individual cards when they want to see the answer.
The complete deck can also be downloaded as a text file:
flashcards.txtThe downloaded file contains each card's question and answer.
How the Application Works
The complete workflow can be summarized as:
Study Topic / Material
↓
Prompt Engineering
↓
FLAN-T5 Model
↓
Question + Answer Generation
↓
Output Parsing
↓
Validation + Fallback
↓
Flashcard Deck
↓
Display / Download
This combination of AI generation, output parsing, and fallback handling makes the application more reliable than simply displaying the raw model response.Requirements
The project uses:streamlit>=1.40.0 transformers>=4.45.0,<5.0.0 torch>=2.2.0 sentencepiece>=0.2.0Install the dependencies with:
pip install -r requirements.txtThen start the application using:
streamlit run app.pyThe first generation can take longer because the pretrained FLAN-T5 model needs to be downloaded.
Conclusion
The AI Flashcard Generator demonstrates how a pretrained Transformer model can be turned into a practical study tool. It uses FLAN-T5 to create concise questions and answers from a topic or study material, while Streamlit provides a simple interface for controlling the number and difficulty of flashcards. The project also goes beyond basic AI generation by parsing the model's response, validating the output, and using fallback templates when necessary. Users can then review the generated cards directly in the application or download the complete deck as a text file.Frequently Asked Questions (FAQs)
1.Which AI model is used in the project?2.Where is Hugging Face used?The application uses Google's google/flan-t5-base model.
3.Can I generate flashcards from my own notes?Hugging Face Transformers is used through AutoTokenizer and AutoModelForSeq2SeqLM to load FLAN-T5 and generate the flashcard content.
4.How many flashcards can be generated?Yes. You can select Study Material and paste your notes, textbook content, lecture material, or other study material.
5.What happens if the AI does not generate enough cards?The application allows between 3 and 20 flashcards per generation.
6.Can I choose the difficulty?The application uses predefined fallback templates to fill in missing cards so that the requested deck size can still be returned.
7.Can I download the flashcards?Yes. You can select Easy, Medium, or Hard before generating the flashcards.
Yes. The generated deck can be downloaded as a flashcards.txt file.
0 Comments