Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from sentence_transformers import SentenceTransformer | |
import faiss | |
import json | |
import random | |
import os | |
# ----------------- DATA SECTION ----------------- | |
# Quran + Hadith sample data (You can expand later) | |
quran_data = [ | |
{ | |
"source": "Surah Al-Baqarah, Ayah 2", | |
"text": "This is the Book about which there is no doubt, a guidance for those conscious of Allah." | |
}, | |
{ | |
"source": "Surah Al-Ikhlas, Ayah 1", | |
"text": "Say, 'He is Allah, [who is] One.'" | |
} | |
] | |
hadith_data = [ | |
{ | |
"source": "Sahih Bukhari, Book 2, Hadith 13", | |
"text": "None of you will have faith till he wishes for his brother what he likes for himself." | |
}, | |
{ | |
"source": "Sahih Muslim, Book 1, Hadith 1", | |
"text": "Actions are judged by intentions." | |
} | |
] | |
# ----------------- EMBEDDING + FAISS ----------------- | |
def build_index(passages): | |
model = SentenceTransformer('all-MiniLM-L6-v2') | |
texts = [p['text'] for p in passages] | |
embeddings = model.encode(texts) | |
index = faiss.IndexFlatL2(embeddings.shape[1]) | |
index.add(embeddings) | |
return model, index, passages | |
model, index, passages = build_index(quran_data + hadith_data) | |
def retrieve_passages(query, k=3): | |
query_vec = model.encode([query]) | |
scores, idxs = index.search(query_vec, k) | |
return [passages[i] for i in idxs[0]] | |
# ----------------- TRANSLATION ----------------- | |
def load_translators(): | |
trans_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur") | |
trans_ar = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar") | |
return trans_ur, trans_ar | |
translator_ur, translator_ar = load_translators() | |
def translate(text, lang): | |
if lang == "Urdu": | |
return translator_ur(text)[0]['translation_text'] | |
elif lang == "Arabic": | |
return translator_ar(text)[0]['translation_text'] | |
return text | |
# ----------------- DAILY VERSES ----------------- | |
def get_random_ayah(): | |
return random.choice(quran_data) | |
def get_random_hadith(): | |
return random.choice(hadith_data) | |
# ----------------- STREAMLIT UI ----------------- | |
st.set_page_config(page_title="Noor-e-Hidayat", layout="centered") | |
st.title("ποΈ Noor-e-Hidayat β Your Islamic AI Assistant") | |
lang = st.selectbox("π Choose Language", ["English", "Urdu", "Arabic"]) | |
st.markdown("---") | |
st.subheader("π Ask Noor-e-Hidayat") | |
query = st.text_input("Type your question related to Qurβan, Hadith, or Islamic guidance...") | |
if query: | |
results = retrieve_passages(query) | |
for r in results: | |
st.markdown(f"π **{r['source']}**") | |
st.write(translate(r['text'], lang)) | |
st.markdown("---") | |
st.subheader("π Ayah of the Day") | |
ayah = get_random_ayah() | |
st.info(f"**{ayah['source']}**\n\n{translate(ayah['text'], lang)}") | |
st.subheader("π Hadith of the Day") | |
hadith = get_random_hadith() | |
st.success(f"**{hadith['source']}**\n\n{translate(hadith['text'], lang)}") | |
st.markdown("---") | |
st.caption("βοΈ Powered by Transformers, Sentence-BERT, and FAISS β’ Built with β€οΈ using Streamlit") | |