Chat_with_NDMO / app.py
MOHAMMED-N's picture
Create app.py
f352484 verified
raw
history blame
4.31 kB
import streamlit as st
import os
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# --- LANGCHAIN IMPORTS ---
from langchain_community.document_loaders import PyPDFLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.memory import ConversationBufferMemory
# 1) SET UP PAGE
st.title("💬 المحادثة التفاعلية - إدارة البيانات وحماية البيانات الشخصية")
local_file = "Policies001.pdf"
index_folder = "faiss_index"
# Inject custom CSS for right-to-left text
st.markdown(
"""
<style>
.rtl {
direction: rtl;
text-align: right;
}
</style>
""",
unsafe_allow_html=True
)
# 2) LOAD OR BUILD VECTORSTORE
embeddings = HuggingFaceEmbeddings(
model_name="CAMeL-Lab/bert-base-arabic-camelbert-mix",
model_kwargs={"trust_remote_code": True}
)
if os.path.exists(index_folder):
vectorstore = FAISS.load_local(index_folder, embeddings, allow_dangerous_deserialization=True)
else:
loader = PyPDFLoader(local_file)
documents = loader.load()
text_splitter = SemanticChunker(
embeddings=embeddings,
breakpoint_threshold_type='percentile',
breakpoint_threshold_amount=90
)
chunked_docs = text_splitter.split_documents(documents)
vectorstore = FAISS.from_documents(chunked_docs, embeddings)
vectorstore.save_local(index_folder)
# 3) CREATE RETRIEVER
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 5})
# 4) SET UP "COMMAND-R7B-ARABIC" AS LLM
# Authenticate and load the model
model_name = "CohereForAI/c4ai-command-r7b-arabic-02-2025" # Replace with the actual Hugging Face model ID
# Set Hugging Face token securely
hf_token = os.getenv("HF_TOKEN") # Ensure you set your token as an environment variable in Hugging Face Spaces
if hf_token is None:
st.error("Hugging Face token not found. Please set the 'HF_TOKEN' environment variable.")
st.stop()
# Load tokenizer and model using the token
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
# Hugging Face pipeline for text generation
qa_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
# Memory object to store conversation
memory = ConversationBufferMemory(
memory_key="chat_history", # key used internally by the chain
return_messages=True # ensures we get the entire message history
)
# 5) MANAGE SESSION STATE FOR UI CHAT
if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": "👋 مرحبًا! اسألني أي شيء عن إدارة البيانات وحماية البيانات الشخصية!"}
]
# Display existing messages in chat format
for msg in st.session_state["messages"]:
with st.chat_message(msg["role"]):
# Apply the "rtl" class to style Arabic text correctly
st.markdown(f'<div class="rtl">{msg["content"]}</div>', unsafe_allow_html=True)
# 6) CHAT INPUT
user_input = st.chat_input("اكتب سؤالك هنا")
# 7) PROCESS NEW USER MESSAGE
if user_input:
# a) Display user message in UI
st.session_state["messages"].append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(f'<div class="rtl">{user_input}</div>', unsafe_allow_html=True)
# b) Run pipeline to generate a response
# Combine retriever results and user input for context-aware answering
retrieved_docs = retriever.get_relevant_documents(user_input)
context = "\n".join([doc.page_content for doc in retrieved_docs])
full_input = f"السياق:\n{context}\n\nالسؤال:\n{user_input}"
# Generate answer using the pipeline
response = qa_pipeline(full_input, max_length=500, num_return_sequences=1)[0]["generated_text"]
# c) Display assistant response
st.session_state["messages"].append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(f'<div class="rtl">{response}</div>', unsafe_allow_html=True)