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( """ """, 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'