Spaces:
Configuration error
Configuration error
Delete rag_pipeline.py
Browse files- rag_pipeline.py +0 -43
rag_pipeline.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
from sentence_transformers import SentenceTransformer
|
2 |
-
from transformers import pipeline
|
3 |
-
import faiss
|
4 |
-
import numpy as np
|
5 |
-
from config import MODEL_CONFIG
|
6 |
-
|
7 |
-
class ArabicRAGSystem:
|
8 |
-
def __init__(self):
|
9 |
-
self.embedder = SentenceTransformer(MODEL_CONFIG["embedding_model"])
|
10 |
-
self.llm = pipeline("text-generation", model=MODEL_CONFIG["llm"])
|
11 |
-
self.index = None
|
12 |
-
self.documents = []
|
13 |
-
|
14 |
-
def build_index(self, chunks: List[str]):
|
15 |
-
"""Create FAISS index from document chunks"""
|
16 |
-
self.documents = chunks
|
17 |
-
embeddings = self.embedder.encode(chunks, show_progress_bar=True)
|
18 |
-
self.index = faiss.IndexFlatIP(embeddings.shape[1])
|
19 |
-
self.index.add(embeddings)
|
20 |
-
|
21 |
-
def retrieve(self, query: str, k: int = 3) -> List[str]:
|
22 |
-
"""Retrieve relevant document chunks"""
|
23 |
-
query_embedding = self.embedder.encode([query])
|
24 |
-
distances, indices = self.index.search(query_embedding, k)
|
25 |
-
return [self.documents[i] for i in indices[0]]
|
26 |
-
|
27 |
-
def generate_answer(self, question: str, context: List[str]) -> str:
|
28 |
-
"""Generate answer using LLM with retrieved context"""
|
29 |
-
prompt = f"""استخدم المعلومات التالية للإجابة على السؤال:
|
30 |
-
|
31 |
-
السياق:
|
32 |
-
{'\n'.join(context)}
|
33 |
-
|
34 |
-
السؤال: {question}
|
35 |
-
الإجابة:"""
|
36 |
-
|
37 |
-
result = self.llm(
|
38 |
-
prompt,
|
39 |
-
max_new_tokens=256,
|
40 |
-
temperature=0.7,
|
41 |
-
do_sample=True
|
42 |
-
)
|
43 |
-
return result[0]["generated_text"].replace(prompt, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|