samiasohail25gmailcom commited on
Commit
6863dbe
·
verified ·
1 Parent(s): ffb8875

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -2,29 +2,43 @@ import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # Title
6
- st.set_page_config(page_title="Noor-e-Hidayat 🌙")
7
- st.title("🌙 Noor-e-Hidayat – Islamic AI Chatbot")
8
- st.markdown("Ask any question based on Qur’an. The model will respond with Qur’an-based Islamic guidance (no fatwas).")
9
 
10
- # Load Pretrained Quran QA 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
- # Input box
16
- user_input = st.text_input("📖 Ask your question:")
 
 
 
 
17
 
18
- # If user types something
19
  if user_input:
20
- with st.spinner("🔍 Thinking..."):
 
 
 
 
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
- st.markdown("### 📜 Answer:")
30
- st.write(response)
 
 
 
 
 
 
 
 
 
 
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"])