Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,43 @@ import streamlit as st
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
st.set_page_config(page_title="Noor-e-Hidayat 🌙")
|
7 |
-
st.
|
8 |
-
st.markdown("Ask
|
9 |
|
10 |
-
# Load
|
11 |
model_id = "llm-soda/quran-qa-phi-2"
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
13 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# If user types something
|
19 |
if user_input:
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
prompt = f"Answer the following Islamic question with Qur’an-based reasoning and reference:\nQuestion: {user_input}\nAnswer:"
|
22 |
-
|
23 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to("cuda" if torch.cuda.is_available() else "cpu")
|
24 |
outputs = model.generate(**inputs, max_new_tokens=300)
|
25 |
-
|
26 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
27 |
response = response.replace(prompt, "").strip()
|
28 |
|
29 |
-
|
30 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
+
# Setup
|
6 |
+
st.set_page_config(page_title="Noor-e-Hidayat 🌙", layout="centered")
|
7 |
+
st.markdown("<h1 style='text-align: center;'>🌙 Noor-e-Hidayat – Islamic Chatbot</h1>", unsafe_allow_html=True)
|
8 |
+
st.markdown("Ask anything based on the Qur’an. This assistant replies gently, spiritually, and with reference.")
|
9 |
|
10 |
+
# Load model
|
11 |
model_id = "llm-soda/quran-qa-phi-2"
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
13 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
14 |
|
15 |
+
# Chat history in session
|
16 |
+
if "messages" not in st.session_state:
|
17 |
+
st.session_state.messages = []
|
18 |
+
|
19 |
+
# Chat input box
|
20 |
+
user_input = st.chat_input("Type your question about Islam...")
|
21 |
|
|
|
22 |
if user_input:
|
23 |
+
# Save user message
|
24 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
25 |
+
|
26 |
+
# Generate bot reply
|
27 |
+
with st.spinner("Answering with Qur’an wisdom..."):
|
28 |
prompt = f"Answer the following Islamic question with Qur’an-based reasoning and reference:\nQuestion: {user_input}\nAnswer:"
|
|
|
29 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to("cuda" if torch.cuda.is_available() else "cpu")
|
30 |
outputs = model.generate(**inputs, max_new_tokens=300)
|
|
|
31 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
32 |
response = response.replace(prompt, "").strip()
|
33 |
|
34 |
+
# Save bot message
|
35 |
+
st.session_state.messages.append({"role": "bot", "content": response})
|
36 |
+
|
37 |
+
# Display chat messages like ChatGPT
|
38 |
+
for msg in st.session_state.messages:
|
39 |
+
if msg["role"] == "user":
|
40 |
+
with st.chat_message("user"):
|
41 |
+
st.markdown(msg["content"])
|
42 |
+
else:
|
43 |
+
with st.chat_message("assistant"):
|
44 |
+
st.markdown(msg["content"])
|