-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covid19_RAG_Implementation.py
391 lines (308 loc) · 11.5 KB
/
Covid19_RAG_Implementation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#%%
import kaggle
from kaggle.api.kaggle_api_extended import KaggleApi
import os
import pandas as pd
import zipfile
import requests
import json
import chromadb
from chromadb.utils import embedding_functions
from typing import Optional, Dict, Any
def setup_kaggle():
try:
api = KaggleApi()
api.authenticate()
print('Kaggle API authenticated')
return api
except Exception as e:
print(f'Authentication Error: {e}')
return None
def process_metadata():
# Check if the unzipped file already exists
if os.path.exists('./data/covid19_subset.csv'): # Check this path
print('Loading existing processed data...')
try:
df = pd.read_csv('./data/covid19_subset.csv')
print("Data loaded successfully")
return df
except Exception as e:
print(f"Error loading existing data: {e}")
return None
#checking if zip file exists
if os.path.exists('./data/metadata.zip'):
print('Metadata found; extracting file starting.....')
with zipfile.ZipFile('/data/metadata.zip', 'r') as zip_re:
zip_re.extractall('./data')
else:
api=setup_kaggle()
if api:
try:
api.dataset_download_file(
'allen-institute-for-ai/CORD-19-research-challenge',
'metadata.csv',
path='./data')
print('Metadata downloaded Successfully')
#extract the zip file
with zipfile.ZipFile('./data/metadata.csv.zip', 'r') as zip_ref:
zip_ref.extractall('./data')
except Exception as e:
print(f"Download Error: {e}")
return None
try:
#processing the extracted dataset
print("Dataset Processing")
chunks = pd.read_csv('./data/metadata.csv.zip', chunksize= 1000)
selected_data=pd.concat([next(chunks) for _ in range(5)])
#save processed data
selected_data.to_csv('./data/covid19_subset.csv', index=False)
print("Created smaller subset of data!")
#set display options to show all columns and rows
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 100)
#Preview the dataset
print("\nPreview of the data:")
print(selected_data.head().to_string())
#display column information '
print("\nDetailed column Information")
for col in selected_data.columns:
non_null = selected_data[col].count()
dtype =selected_data[col].dtype
print(f"{col:20} | Type: {dtype :10} | Non-null count:{non_null}")
#display basic statistics
print("\nBasic Statistics::")
print(selected_data.describe().to_string())
#display sample of text columns (like abstract and title)
print("\nSample Title:")
print(selected_data['title'].head().to_string())
print("\nSample Abstract:")
print(selected_data['abstract'].head().to_string())
return selected_data
except Exception as e:
print(f"Processing Error: {e}")
return None
#RAG Implementation fro Questioning and answering system about medical research
def clean_dataset(df):
relevant_columns=['title', 'abstract', 'authors', 'publish_time', 'journal']
df_cleaned = df[relevant_columns]
#remove missing values in abstract
df_cleaned=df_cleaned.dropna(subset=['abstract'])
df_cleaned['text']="Title: " + df_cleaned['title'] + "\nAbstract: " + df_cleaned['abstract']
return df_cleaned
from langchain.text_splitter import RecursiveCharacterTextSplitter
def preprocess_text(df):
text_splitter=RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=50,
separators=["\n\n", "\n", ".", "!", "?", " ", ""]
)
documents=[]
metadata=[]
for idx, row in df.iterrows():
chunks =text_splitter.split_text(row['text'])
for chunk in chunks:
documents.append(chunk)
metadata.append({
'title':row['title'],
'authors': row['authors'],
'publish_time': row['publish_time'],
})
return documents, metadata
#setup chromadb and store embeddings
import chromadb
from chromadb.utils import embedding_functions
def setup_chroma():
"""Setup ChromaDB with minimal output"""
try:
import tensorflow as tf
from sentence_transformers import SentenceTransformer
client = chromadb.PersistentClient(path="./medical_research_db")
try:
existing_collection = client.get_collection(
name="medical_research",
embedding_function=embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="all-mpnet-base-v2"
)
)
return existing_collection
except Exception:
# Create new collection only if it doesn't exist
collection = client.create_collection(
name="medical_research",
embedding_function=embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="all-mpnet-base-v2"
)
)
return collection
except Exception as e:
print(f"Error in ChromaDB setup: {e}")
raise
#store documents - creating vector stores
def store_documents(collection, documents, metadata):
"""Store documents quietly"""
try:
# Get existing document count
existing_count = collection.count()
if existing_count > 0:
return
# Store in batches only if collection is empty
batch_size = 100
for i in range(0, len(documents), batch_size):
batch_docs = documents[i:i + batch_size]
batch_metadata = metadata[i:i + batch_size]
collection.add(
documents=batch_docs,
metadatas=batch_metadata,
ids=[f"doc_{j}" for j in range(i, i + len(batch_docs))]
)
except Exception as e:
print(f"Error storing documents: {e}")
raise
#testing hugging face access token
def test_hf_token():
try:
from huggingface_hub import HfApi
api=HfApi()
identity = api.whoami()
print(f"Successfully authenticated as :{identity}")
return True
except Exception as e:
print(f"Token verification failed: {e}")
return False
#query and retrival functions
def setup_ollama(model_name="llama2"):
"""
Setup Ollama model for inference
using Huggingface API directly
"""
def query_ollama(prompt, temperature=0.5):
try:
response = requests.post('http://localhost:11434/api/generate',
json={
"model": model_name,
"prompt": prompt,
"temperature": temperature,
"stream": False
}
)
if response.status_code == 200:
result = response.json()['response']
print("Ollama test successful!")
print("Response:", result)
return result
else:
print(f"Error: Status code {response.status_code}")
return None
except Exception as e:
print(f"Error querying Ollama: {e}")
return None
# Test the connection
try:
test_response = query_ollama("Test connection")
if test_response:
return query_ollama
return None
except Exception as e:
print(f"Error setting up Ollama: {e}")
return None
def query_medical_research(collection, query, n_results=3):
# Search for relevant documents
results = collection.query(
query_texts=[query],
n_results=n_results
)
return results
def medical_qa(query, collection, ollama_query):
"""
Medical QA using Ollama and ChromaDB
"""
try:
# Get relevant contexts
results = collection.query(
query_texts=[query],
n_results=3
)
if not results or not results['documents']:
return {"answer": "No relevant documents found", "sources": []}
# Format context
context = "\n".join(results['documents'][0])
# Create prompt
prompt = f"""Based on the following medical research context, please provide a detailed answer.
Focus on medical findings and cite specific research evidence when possible.
Context: {context}
Question: {query}
Answer:"""
# Get response from Ollama
response = ollama_query(prompt)
# Add source information
sources = [
{
"title": meta["title"],
"publish_time": meta["publish_time"]
}
for meta in results["metadatas"][0]
]
return {
"answer": response,
"sources": sources
}
except Exception as e:
print(f"Error in medical QA: {e}")
return {"answer": f"Error: {str(e)}", "sources": []}
#main execution
def main():
try:
# Create data directory if it doesn't exist
os.makedirs('./data', exist_ok=True)
# Process metadata first
print("Loading dataset...")
df = process_metadata()
if df is None:
raise ValueError("Failed to load dataset")
print("Dataset loaded successfully")
# Clean dataset
print("Cleaning dataset...")
df_cleaned = clean_dataset(df)
print("Dataset cleaned")
# Preprocess and chunk text
print("Preprocessing text...")
documents, metadata = preprocess_text(df_cleaned)
print(f"Created {len(documents)} chunks")
# Setup ChromaDB
print("Setting up ChromaDB...")
collection = setup_chroma()
if collection is None:
raise ValueError("ChromaDB setup failed")
print("ChromaDB setup complete")
# Store documents
print("Storing documents...")
if documents is None or metadata is None:
raise ValueError("Documents or metadata is None")
store_documents(collection, documents, metadata)
print("Documents stored in ChromaDB")
#setup LLM
print("Setting up LLM.....")
#Replace LLM setup with Ollama setup
print("Setting up Ollama.....")
ollama_query = setup_ollama("llama2") # or another model you've pulled
if ollama_query is None:
raise ValueError("Ollama setup failed - check if Ollama is running")
print("Ollama setup complete")
# Example query
query = "What are the main symptoms of respiratory infections in these studies?"
print("\nQuery:", query)
response = medical_qa(query, collection, ollama_query)
print("\nAnswer:", response["answer"])
print("\nSources:")
for source in response["sources"]:
print(f"- {source['title']} ({source['publish_time']})")
return response
except Exception as e:
print(f"Error in main: {e}")
import traceback
print(traceback.format_exc())
return None
if __name__ == '__main__':
main()