Spaces:
Sleeping
Sleeping
Create 3_Ask_Quran.py
Browse files- pages/3_Ask_Quran.py +44 -0
pages/3_Ask_Quran.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
# Initialize OpenAI
|
| 5 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 6 |
+
|
| 7 |
+
st.title("📚 Shaafee - Ask the Qur’an")
|
| 8 |
+
|
| 9 |
+
st.write(
|
| 10 |
+
"Type any topic or question — I’ll find you a relevant Ayah, its translation, and a simple tafsir."
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# User input
|
| 14 |
+
topic = st.text_input(
|
| 15 |
+
"What do you want to ask?",
|
| 16 |
+
placeholder="Example: What does the Qur’an say about patience?"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if st.button("Get Qur’anic Answer"):
|
| 20 |
+
if topic.strip():
|
| 21 |
+
prompt = f"""
|
| 22 |
+
Provide a Qur'anic Ayah in Arabic related to: '{topic}'.
|
| 23 |
+
Include:
|
| 24 |
+
1) The Ayah in Arabic
|
| 25 |
+
2) Its English translation
|
| 26 |
+
3) A short, simple tafsir.
|
| 27 |
+
Be authentic and concise.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = client.chat.completions.create(
|
| 32 |
+
model="gpt-4o-mini",
|
| 33 |
+
messages=[{"role": "user", "content": prompt}]
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
output = response.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
st.subheader("🕋 Qur’anic Answer")
|
| 39 |
+
st.write(output)
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"❌ API Error: {e}")
|
| 43 |
+
else:
|
| 44 |
+
st.warning("Please enter a topic or question.")
|