Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314151 unmodified line1718192021222324252627285 unmodified lines3435363738394041424344455 unmodified lines515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262274 unmodified lines2322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623632 unmodified lines366367368369370371372373374375376377378import timeimport fitzimport numpy as npimport pickleimport osimport dillimport loggingimport asyncioimport networkx as nxfrom mistralai import Mistralfrom annoy import AnnoyIndexfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityfrom rank_bm25 import BM25Okapifrom gensim.models import Word2Vec1 unmodified lineimport gradio as grfrom openai import OpenAIlogger = logging.getLogger(__name__)logging.basicConfig(level=logging.INFO)api_key = os.getenv("MISTRAL_API_KEY")client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))#openai.api_key = os.getenv("OPENAI_API_KEY")def get_text_embedding_with_rate_limit(text_list, initial_delay=2, max_retries=10, max_delay=60):embeddings = []5 unmodified linesif len(text) > 8192:logging.warning("Text chunk exceeds the token limit. Truncating the text.")text = text[:8192]response = client.embeddings.create(model="text-embedding-3-small",input=[text])embeddings.append(response.data[0].embedding) # ✅ dot notationtime.sleep(delay)breakexcept Exception as e:5 unmodified lineslogging.error("Max retries reached. Skipping this chunk.")return embeddingsdef store_embeddings_in_vector_db(pdf_path: str,vector_db_path: str,annoy_index_path: str,chunk_size: int = 2048,overlap: int = 200,num_trees: int = 10):doc = fitz.open(pdf_path)all_embeddings = []all_texts = []total_pages = doc.page_countlogging.info(f"Processing PDF: {pdf_path} with {total_pages} pages.")for page_num in range(total_pages):page = doc.load_page(page_num)text = page.get_text()if text.strip():chunks = split_text_into_chunks(text, chunk_size, overlap)embeddings = get_text_embedding_with_rate_limit(chunks)all_embeddings.extend(embeddings)all_texts.extend(chunks)logging.info(f"Processed page {page_num + 1}/{total_pages}, extracted {len(chunks)} chunks.")else:logging.warning(f"No text found on page {page_num + 1}.")embeddings_np = np.array(all_embeddings).astype('float32')with open(vector_db_path, "wb") as f:dill.dump({'embeddings': embeddings_np, 'texts': all_texts}, f)logging.info(f"Stored embeddings and texts to {vector_db_path}.")if os.path.exists(annoy_index_path):os.remove(annoy_index_path)logging.info(f"Existing Annoy index at {annoy_index_path} removed.")embedding_dim = embeddings_np.shape[1]annoy_index = AnnoyIndex(embedding_dim, 'angular')for i, embedding in enumerate(embeddings_np):annoy_index.add_item(i, embedding)annoy_index.build(num_trees)annoy_index.save(annoy_index_path)logging.info(f"Annoy index built with {len(all_embeddings)} items and saved to {annoy_index_path}.")def split_text_into_chunks(text: str, chunk_size: int = 2048, overlap: int = 200) -> List[str]:tokens = text.split()chunks = []start = 0while start < len(tokens):end = start + chunk_sizechunk = " ".join(tokens[start:end])chunks.append(chunk)start += chunk_size - overlapreturn chunksclass MistralRAGChatbot:def __init__(self, vector_db_path: str, annoy_index_path: str):self.embeddings, self.texts = self.load_vector_db(vector_db_path)self.annoy_index = self.load_annoy_index(annoy_index_path, self.embeddings.shape[1])self.bm25 = BM25Okapi([text.split() for text in self.texts])self.word2vec_model = self.train_word2vec(self.texts)self.reranking_methods = {'advanced_fusion': self.advanced_fusion_retrieval}logging.info("MistralRAGChatbot initialized successfully.")def load_vector_db(self, vector_db_path: str) -> Tuple[np.ndarray, List[str]]:with open(vector_db_path, "rb") as f:data = dill.load(f)embeddings = np.array(data['embeddings'], dtype='float32')texts = data['texts']logging.info(f"Loaded vector database from {vector_db_path} with {len(texts)} entries.")return embeddings, textsdef load_annoy_index(self, annoy_index_path: str, embedding_dim: int) -> AnnoyIndex:if not os.path.exists(annoy_index_path):raise FileNotFoundError(f"Annoy index file {annoy_index_path} not found.")annoy_index = AnnoyIndex(embedding_dim, 'angular')annoy_index.load(annoy_index_path)logging.info(f"Loaded Annoy index from {annoy_index_path}.")return annoy_indexdef train_word2vec(self, texts: List[str]) -> Word2Vec:tokenized_texts = [text.split() for text in texts]model = Word2Vec(sentences=tokenized_texts, vector_size=100, window=5, min_count=1, workers=4)logging.info("Word2Vec model trained.")return modeldef get_text_embedding(self, text: str, model: str = "text-embedding-3-small") -> np.ndarray:try:response = client.embeddings.create(model="text-embedding-3-small",input=[text])return np.array(response.data[0].embedding, dtype=np.float32) # ✅ dot notationexcept Exception as e:logging.error(f"Error fetching OpenAI embedding: {e}")return np.zeros((1536,), dtype=np.float32)def advanced_fusion_retrieval(self, user_query: str, docs: List[dict]) -> List[dict]:query_embedding = self.create_embeddings([user_query])[0]vector_scores = {doc['index']: doc['score'] for doc in docs if doc['method'] == 'annoy'}bm25_scores = {doc['index']: doc['score'] for doc in docs if doc['method'] == 'bm25'}sim_graph = nx.Graph()sim_matrix = cosine_similarity(self.embeddings)for i in range(len(self.embeddings)):for j in range(i + 1, len(self.embeddings)):if sim_matrix[i, j] > 0.5:sim_graph.add_edge(i, j, weight=sim_matrix[i, j])pagerank_scores = np.array(list(nx.pagerank(sim_graph, weight='weight').values()))combined_scores = {}for doc in docs:idx = doc['index']combined_scores[idx] = (0.5 * vector_scores.get(idx, 0) +0.3 * bm25_scores.get(idx, 0) +0.2 * pagerank_scores[idx] if idx < len(pagerank_scores) else 0)min_score = min(combined_scores.values())max_score = max(combined_scores.values())if min_score == max_score:normalized_scores = {idx: 0.5 for idx in combined_scores}else:normalized_scores = {idx: (score - min_score) / (max_score - min_score) for idx, score in combined_scores.items()}sorted_indices = sorted(combined_scores, key=combined_scores.get, reverse=True)return [{'text': self.texts[i], 'method': 'advanced_fusion', 'score': normalized_scores[i], 'index': i} for i in sorted_indices[:5]]def create_embeddings(self, text_list: List[str]) -> np.ndarray:expected_dim = 1536 # match OpenAI embedding sizeembeddings = []for text in text_list:word_vectors = [self.word2vec_model.wv[token] for token in text.split() if token in self.word2vec_model.wv]avg_embedding = np.mean(word_vectors, axis=0, dtype=np.float32) if word_vectors else np.zeros(self.word2vec_model.vector_size, dtype=np.float32)if avg_embedding.shape[0] < expected_dim:avg_embedding = np.pad(avg_embedding, (0, expected_dim - avg_embedding.shape[0]), 'constant')elif avg_embedding.shape[0] > expected_dim:avg_embedding = avg_embedding[:expected_dim]embeddings.append(avg_embedding)return np.array(embeddings, dtype=np.float32)async def generate_response_with_rag(self,user_query: str,model: str = "gpt-4o-mini",top_k: int = 10,response_style: str = "Detailed",selected_retrieval_methods: Optional[List[str]] = None,selected_reranking_methods: Optional[List[str]] = None) -> Tuple[str, List[str], List[dict]]:if not selected_retrieval_methods:selected_retrieval_methods = ['annoy', 'bm25']if not selected_reranking_methods:selected_reranking_methods = ['advanced_fusion']query_embedding = self.get_text_embedding(user_query)retrieved_docs = self.retrieve_documents(user_query, query_embedding, top_k, selected_retrieval_methods)reranked_docs = self.rerank_documents(user_query, retrieved_docs, selected_reranking_methods)context = "\n\n".join([doc['text'] for doc in reranked_docs[:5]])prompt = self.build_prompt(context, user_query, response_style)try:response = ""stream = client.chat.completions.create(4 unmodified linesfor chunk in stream:if chunk.choices and chunk.choices[0].delta.content:response += chunk.choices[0].delta.contentlogging.info("Response generated successfully.")except Exception as e:logging.error(f"Error generating response: {e}")response = "An error occurred while generating the response."return response, [doc['text'] for doc in reranked_docs[:5]], reranked_docs[:5]def retrieve_documents(self,user_query: str,query_embedding: np.ndarray,top_k: int,selected_methods: List[str]) -> List[dict]:all_docs = []for method in selected_methods:indices, scores = getattr(self, f"retrieve_with_{method}")(user_query, query_embedding, top_k)for idx, score in zip(indices, scores):all_docs.append({'text': self.texts[idx],'method': method,'score': score,'index': idx})return all_docsdef retrieve_with_annoy(self, user_query: str, query_embedding: np.ndarray, top_k: int) -> Tuple[List[int], List[float]]:n_results = min(top_k, len(self.texts))indices, distances = self.annoy_index.get_nns_by_vector(query_embedding, n_results, include_distances=True)scores = [1.0 - (dist / max(distances)) for dist in distances] if distances else []logging.debug(f"Annoy retrieval returned {len(indices)} documents.")return indices, scoresdef retrieve_with_bm25(self, user_query: str, query_embedding: np.ndarray, top_k: int) -> Tuple[List[int], List[float]]:tokenized_query = user_query.split()scores = self.bm25.get_scores(tokenized_query)indices = np.argsort(-scores)[:top_k]logging.debug(f"BM25 retrieval returned {len(indices)} documents.")return indices, scores[indices].tolist()def rerank_documents(self,user_query: str,retrieved_docs: List[dict],selected_methods: List[str]) -> List[dict]:reranked_docs = retrieved_docsfor method in selected_methods:if method == 'advanced_fusion':reranked_docs = self.advanced_fusion_retrieval(user_query, reranked_docs)else:reranked_docs = self.reranking_methods[method](user_query, reranked_docs)return reranked_docsdef build_prompt(self, context: str, user_query: str, response_style: str) -> str:styles = {"detailed": "Provide a comprehensive and detailed answer based on the provided context.","concise": "Provide a brief and concise answer based on the provided context.","creative": "Provide a creative and engaging answer based on the provided context.","technical": "Provide a technical and in-depth answer based on the provided context."}style_instruction = styles.get(response_style.lower(), styles["detailed"])if not context or not self.is_context_relevant(context, user_query):prompt = f"""You are an intelligent assistant.User Question:{user_query}Instruction:The document database does not contain relevant information to answer the question. Please inform the user that no relevant documents were found and refrain from generating an imaginative or unrelated response."""else:prompt = f"""You are an intelligent assistant.Context:{context}User Question:{user_query}Instruction:{style_instruction}"""logging.debug("Prompt constructed for response generation.")return promptdef is_context_relevant(self, context: str, user_query: str) -> bool:context_lower = context.lower()user_query_lower = user_query.lower()query_terms = set(user_query_lower.split())context_terms = set(context_lower.split())common_terms = query_terms.intersection(context_terms)return len(common_terms) > len(query_terms) * 0.2def create_vector_db_and_annoy_index(pdf_path, vector_db_path, annoy_index_path):store_embeddings_in_vector_db(pdf_path, vector_db_path, annoy_index_path)print("Vector database and Annoy index creation completed.")def chatbot_interface(file, user_query, response_style):vector_db_path = "vector_db.pkl"annoy_index_path = "vector_index.ann"chunk_size = 2048overlap = 200store_embeddings_in_vector_db(file.name, vector_db_path, annoy_index_path, chunk_size, overlap)chatbot = MistralRAGChatbot(vector_db_path, annoy_index_path)selected_retrieval_methods_list = ['annoy', 'bm25']selected_reranking_methods_list = ["advanced_fusion"]response, retrieved_docs, source_info = asyncio.run(chatbot.generate_response_with_rag(user_query=user_query,response_style=response_style,selected_retrieval_methods=selected_retrieval_methods_list,selected_reranking_methods=selected_reranking_methods_list))formatted_response = f"Response:\n{response}\n\n"formatted_response += "Retrieved and Reranked Documents:\n"for idx, doc_info in enumerate(source_info, start=1):formatted_response += f"\nDocument {idx}:\n"formatted_response += f"Content Preview: {doc_info['text'][:200]}...\n"formatted_response += f"Retrieval Method: {doc_info['method']}\n"if 'score' in doc_info:formatted_response += f"Precision Score: {doc_info['score']:.4f}\n"return formatted_responseiface = gr.Blocks(theme="Rabbitt-AI/ChanceRAG")with iface:2 unmodified linesfn=chatbot_interface,theme="Rabbitt-AI/ChanceRAG",inputs=[gr.File(label="Upload a File"),gr.Textbox(lines=5, label="User Query"),gr.Dropdown(["Detailed", "Concise", "Creative", "Technical"], label="Response Style"),],outputs= gr.Textbox(label="ChanceRAG Response"),)iface.launch(share=True)1234567891011121 unmodified line14151617181920212223242526275 unmodified lines33343536373839404142435 unmodified lines4950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561574 unmodified lines1621631641651661671681691701711721731741752 unmodified lines178179180181182183184185186187import timeimport fitzimport numpy as npimport dillimport osimport loggingimport asyncioimport networkx as nxfrom annoy import AnnoyIndexfrom sklearn.metrics.pairwise import cosine_similarityfrom rank_bm25 import BM25Okapifrom gensim.models import Word2Vec1 unmodified lineimport gradio as grfrom openai import OpenAIlogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))PDF_PATH = "input.pdf"VECTOR_DB_PATH = "vector_db.pkl"ANNOY_INDEX_PATH = "vector_index.ann"def get_text_embedding_with_rate_limit(text_list, initial_delay=2, max_retries=10, max_delay=60):embeddings = []5 unmodified linesif len(text) > 8192:logging.warning("Text chunk exceeds the token limit. Truncating the text.")text = text[:8192]response = client.embeddings.create(model="text-embedding-3-small",input=[text])embeddings.append(response.data[0].embedding)time.sleep(delay)breakexcept Exception as e:5 unmodified lineslogging.error("Max retries reached. Skipping this chunk.")return embeddingsdef split_text_into_chunks(text: str, chunk_size: int = 2048, overlap: int = 200) -> List[str]:tokens = text.split()chunks = []start = 0while start < len(tokens):end = start + chunk_sizechunk = " ".join(tokens[start:end])chunks.append(chunk)start += chunk_size - overlapreturn chunksdef store_embeddings_in_vector_db(pdf_path, vector_db_path, annoy_index_path, chunk_size=2048, overlap=200, num_trees=10):doc = fitz.open(pdf_path)all_embeddings = []all_texts = []for page_num in range(doc.page_count):text = doc.load_page(page_num).get_text()if text.strip():chunks = split_text_into_chunks(text, chunk_size, overlap)embeddings = get_text_embedding_with_rate_limit(chunks)all_embeddings.extend(embeddings)all_texts.extend(chunks)embeddings_np = np.array(all_embeddings).astype('float32')with open(vector_db_path, "wb") as f:dill.dump({'embeddings': embeddings_np, 'texts': all_texts}, f)if os.path.exists(annoy_index_path):os.remove(annoy_index_path)embedding_dim = embeddings_np.shape[1]annoy_index = AnnoyIndex(embedding_dim, 'angular')for i, embedding in enumerate(embeddings_np):annoy_index.add_item(i, embedding)annoy_index.build(num_trees)annoy_index.save(annoy_index_path)if not os.path.exists(VECTOR_DB_PATH) or not os.path.exists(ANNOY_INDEX_PATH):store_embeddings_in_vector_db(PDF_PATH, VECTOR_DB_PATH, ANNOY_INDEX_PATH)class MistralRAGChatbot:def __init__(self, vector_db_path: str, annoy_index_path: str):with open(vector_db_path, "rb") as f:data = dill.load(f)self.embeddings = np.array(data['embeddings'], dtype='float32')self.texts = data['texts']self.annoy_index = AnnoyIndex(self.embeddings.shape[1], 'angular')self.annoy_index.load(annoy_index_path)self.bm25 = BM25Okapi([text.split() for text in self.texts])self.word2vec_model = Word2Vec([text.split() for text in self.texts], vector_size=100, window=5, min_count=1, workers=4)def get_text_embedding(self, text: str) -> np.ndarray:try:response = client.embeddings.create(model="text-embedding-3-small",input=[text])return np.array(response.data[0].embedding, dtype=np.float32)except Exception as e:logging.error(f"Error fetching embedding: {e}")return np.zeros((1536,), dtype=np.float32)def retrieve_documents(self, query: str, embedding: np.ndarray, top_k=10):indices, distances = self.annoy_index.get_nns_by_vector(embedding, top_k, include_distances=True)bm25_scores = self.bm25.get_scores(query.split())combined_docs = []for idx in indices:combined_docs.append({'text': self.texts[idx],'method': 'hybrid','score': float(bm25_scores[idx]),'index': idx})return combined_docsdef rerank_documents(self, query: str, docs: List[dict]) -> List[dict]:query_embedding = self.get_text_embedding(query)vector_scores = {doc['index']: doc['score'] for doc in docs}sim_graph = nx.Graph()sim_matrix = cosine_similarity(self.embeddings)for i in range(len(self.embeddings)):for j in range(i + 1, len(self.embeddings)):if sim_matrix[i, j] > 0.5:sim_graph.add_edge(i, j, weight=sim_matrix[i, j])pagerank_scores = np.array(list(nx.pagerank(sim_graph, weight='weight').values()))for doc in docs:idx = doc['index']doc['score'] = 0.7 * vector_scores.get(idx, 0) + 0.3 * pagerank_scores[idx]return sorted(docs, key=lambda x: x['score'], reverse=True)[:5]def build_prompt(self, context: str, query: str, style: str) -> str:styles = {"detailed": "Provide a detailed answer.","concise": "Provide a concise answer.","creative": "Be creative in your response.","technical": "Provide a technically sound answer."}instruction = styles.get(style.lower(), styles["detailed"])return f"""You are a helpful assistant.\nContext:\n{context}\nQuestion:\n{query}\nInstruction:\n{instruction}"""def generate_response(self, query: str, style: str) -> str:query_embedding = self.get_text_embedding(query)docs = self.retrieve_documents(query, query_embedding)reranked_docs = self.rerank_documents(query, docs)context = "\n\n".join([doc['text'] for doc in reranked_docs])prompt = self.build_prompt(context, query, style)try:response = ""stream = client.chat.completions.create(4 unmodified linesfor chunk in stream:if chunk.choices and chunk.choices[0].delta.content:response += chunk.choices[0].delta.contentreturn responseexcept Exception as e:logging.error(f"Error generating response: {e}")return "Sorry, I couldn't generate a response."def chatbot_interface(user_query, response_style):bot = MistralRAGChatbot(VECTOR_DB_PATH, ANNOY_INDEX_PATH)return bot.generate_response(user_query, response_style)iface = gr.Blocks(theme="Rabbitt-AI/ChanceRAG")with iface:2 unmodified linesfn=chatbot_interface,theme="Rabbitt-AI/ChanceRAG",inputs=[gr.Textbox(lines=5, label="User Query"),gr.Dropdown(["Detailed", "Concise", "Creative", "Technical"], label="Response Style"),],outputs=gr.Textbox(label="ChanceRAG Response"),)iface.launch(share=True)
app.py+73-264