Overview
Theapp.py module is the main entry point for the DeenPAL Streamlit web application. It provides an interactive chat interface for users to ask questions about Islamic hadiths and receive AI-generated responses.
Application Structure
Entry Point
Run the application using Streamlit:Session State Variables
Streamlit uses session state to persist data across reruns.Stores the complete chat history for the current session. Each message is a dictionary with
role and content keys.Message Structure
Initialization
UI Components
Page Title
Chat History Display
Chat Input
:=) for assignment and condition checking.
Message Handling Flow
1. User Input Display
2. Store User Message
3. RAG Chain Invocation
Input Format
The user’s current question.
Complete conversation history including previous questions and answers.
Response Format
The generated response from the LLM containing hadith citations and explanations.
Retrieved document chunks used to generate the answer (not displayed in UI).
4. Assistant Response Display
5. Store Assistant Response
Complete Application Code
Usage Example
Starting the Application
- Install dependencies:
- Set up environment variables:
- Run the application:
User Interaction Flow
- User opens the web application in browser
- User types a question: “What does Islam say about prayer?”
- Application displays the question immediately
- RAG chain retrieves relevant hadiths and generates response
- Application displays the AI-generated answer with citations
- Conversation continues with full context maintained
Dependencies
streamlit- Web application frameworkchains.rag_chain- RAG pipeline for question answering
Features
Persistent Chat History - Maintains conversation context throughout the session
Real-time Response - Displays messages instantly as they’re processed
Context-Aware - Sends full chat history to RAG chain for better responses
Clean UI - Uses Streamlit’s native chat components for professional appearance
Configuration Options
Customizing the UI
You can modify the appearance by adding Streamlit configuration:Adding Sidebar Information
Chat History Management
Add a button to clear chat history:Performance Considerations
The
load_and_prepare_data() function in chains.py is cached with @st.cache_resource, ensuring the vector database is only initialized once per session, significantly improving performance.Error Handling
For production use, consider adding error handling:Session Lifecycle
- First Visit: Session state is initialized with empty messages list
- User Interaction: Messages are appended to session state
- Page Refresh: Session state persists within the same browser session
- Browser Close: Session state is cleared
- New Tab: Creates a new independent session
