Overview
Theapp.py module creates the user-facing Streamlit application, providing an interactive chat interface for querying hadiths.
This is the entry point for users - a clean, conversational interface for asking questions about Islam.
Complete Module Code
Application Structure
1. Title and Setup
- Imports the RAG chain from
chains.py - Sets the page title to “Deen Pal Chatbot”
2. Session State for Chat History
Streamlit usesst.session_state to persist data across reruns:
Why Session State?Streamlit reruns the entire script on every interaction. Without session state, the chat history would be lost after each message.
messages list stores conversation history:
3. Displaying Chat History
The app renders all previous messages:- Loop through all stored messages
st.chat_message(role): Creates a chat bubble with appropriate styling"user": Right-aligned, different color"assistant": Left-aligned, different color
st.markdown(): Renders message content with markdown support
Streamlit’s
st.chat_message() automatically handles:- Message styling
- Avatar display
- Proper alignment
- Visual distinction between roles
4. User Input with st.chat_input
The chat input box at the bottom of the page::=):
- Calls
st.chat_input() - Assigns the return value to
prompt - Checks if
promptis not None (user pressed Enter)
st.chat_input() returns:- The user’s text if they press Enter
Noneif no input yet
5. Storing User Query
Immediately save the user’s message:6. Invoking the RAG Chain
The core retrieval and generation step:input: The current user questionchat_history: Full conversation context (for multi-turn conversations)
What happens during invoke:
- User’s question is embedded as a vector
- ChromaDB retrieves relevant hadiths (MMR search)
- Retrieved documents are stuffed into the prompt
- DeepSeek LLM generates a structured answer
- Response is returned to the UI
7. Displaying the Assistant’s Response
Show the generated answer to the user:- Creates an assistant chat bubble
- Renders the answer with markdown formatting
- Supports bold, italics, lists, etc.
8. Storing Assistant Response
Finally, save the assistant’s answer to history:User Experience Flow
-
User opens app
- Sees “Deen Pal Chatbot” title
- Empty chat interface
- Input box at bottom
-
User types question
- “What does Islam say about honesty?”
- Presses Enter
-
Message appears
- User’s question shown in user bubble
- Loading indicator while processing
-
Assistant responds
- Retrieved hadiths with citations
- Brief explanations
- Summary answer
- All in assistant bubble
-
Conversation continues
- User can ask follow-up questions
- Full context maintained
- Previous messages remain visible
Key Streamlit Components
| Component | Purpose | Code |
|---|---|---|
st.title() | Page title | st.title("Deen Pal Chatbot") |
st.session_state | Persist data | st.session_state.messages |
st.chat_message() | Chat bubble | with st.chat_message("user"): |
st.markdown() | Render text | st.markdown(content) |
st.chat_input() | Input box | st.chat_input("placeholder") |
Running the Application
To launch the app:- Load all dependencies
- Initialize the data loader (via
@st.cache_resource) - Build the RAG chain
- Open browser at
http://localhost:8501 - Display the chat interface
First Run:
The initial startup takes longer because:
- PDFs are loaded and processed
- Embeddings are generated
- ChromaDB is initialized
Design Choices
Simple and Clean
The UI is intentionally minimal:- No complex sidebars or settings
- Focus on conversation
- Familiar chat interface
Stateful Conversations
Chat history enables:- Follow-up questions
- Context-aware responses
- Natural conversation flow
Markdown Support
Usingst.markdown() allows:
- Formatted hadith citations
- Structured responses
- Better readability
Error Handling
Current implementation:
No explicit error handling. If the RAG chain fails, Streamlit will display an error.Production consideration:
Wrap the
rag_chain.invoke() call in a try-except block to handle:- API failures
- Network issues
- Invalid responses
Dependencies
- Streamlit: Web framework
- chains.py: Provides the RAG pipeline
Customization Opportunities
The simple structure allows easy extensions:-
Add sidebar settings
-
Stream responses
-
Export conversation
-
Clear chat button
