Learn Sphere: Your AI Smart Study Companion
In today’s digital age, technology is redefining education, making it more interactive, personalized, and efficient. To address the growing need for advanced learning tools, I proposed developing an educational application that integrates a AI-powered chatbot. This application will offer real-time answers to students’ queries and summarize documents to enhance their learning experience. In this blog, we’ll explore how to build this application using Python, Streamlit, and Gemini.
Overview
The educational application we are developing includes the following key features:
- AI-Powered Chatbot: An interactive chatbot capable of answering questions across various subjects.
- Document Processing: Functionality to generate Q&A and summaries from uploaded PDFs.
- User-Friendly Interface: An intuitive interface for interacting with the chatbot and processing documents.
Let’s dive into the technical details and see how you can create this application step by step.
Design
High-Level Architecture
The application consists of:
- Frontend: Built using Streamlit, allowing users to interact with the chatbot, upload documents, and view responses.
- Backend: Handles PDF processing, text chunking, embedding generation, and query answering using AI models.
- Storage: Uses FAISS for storing and retrieving document embeddings.
Rationale Behind the Design
- Streamlit: Provides a simple way to create interactive web applications.
- FAISS: Efficiently handles vector storage and similarity search.
- AI Models: Utilize Google’s Gemini for natural language understanding and document processing.
Prerequisites
Before you start, ensure you have the following:
- Python: Install Python 3.x.
- Libraries: Install required libraries using pip install.
- Google Cloud API Key: Obtain your API key from Google Cloud and set it up in a
.env
file.
Step-by-Step Instructions
1. Set Up the Environment
Create a .env
file in your project directory and add your API Key
GOOGLE_API_KEY=your_google_api_key_here
2. Read and Process PDF Files
The function get_pdf_text
extracts text from PDF files
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
3. Split Text into Chunks
Split the extracted text into manageable chunks
def get_text_chunks(text):
splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
chunks = splitter.split_text(text)
return chunks
4. Generate Embeddings and Create Vector Store
Generate embeddings for text chunks and store them using FAISS:
def get_vector_store(chunks):
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
vector_store.save_local("faiss_index")
5. Set Up the Conversational Chain
Create a conversational chain for answering user queries:
def get_conversational_chain():
prompt_template = """
Answer the question as detailed as possible from the provided context. If the answer is not in
the context, just say, "answer is not available in the context".
Context:\n {context}?\n
Question: \n{question}\n
Answer:
"""
model = ChatGoogleGenerativeAI(model="gemini-pro", client=genai, temperature=0.3)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
return chain
6. Handle User Input
Process user queries and retrieve responses:
def user_input(user_question):
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = new_db.similarity_search(user_question)
chain = get_conversational_chain()
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
return response
7. Build the Frontend with Streamlit
Create a Streamlit application to interact with users:
def main():
st.set_page_config(page_title="LearnSphere")
with st.sidebar:
st.title("Menu:")
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
if st.button("Submit & Process"):
with st.spinner("Processing..."):
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
get_vector_store(text_chunks)
st.success("Done") st.title("LearnSphere: Your AI-Powered Learning Assistant")
st.write("Welcome to the chat!")
st.sidebar.button('Clear Chat History', on_click=clear_chat_history) if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant", "content": "Upload PDFs and ask me a question"}] for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"]) if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.write(prompt) if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = user_input(prompt)
full_response = ''.join(response.get('output_text', []))
st.write(full_response)
if response:
st.session_state.messages.append({"role": "assistant", "content": full_response})if __name__ == "__main__":
main()
Conclusion
This blog outlines the process of creating an advanced educational application with AI integration. By leveraging Python, Streamlit, and Google’s Generative AI, we can build a tool that provides interactive learning experiences, personalized support, and efficient document processing. This application not only enhances student engagement but also streamlines their study processes, making learning more effective and enjoyable.
Explore More
- Register for Code Vipassana sessions
- Join the meetup group Datapreneur Social
- Sign up to become Google Cloud Innovator