import streamlit as st from langchain_community.vectorstores import FAISS from langchain_huggingface import HuggingFaceEmbeddings from transformers import pipeline # -------------------------------------------------- # PAGE CONFIG # -------------------------------------------------- st.set_page_config( page_title="BibleGuard AI", page_icon="✝️", layout="wide" ) # -------------------------------------------------- # EMBEDDINGS # -------------------------------------------------- @st.cache_resource def load_embeddings(): return HuggingFaceEmbeddings( model_name="sentence-transformers/all-MiniLM-L6-v2" ) # -------------------------------------------------- # FAISS # -------------------------------------------------- @st.cache_resource def load_vector_db(): ``` embeddings = load_embeddings() db = FAISS.load_local( "bible_faiss", embeddings, allow_dangerous_deserialization=True ) return db ``` # -------------------------------------------------- # MODEL # -------------------------------------------------- @st.cache_resource def load_llm(): ``` return pipeline( "text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", device_map="auto" ) ``` # -------------------------------------------------- # SAFETY # -------------------------------------------------- def moderate_query(query): ``` blocked = [ "rewrite bible to support racism", "christian terrorism", "religious violence", "hate muslims", "kill unbelievers", "religious supremacy" ] q = query.lower() for item in blocked: if item in q: return False return True ``` # -------------------------------------------------- # QUERY EXPANSION # -------------------------------------------------- def expand_query(question): ``` mapping = { "who was moses": "Moses prophet Israel Exodus Sinai", "who was jesus": "Jesus Christ Messiah Son of God", "forgiveness": "forgiveness mercy repentance sin", "salvation": "saved eternal life salvation", "faith": "faith trust God belief", "love": "love God neighbor charity" } q = question.lower() for key in mapping: if key in q: return mapping[key] return question ``` # -------------------------------------------------- # RETRIEVAL # -------------------------------------------------- def retrieve_context(question): ``` db = load_vector_db() retriever = db.as_retriever( search_type="mmr", search_kwargs={ "k": 8, "fetch_k": 20 } ) docs = retriever.invoke( expand_query(question) ) context = [] citations = [] for doc in docs: context.append( f"{doc.metadata['reference']}: {doc.page_content}" ) citations.append( doc.metadata["reference"] ) return "\n\n".join(context), citations ``` # -------------------------------------------------- # PROMPT # -------------------------------------------------- def build_prompt( question, context, denomination ): ``` return f""" ``` You are a Christianity-focused AI Assistant. Denomination: {denomination} Rules: 1. Use ONLY supplied scripture. 2. Never invent Bible verses. 3. Cite references. 4. Stay aligned with Biblical context. 5. Mention differing viewpoints when appropriate. 6. If uncertain say: 'I cannot verify this from scripture.' Scripture: {context} Question: {question} Answer: """ # -------------------------------------------------- # BIBLE QA # -------------------------------------------------- def christian_assistant( question, denomination ): ``` if not moderate_query(question): return ( "I cannot assist with hateful, violent, or harmful religious content.", [] ) context, citations = retrieve_context( question ) prompt = build_prompt( question, context, denomination ) generator = load_llm() response = generator( prompt, max_new_tokens=200, do_sample=False ) answer = response[0]["generated_text"] if "Answer:" in answer: answer = answer.split("Answer:")[-1].strip() return answer, citations ``` # -------------------------------------------------- # PRAYER # -------------------------------------------------- def generate_prayer(topic): ``` generator = load_llm() prompt = f""" ``` Write a Christian prayer about: {topic} Keep it biblically sound. """ ``` response = generator( prompt, max_new_tokens=200, do_sample=False ) return response[0]["generated_text"] ``` # -------------------------------------------------- # SERMON # -------------------------------------------------- def generate_sermon(topic): ``` generator = load_llm() prompt = f""" ``` Write a Christian sermon about: {topic} Use biblical principles. """ ``` response = generator( prompt, max_new_tokens=300, do_sample=False ) return response[0]["generated_text"] ``` # -------------------------------------------------- # DEVOTIONAL # -------------------------------------------------- def generate_devotional(topic): ``` generator = load_llm() prompt = f""" ``` Write a Christian devotional about: {topic} Include: 1. Reflection 2. Encouragement 3. Prayer """ response = generator( prompt, max_new_tokens=300, do_sample=False ) return response[0]["generated_text"] # -------------------------------------------------- # MEMORY # -------------------------------------------------- if "messages" not in st.session_state: st.session_state.messages = [] # -------------------------------------------------- # SIDEBAR # -------------------------------------------------- with st.sidebar: ``` st.title("✝️ BibleGuard AI") denomination = st.selectbox( "Denomination", [ "Catholic", "Protestant", "Orthodox", "Non-Denominational" ] ) st.markdown("---") st.markdown(""" ``` ### Features ✅ Bible QA ✅ Scripture Citations ✅ Prayer Generator ✅ Sermon Generator ✅ Devotional Generator ✅ Safety Layer ✅ Denomination Awareness """) # -------------------------------------------------- # MAIN UI # -------------------------------------------------- st.title("✝️ BibleGuard AI") st.caption( "Bible-grounded Christian AI Assistant powered by RAG" ) tab1, tab2, tab3, tab4 = st.tabs( [ "Bible QA", "Prayer", "Sermon", "Devotional" ] ) # -------------------------------------------------- # TAB 1 # -------------------------------------------------- with tab1: ``` question = st.chat_input( "Ask a Bible question..." ) if question: answer, citations = christian_assistant( question, denomination ) st.session_state.messages.append( { "question": question, "answer": answer } ) st.markdown("### Answer") st.write(answer) st.markdown("### Citations") st.write(citations) ``` # -------------------------------------------------- # TAB 2 # -------------------------------------------------- with tab2: ``` prayer_topic = st.text_input( "Prayer Topic" ) if st.button( "Generate Prayer" ): st.write( generate_prayer( prayer_topic ) ) ``` # -------------------------------------------------- # TAB 3 # -------------------------------------------------- with tab3: ``` sermon_topic = st.text_input( "Sermon Topic" ) if st.button( "Generate Sermon" ): st.write( generate_sermon( sermon_topic ) ) ``` # -------------------------------------------------- # TAB 4 # -------------------------------------------------- with tab4: ``` devotional_topic = st.text_input( "Devotional Topic" ) if st.button( "Generate Devotional" ): st.write( generate_devotional( devotional_topic ) ) ``` # -------------------------------------------------- # HISTORY # -------------------------------------------------- st.markdown("---") st.subheader("Conversation History") for msg in st.session_state.messages: ``` st.markdown( f"**Q:** {msg['question']}" ) st.markdown( f"**A:** {msg['answer']}" ) ```