CamiloVega commited on
Commit
e5afc54
·
verified ·
1 Parent(s): d0f4bec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -38
app.py CHANGED
@@ -1,6 +1,17 @@
1
  import os
2
  import logging
3
  from typing import List, Dict
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Configure logging
6
  logging.basicConfig(
@@ -9,34 +20,12 @@ logging.basicConfig(
9
  )
10
  logger = logging.getLogger(__name__)
11
 
12
- try:
13
- # Basic imports first
14
- import torch
15
- import gradio as gr
16
- from langchain.text_splitter import RecursiveCharacterTextSplitter
17
- from langchain.embeddings import HuggingFaceEmbeddings
18
- from langchain.vectorstores import FAISS
19
- from langchain.chains import RetrievalQA
20
- from langchain.prompts import PromptTemplate
21
- from langchain.llms import HuggingFacePipeline
22
- from langchain_community.document_loaders import PyPDFLoader
23
-
24
- # Now try to import transformers components one by one
25
- from transformers.pipelines import pipeline
26
- from transformers import AutoTokenizer, AutoModelForCausalLM
27
-
28
- # If all imports successful, proceed with spaces import
29
- import spaces
30
-
31
- except ImportError as e:
32
- logger.error(f"Error importing dependencies: {str(e)}")
33
- logger.error("Trying to install missing packages...")
34
- os.system('pip install -q transformers torch accelerate safetensors')
35
-
36
- # Try imports again after installation
37
- from transformers.pipelines import pipeline
38
- from transformers import AutoTokenizer, AutoModelForCausalLM
39
- import spaces
40
 
41
  # Constants
42
  MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf"
@@ -54,6 +43,10 @@ class DocumentLoader:
54
  (f.startswith('valencia') or 'fislac' in f.lower() or 'Valencia' in f)
55
  ]
56
 
 
 
 
 
57
  for pdf_file in pdf_files:
58
  pdf_path = os.path.join(directory_path, pdf_file)
59
  try:
@@ -73,9 +66,6 @@ class DocumentLoader:
73
  except Exception as e:
74
  logger.error(f"Error loading {pdf_file}: {str(e)}")
75
 
76
- if not documents:
77
- logger.warning("No PDF documents found in the specified directory")
78
-
79
  return documents
80
 
81
  class TextProcessor:
@@ -129,39 +119,47 @@ class RAGSystem:
129
  # Load and process documents
130
  loader = DocumentLoader()
131
  documents = loader.load_pdfs(KNOWLEDGE_BASE_DIR)
 
 
132
 
133
  processor = TextProcessor()
134
  processed_chunks = processor.process_documents(documents)
 
 
135
 
136
  # Initialize embeddings
 
137
  self.embeddings = HuggingFaceEmbeddings(
138
  model_name="intfloat/multilingual-e5-large",
139
- model_kwargs={'device': 'cuda'},
140
  encode_kwargs={'normalize_embeddings': True}
141
  )
142
 
143
  # Create vector store
 
144
  self.vector_store = FAISS.from_documents(
145
  processed_chunks,
146
  self.embeddings
147
  )
148
 
149
  # Initialize LLM
 
150
  self.tokenizer = AutoTokenizer.from_pretrained(
151
  self.model_name,
152
- trust_remote_code=True,
153
- token=hf_token
154
  )
155
 
156
  self.model = AutoModelForCausalLM.from_pretrained(
157
  self.model_name,
 
158
  torch_dtype=torch.float16,
159
  trust_remote_code=True,
160
- token=hf_token,
161
  device_map="auto"
162
  )
163
 
164
  # Create generation pipeline
 
165
  pipe = pipeline(
166
  "text-generation",
167
  model=self.model,
@@ -191,6 +189,7 @@ class RAGSystem:
191
  )
192
 
193
  # Set up QA chain
 
194
  self.qa_chain = RetrievalQA.from_chain_type(
195
  llm=llm,
196
  chain_type="stuff",
@@ -258,9 +257,13 @@ def process_response(user_input: str, chat_history: List) -> tuple:
258
 
259
  # Initialize RAG system
260
  logger.info("Initializing RAG system...")
261
- rag_system = RAGSystem()
262
- rag_system.initialize_system()
263
- logger.info("RAG system initialization completed")
 
 
 
 
264
 
265
  # Create Gradio interface
266
  try:
 
1
  import os
2
  import logging
3
  from typing import List, Dict
4
+ import torch
5
+ import gradio as gr
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain.embeddings import HuggingFaceEmbeddings
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.chains import RetrievalQA
10
+ from langchain.prompts import PromptTemplate
11
+ from langchain.llms import HuggingFacePipeline
12
+ from langchain_community.document_loaders import PyPDFLoader
13
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
14
+ import spaces
15
 
16
  # Configure logging
17
  logging.basicConfig(
 
20
  )
21
  logger = logging.getLogger(__name__)
22
 
23
+ # Get HuggingFace token from environment variables
24
+ hf_token = os.environ.get('HUGGINGFACE_TOKEN') or os.environ.get('HF_TOKEN')
25
+ if not hf_token:
26
+ logger.error("No Hugging Face token found in environment variables")
27
+ logger.error("Please set either HUGGINGFACE_TOKEN or HF_TOKEN in your Space settings")
28
+ raise ValueError("Missing Hugging Face token. Please configure it in the Space settings under Repository Secrets.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Constants
31
  MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf"
 
43
  (f.startswith('valencia') or 'fislac' in f.lower() or 'Valencia' in f)
44
  ]
45
 
46
+ if not pdf_files:
47
+ logger.warning(f"No matching PDF files found in {directory_path}")
48
+ return documents
49
+
50
  for pdf_file in pdf_files:
51
  pdf_path = os.path.join(directory_path, pdf_file)
52
  try:
 
66
  except Exception as e:
67
  logger.error(f"Error loading {pdf_file}: {str(e)}")
68
 
 
 
 
69
  return documents
70
 
71
  class TextProcessor:
 
119
  # Load and process documents
120
  loader = DocumentLoader()
121
  documents = loader.load_pdfs(KNOWLEDGE_BASE_DIR)
122
+ if not documents:
123
+ raise ValueError("No documents were loaded. Please check the PDF files in the root directory.")
124
 
125
  processor = TextProcessor()
126
  processed_chunks = processor.process_documents(documents)
127
+ if not processed_chunks:
128
+ raise ValueError("No chunks were created from the documents.")
129
 
130
  # Initialize embeddings
131
+ logger.info("Initializing embeddings...")
132
  self.embeddings = HuggingFaceEmbeddings(
133
  model_name="intfloat/multilingual-e5-large",
134
+ model_kwargs={'device': 'cuda' if torch.cuda.is_available() else 'cpu'},
135
  encode_kwargs={'normalize_embeddings': True}
136
  )
137
 
138
  # Create vector store
139
+ logger.info("Creating vector store...")
140
  self.vector_store = FAISS.from_documents(
141
  processed_chunks,
142
  self.embeddings
143
  )
144
 
145
  # Initialize LLM
146
+ logger.info("Initializing language model...")
147
  self.tokenizer = AutoTokenizer.from_pretrained(
148
  self.model_name,
149
+ token=hf_token,
150
+ trust_remote_code=True
151
  )
152
 
153
  self.model = AutoModelForCausalLM.from_pretrained(
154
  self.model_name,
155
+ token=hf_token,
156
  torch_dtype=torch.float16,
157
  trust_remote_code=True,
 
158
  device_map="auto"
159
  )
160
 
161
  # Create generation pipeline
162
+ logger.info("Creating generation pipeline...")
163
  pipe = pipeline(
164
  "text-generation",
165
  model=self.model,
 
189
  )
190
 
191
  # Set up QA chain
192
+ logger.info("Setting up QA chain...")
193
  self.qa_chain = RetrievalQA.from_chain_type(
194
  llm=llm,
195
  chain_type="stuff",
 
257
 
258
  # Initialize RAG system
259
  logger.info("Initializing RAG system...")
260
+ try:
261
+ rag_system = RAGSystem()
262
+ rag_system.initialize_system()
263
+ logger.info("RAG system initialization completed")
264
+ except Exception as e:
265
+ logger.error(f"Failed to initialize RAG system: {str(e)}")
266
+ raise
267
 
268
  # Create Gradio interface
269
  try: