Mohamed Dyab commited on
Commit
5dbae09
ยท
1 Parent(s): 7a28136
Files changed (3) hide show
  1. README.md +16 -10
  2. app.py +10 -10
  3. requirements.txt +1 -1
README.md CHANGED
@@ -1,16 +1,22 @@
1
  ---
2
- title: Arabic Rag Chat
3
- emoji: ๐Ÿ’ฌ
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.42.0
8
  app_file: app.py
9
  pinned: false
10
- hf_oauth: true
11
- hf_oauth_scopes:
12
- - inference-api
13
- short_description: Arabic RAG Chat
14
  ---
15
 
16
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Arabic RAG Chat
3
+ emoji: ๐Ÿ“š
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Arabic PDF Q&A with RAG
 
 
 
11
  ---
12
 
13
+ # Arabic RAG Chat
14
+
15
+ Upload Arabic PDF documents and ask questions. Powered by:
16
+ - **Embeddings**: Multilingual-E5-Small (GPU-accelerated via ZeroGPU)
17
+ - **LLM**: Cohere Command R
18
+ - **Vector Store**: FAISS (CPU - fast enough for RAG workloads)
19
+
20
+ ## Setup
21
+
22
+ Add your `COHERE_API_KEY` in Space Settings โ†’ Secrets.
app.py CHANGED
@@ -5,13 +5,13 @@ import spaces
5
  from langchain_community.document_loaders import PyPDFLoader
6
  from langchain_text_splitters import RecursiveCharacterTextSplitter
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
- from langchain_community.vectorstores import Chroma
9
  from langchain_cohere import ChatCohere
10
  from langchain_classic.chains import RetrievalQA
11
 
12
  # 1. SETUP: DEFINING THE MODELS
13
- # We use a multilingual embedding model specifically tuned for retrieval
14
- embedding_model_name = "intfloat/multilingual-e5-large"
15
 
16
  # Detect device - use CUDA if available, otherwise CPU
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -43,8 +43,8 @@ def process_and_chat(file, query):
43
  if not api_key:
44
  return "**Error:** COHERE_API_KEY environment variable is not set."
45
 
46
- # Initialize LLM (Cohere Command R+ via API)
47
- llm = ChatCohere(model="command-a-03-2025", temperature=0.3, cohere_api_key=api_key)
48
 
49
  # 2. LOAD & PROCESS DOCUMENT
50
  # Gradio 6.x returns file path as string directly
@@ -60,10 +60,10 @@ def process_and_chat(file, query):
60
  )
61
  texts = text_splitter.split_documents(documents)
62
 
63
- # 3. CREATE VECTOR STORE (In-Memory)
64
  # This turns your Arabic text into searchable vectors
65
- db = Chroma.from_documents(texts, get_embeddings())
66
- retriever = db.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 chunks
67
 
68
  # 4. RAG CHAIN
69
  qa_chain = RetrievalQA.from_chain_type(
@@ -93,8 +93,8 @@ iface = gr.Interface(
93
  gr.Textbox(label="Ask a question in Arabic", placeholder="ู…ุง ู‡ูŠ ุงู„ู†ู‚ุงุท ุงู„ุฑุฆูŠุณูŠุฉ ููŠ ู‡ุฐุง ุงู„ู…ุณุชู†ุฏุŸ")
94
  ],
95
  outputs=gr.Markdown(),
96
- title="Arabic RAG (Command R+)",
97
- description="Upload a PDF and ask questions. Powered by Cohere Command R+ and Multilingual-E5 embeddings."
98
  )
99
 
100
  iface.launch(share=True)
 
5
  from langchain_community.document_loaders import PyPDFLoader
6
  from langchain_text_splitters import RecursiveCharacterTextSplitter
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
+ from langchain_community.vectorstores import FAISS
9
  from langchain_cohere import ChatCohere
10
  from langchain_classic.chains import RetrievalQA
11
 
12
  # 1. SETUP: DEFINING THE MODELS
13
+ # We use a smaller/faster multilingual embedding model for retrieval
14
+ embedding_model_name = "intfloat/multilingual-e5-small"
15
 
16
  # Detect device - use CUDA if available, otherwise CPU
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
43
  if not api_key:
44
  return "**Error:** COHERE_API_KEY environment variable is not set."
45
 
46
+ # Initialize LLM (Cohere Command R - faster model)
47
+ llm = ChatCohere(model="command-r7b-12-2024", temperature=0.3, cohere_api_key=api_key)
48
 
49
  # 2. LOAD & PROCESS DOCUMENT
50
  # Gradio 6.x returns file path as string directly
 
60
  )
61
  texts = text_splitter.split_documents(documents)
62
 
63
+ # 3. CREATE VECTOR STORE (In-Memory FAISS - faster than Chroma)
64
  # This turns your Arabic text into searchable vectors
65
+ db = FAISS.from_documents(texts, get_embeddings())
66
+ retriever = db.as_retriever(search_kwargs={"k": 3}) # Retrieve top 3 chunks (faster)
67
 
68
  # 4. RAG CHAIN
69
  qa_chain = RetrievalQA.from_chain_type(
 
93
  gr.Textbox(label="Ask a question in Arabic", placeholder="ู…ุง ู‡ูŠ ุงู„ู†ู‚ุงุท ุงู„ุฑุฆูŠุณูŠุฉ ููŠ ู‡ุฐุง ุงู„ู…ุณุชู†ุฏุŸ")
94
  ],
95
  outputs=gr.Markdown(),
96
+ title="Arabic RAG (Command R)",
97
+ description="Upload a PDF and ask questions. Powered by Cohere Command R and Multilingual-E5-Small embeddings."
98
  )
99
 
100
  iface.launch(share=True)
requirements.txt CHANGED
@@ -6,6 +6,6 @@ langchain-community
6
  langchain-text-splitters
7
  langchain-cohere
8
  langchain-huggingface
9
- chromadb
10
  pypdf
11
  sentence-transformers
 
6
  langchain-text-splitters
7
  langchain-cohere
8
  langchain-huggingface
9
+ faiss-cpu
10
  pypdf
11
  sentence-transformers