Spaces:
Sleeping
Sleeping
Mohamed Dyab commited on
Commit ยท
5dbae09
1
Parent(s): 7a28136
faster
Browse files- README.md +16 -10
- app.py +10 -10
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -1,16 +1,22 @@
|
|
| 1 |
---
|
| 2 |
-
title: Arabic
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
-
hf_oauth_scopes:
|
| 12 |
-
- inference-api
|
| 13 |
-
short_description: Arabic RAG Chat
|
| 14 |
---
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 14 |
-
embedding_model_name = "intfloat/multilingual-e5-
|
| 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
|
| 47 |
-
llm = ChatCohere(model="command-
|
| 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 =
|
| 66 |
-
retriever = db.as_retriever(search_kwargs={"k":
|
| 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
|
| 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 |
-
|
| 10 |
pypdf
|
| 11 |
sentence-transformers
|
|
|
|
| 6 |
langchain-text-splitters
|
| 7 |
langchain-cohere
|
| 8 |
langchain-huggingface
|
| 9 |
+
faiss-cpu
|
| 10 |
pypdf
|
| 11 |
sentence-transformers
|