Download src/streamlit_app.py from faiz3123/prophets: direct link, hf CLI and curl.
- Browser
- Download file 12.1 kB
-
https://huggingface.co/spaces/faiz3123/prophets/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/faiz3123/prophets/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/faiz3123/prophets/resolve/main/src/streamlit_app.py
12.1 kB
| import streamlit as st | |
| import google.generativeai as genai | |
| import time | |
| from datetime import datetime | |
| # Configure the page | |
| st.set_page_config( | |
| page_title="Quranic Prophets Stories", | |
| page_icon="📿", | |
| layout="centered", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Initialize the Gemini model | |
| def setup_gemini(api_key): | |
| genai.configure(api_key=api_key) | |
| return genai.GenerativeModel('gemini-2.0-flash') | |
| # System prompt to guide the AI responses for prophets stories | |
| PROPHETS_SYSTEM_PROMPT = """ | |
| You are a knowledgeable Islamic scholar specializing in the stories of prophets from the Quran. | |
| Your responses must be based ONLY on the Quran and authentic Tafseer (exegesis) sources. | |
| STRICT GUIDELINES: | |
| 1. Only provide information that is directly mentioned in the Quran or well-established authentic Tafseer | |
| 2. Never invent stories, add details, or speculate beyond what is in authentic sources | |
| 3. Always include specific Quranic references (Surah name and verse numbers) for each part of the narrative | |
| 4. If something is not explicitly mentioned in the Quran, clearly state that | |
| 5. Maintain respect and reverence for all prophets | |
| 6. Respond in the exact language requested by the user (English, Urdu, or Roman English) | |
| When sharing stories of prophets: | |
| - Always begin with "Bismillah Hir Rahman Nir Raheem" | |
| - Provide the Arabic name followed by English name in parentheses | |
| - Structure your response with clear sections: Introduction, Narrative, Lessons, and References | |
| - For each event mentioned, cite the specific Quranic verses (Surah name and verse numbers) | |
| - Share the story accurately based on Quranic narrative | |
| - Explain the lessons and wisdom from the story | |
| - Conclude with a relevant dua | |
| SPECIFIC FORMATTING REQUIREMENTS: | |
| - Under a "Quranic References" section, list all referenced verses with Surah names and verse numbers | |
| - For each major event in the narrative, include the specific Quranic reference in parentheses | |
| - Use this format for references: (Surah Name Chapter:Verse) | |
| If asked about something not explicitly in the Quran, respond: "This detail is not explicitly mentioned in the Quran. We should avoid speculating about matters not revealed by Allah." | |
| For Urdu responses, use proper Urdu script with complete diacritics and formatting. | |
| For Roman English, use English script but maintain Urdu sentence structure and key Urdu terms. | |
| """ | |
| # List of prophets from the Quran | |
| PROPHETS = [ | |
| "آدم (Adam)", | |
| "إدریس (Idris)", | |
| "نوح (Nuh)", | |
| "ہود (Hud)", | |
| "صالح (Salih)", | |
| "إبراهيم (Ibrahim)", | |
| "لوط (Lut)", | |
| "إسماعيل (Ismail)", | |
| "إسحاق (Ishaq)", | |
| "يعقوب (Yaqub)", | |
| "یوسف (Yusuf)", | |
| "أيوب (Ayyub)", | |
| "شعيب (Shuayb)", | |
| "موسى (Musa)", | |
| "هارون (Harun)", | |
| "اليسع (Al-Yasa)", | |
| "ذو الكفل (Dhul-Kifl)", | |
| "داود (Dawud)", | |
| "سليمان (Sulaiman)", | |
| "إلياس (Ilyas)", | |
| "يونس (Yunus)", | |
| "زكريا (Zakariya)", | |
| "يحيى (Yahya)", | |
| "عيسى (Isa)", | |
| "محمد (Muhammad) صلی اللہ علیہ وسلم" | |
| ] | |
| # Language-specific instructions | |
| LANGUAGE_INSTRUCTIONS = { | |
| "English": "Respond in clear English with proper Islamic terminology.", | |
| "Urdu": "Respond in complete Urdu sentences using proper Urdu script (نستعلیق فونٹ) with diacritics. Use authentic Islamic terms in Urdu.", | |
| "Roman English": "Respond using English alphabet but with Urdu sentence structure. Include important Urdu terms in parentheses where needed." | |
| } | |
| # Initialize session state | |
| if "prophet_messages" not in st.session_state: | |
| st.session_state.prophet_messages = [] | |
| st.session_state.prophet_messages.append({"role": "assistant", "content": "Assalamu Alaikum! Select a prophet and story type to learn about their Quranic narrative."}) | |
| # App title and description | |
| st.title("📖 Stories of Prophets from the Quran") | |
| st.markdown(""" | |
| Explore the authentic stories of prophets as mentioned in the Holy Quran with proper references and translations. | |
| """) | |
| # Sidebar for controls | |
| with st.sidebar: | |
| st.header("Configuration") | |
| api_key = st.text_input("Enter your Gemini API key:", type="password", value="AIzaSyCWxM6Ic9zs-hZiovnMU8a4UJko7ObqPqU") | |
| if api_key: | |
| try: | |
| model = setup_gemini(api_key) | |
| st.success("API key configured successfully!") | |
| except Exception as e: | |
| st.error(f"Error configuring API: {e}") | |
| else: | |
| st.info("Please enter your Gemini API key to begin") | |
| model = None | |
| st.divider() | |
| st.subheader("Select Prophet") | |
| selected_prophet = st.selectbox("Choose a prophet:", PROPHETS) | |
| st.subheader("Story Type") | |
| story_type = st.radio("Select story type:", | |
| ["Summary of Life", "Detailed Narrative", "Specific Event"]) | |
| st.subheader("Language") | |
| language = st.radio("Select language:", | |
| ["English", "Urdu", "Roman English"]) | |
| generate_btn = st.button("Generate Story", type="primary") | |
| st.divider() | |
| st.subheader("About") | |
| st.markdown(""" | |
| This application provides authentic stories of prophets from the Quran with: | |
| - Direct Quranic references (Surah and verse numbers) | |
| - Accurate narratives based on Islamic sources | |
| - Multiple language options | |
| - Different detail levels based on your preference | |
| """) | |
| st.divider() | |
| st.caption("Note: All information is sourced from the Quran and authentic Tafseer. For matters of Islamic law, consult qualified scholars.") | |
| # Display chat messages | |
| for message in st.session_state.prophet_messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Generate story when button is clicked | |
| if generate_btn and selected_prophet: | |
| # Clear previous messages except the first one | |
| if len(st.session_state.prophet_messages) > 1: | |
| st.session_state.prophet_messages = [st.session_state.prophet_messages[0]] | |
| # Create prompt based on user selection | |
| prompt = f""" | |
| Please provide a {story_type.lower()} of Prophet {selected_prophet} in {language}. | |
| SPECIFIC REQUIREMENTS: | |
| 1. Base the narrative ONLY on what is mentioned in the Quran and authentic Tafseer | |
| 2. Include specific Quranic references (Surah names and verse numbers) for each event mentioned | |
| 3. Do not add any details not explicitly mentioned in authentic sources | |
| 4. Structure your response with clear sections: Introduction, Narrative, Lessons, and Quranic References | |
| 5. For each event in the narrative, include the specific Quranic reference in parentheses | |
| 6. Create a dedicated "Quranic References" section listing all referenced verses | |
| 7. Share the lessons and wisdom from the story | |
| 8. {LANGUAGE_INSTRUCTIONS[language]} | |
| Important: For Urdu responses, you MUST use complete Urdu script (نستعلیق) with proper diacritics. | |
| Format references as: (Surah Name Chapter:Verse) | |
| """ | |
| # Add user message to chat history | |
| st.session_state.prophet_messages.append({"role": "user", "content": f"Tell me about {selected_prophet} - {story_type} in {language}"}) | |
| # Display user message | |
| with st.chat_message("user"): | |
| st.markdown(f"Tell me about {selected_prophet} - {story_type} in {language}") | |
| # Display assistant response | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| message_placeholder.markdown("Generating authentic Quranic narrative with references...") | |
| if not api_key: | |
| message_placeholder.markdown("Please enter your Gemini API key in the sidebar to use the application.") | |
| st.session_state.prophet_messages.append({"role": "assistant", "content": "Please enter your Gemini API key in the sidebar to use the application."}) | |
| else: | |
| try: | |
| # Generate response using Gemini | |
| full_response = "" | |
| # Create a more explicit prompt for the model | |
| explicit_prompt = f""" | |
| LANGUAGE: {language} | |
| {LANGUAGE_INSTRUCTIONS[language]} | |
| PROPHET: {selected_prophet} | |
| STORY TYPE: {story_type} | |
| Please provide an authentic Islamic narrative based on the Quran and Tafseer. | |
| SPECIFIC REQUIREMENTS: | |
| 1. Include specific Quranic references (Surah name and verse numbers) for each event | |
| 2. Structure your response with clear sections: Introduction, Narrative, Lessons, and References | |
| 3. For each event mentioned, include the specific Quranic reference in parentheses | |
| 4. Create a dedicated "Quranic References" section listing all referenced verses | |
| 5. Format references as: (Surah Name Chapter:Verse) | |
| Response must be in: {language} | |
| """ | |
| # Combine with the original prompt | |
| full_prompt = PROPHETS_SYSTEM_PROMPT + "\n\n" + explicit_prompt + "\n\n" + prompt | |
| # Use chat history for context | |
| chat = model.start_chat(history=[]) | |
| response = chat.send_message(full_prompt, stream=True) | |
| for chunk in response: | |
| full_response += chunk.text | |
| message_placeholder.markdown(full_response + "▌") | |
| message_placeholder.markdown(full_response) | |
| st.session_state.prophet_messages.append({"role": "assistant", "content": full_response}) | |
| except Exception as e: | |
| error_message = f"Sorry, I encountered an error: {str(e)}" | |
| message_placeholder.markdown(error_message) | |
| st.session_state.prophet_messages.append({"role": "assistant", "content": error_message}) | |
| # Add a button to clear chat history | |
| if st.sidebar.button("Clear Conversation"): | |
| st.session_state.prophet_messages = [{"role": "assistant", "content": "Assalamu Alaikum! Select a prophet and story type to learn about their Quranic narrative."}] | |
| st.rerun() | |
| # Display disclaimer | |
| st.sidebar.divider() | |
| st.sidebar.caption( | |
| "This application provides information based on the Quran and authentic Tafseer. " | |
| "For matters of Islamic law and practice, please consult qualified scholars." | |
| ) | |
| # Additional information | |
| with st.expander("Important Notes About Quranic Stories"): | |
| st.markdown(""" | |
| - The stories of prophets in the Quran are not mere historical accounts but contain wisdom and lessons for all humanity | |
| - Each prophet's story emphasizes specific aspects of Tawheed (monotheism) and moral teachings | |
| - The Quran may mention different aspects of a prophet's story in different Surahs | |
| - We should avoid adding details from non-Islamic sources or folklore to these sacred narratives | |
| - The primary purpose of these stories is to strengthen faith and provide guidance | |
| """) | |
| with st.expander("References and Sources"): | |
| st.markdown(""" | |
| This application draws from: | |
| - The Holy Quran (with translations) | |
| - Tafseer Ibn Kathir | |
| - Tafseer Al-Qurtubi | |
| - Tafseer Al-Tabari | |
| - Other authenticated books of Tafseer | |
| All narratives are verified against these authentic sources to avoid any innovation or distortion. | |
| """) | |
| # Add custom CSS for better Urdu rendering and reference formatting | |
| st.markdown(""" | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu&display=swap'); | |
| .urdu-text { | |
| font-family: 'Noto Nastaliq Urdu', serif; | |
| font-size: 18px; | |
| line-height: 1.8; | |
| direction: rtl; | |
| text-align: right; | |
| } | |
| .references { | |
| background-color: #f8f9fa; | |
| border-left: 4px solid #2e86ab; | |
| padding: 15px; | |
| margin: 20px 0; | |
| border-radius: 5px; | |
| } | |
| .references h3 { | |
| color: #2e86ab; | |
| margin-top: 0; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) |