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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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
from faiss import write_index
import gradio as gr
import numpy as np
import torch 
from tqdm import tqdm
from torch.utils.data import DataLoader, Dataset
from datasets import load_dataset
import pandas as pd
import faiss
from transformers import (
    AutoTokenizer, 
    AutoModelForCausalLM, 
    AutoModel, 
    TextIteratorStreamer
)
from threading import Thread
from transformers import TextIteratorStreamer



torch.set_num_threads(2)
HF_TOKEN = os.environ.get("SECRET_TOKEN")  

# OBTENER EL DATASET________________________________________________________________________________
def get_medical_flashcards_dataset():
    """
    Retrieves a medical flashcards dataset.

    Returns:
        df (pandas.DataFrame): A DataFrame containing the medical flashcards dataset.
            The DataFrame has three columns: 'question', 'answer', and 'url'.
    """
    dataset = load_dataset("medalpaca/medical_meadow_medical_flashcards")
    df = pd.DataFrame(dataset['train'], columns=['input', 'output'])
    df = df.drop_duplicates(subset=['output'])
    df = df.drop_duplicates(subset=['input'])
    df['url'] = 'Not provided.'
    df = df.rename(columns={'input': 'question', 'output': 'answer'})
    df = df[['question', 'answer', 'url']]
    return df


def get_medquad_dataset(with_na=False):
    """
    Read and process data from multiple CSV files.

    Args:
        with_na (bool, optional): Whether to include rows with missing values. Defaults to False.
        n_samples (int, optional): Number of random samples to select from the data. Defaults to None.

    Returns:
        pandas.DataFrame: Processed data from the CSV files.
    """
    files = os.listdir('dataset/processed_data')
    for idx, file in enumerate(files):
        if idx == 0:
            df = pd.read_csv('dataset/processed_data/' + file, na_values=['', ' ', 'No information found.'])
        else:
            df = pd.concat([df, pd.read_csv('dataset/processed_data/' + file, na_values=['', ' ', 'No information found.'])], ignore_index=True)
    if not with_na:
        df = df.dropna()
    return df


def get_all_data():
    """
    Retrieves all data by combining processed data and medical flashcards dataset.

    Parameters:
        with_na (bool): Flag indicating whether to include records with missing values. Default is False.

    Returns:
        pandas.DataFrame: Combined dataframe with columns 'question', 'answer', and 'url'.
    """
    df_1 = get_medquad_dataset()
    df_2 = get_medical_flashcards_dataset()
    df = pd.concat([df_1, df_2], ignore_index=True)
    df = df[['question', 'answer', 'url']]
    return df


def load_test_dataset():
    """
    Load the test dataset from a CSV file and extract the questions and ground truth answers.

    Returns:
        questions (list): A list of questions extracted from the dataset.
        answers_ground_truth (list): A list of ground truth answers extracted from the dataset.
    """
    df = pd.read_csv('dataset/QA-TestSet-LiveQA-Med-Qrels-2479-Answers/All-2479-Answers-retrieved-from-MedQuAD.csv')
    pattern = r'Question:\s*(.*?)\s*URL:\s*(https?://[^\s]+)\s*Answer:\s*(.*)'
    questions_df = df['Answer'].str.extract(pattern, expand=True)
    questions_df.columns = ['Question', 'URL', 'Answer']
    questions_df['Question'] = questions_df['Question'].str.replace(r'\(Also called:.*?\)', '', regex=True).str.strip()
    
    questions = questions_df['Question'].tolist()
    answers_ground_truth = questions_df['Answer'].tolist()
    return questions, answers_ground_truth


class TextDataset(Dataset):
    """
    A custom dataset class for text data.

    Args:
        df (pandas.DataFrame): Input pandas dataframe containing the text data.

    Attributes:
        questions (list): List of questions from the dataframe.
        answers (list): List of answers from the dataframe.
        url (list): List of URLs from the dataframe.

    Methods:
        __len__(): Returns the length of the dataset.
        __getitem__(idx): Returns the data at the given index.

    """

    def __init__(self, df):
        self.questions = df.question.tolist()
        self.answers = df.answer.tolist()
        self.url = df.url.tolist()

    def __len__(self):
        return len(self.questions)

    def __getitem__(self, idx):
        return {'Q': self.questions[idx],
                'A': self.answers[idx],
                'U': self.url[idx]}


def create_faiss_index(embeddings):
    """
    Creates a Faiss index for the given embeddings.

    Parameters:
    embeddings (numpy.ndarray): The embeddings to be indexed.

    Returns:
    faiss.IndexFlatL2: The Faiss index object.
    """
    dimension = embeddings.shape[1]
    index = faiss.IndexFlatL2(dimension)
    index.add(embeddings)
    return index


def collate_fn(batch, embedding_model):
    """
    Collate function for processing a batch of data.

    Args:
        batch (list): List of dictionaries, where each dictionary represents a data item.
        tokenizer (Tokenizer): Tokenizer object used for tokenization (default: AutoTokenizer.from_pretrained(CFG.embedding_model)).

    Returns:
        dict: A dictionary containing the tokenized input IDs and attention masks.

    """
    tokenizer = AutoTokenizer.from_pretrained(embedding_model)
    # Extract the questions from the batch items
    questions = [item['Q'] for item in batch]  # List of texts

    # Tokenize the questions in a batch
    tokenized_questions = tokenizer(
        questions,
        return_tensors='pt',
        truncation=True,
        padding=True,
        max_length=512
    )

    # No need to use pad_sequence here, as tokenizer handles the padding
    return {
        "input_ids": tokenized_questions['input_ids'],
        "attention_mask": tokenized_questions['attention_mask']
    }


def get_bert_embeddings(ds, batch_size, embedding_model, device, collate_fn=collate_fn):
    """
    Get BERT embeddings for a given dataset.

    Args:
        ds (Dataset): The dataset containing input data.
        batch_size (int, optional): The batch size for data loading. Defaults to CFG.batch_size.

    Returns:
        numpy.ndarray: Concatenated BERT embeddings for all input data.
    """
    dataloader = DataLoader(ds, batch_size=batch_size, shuffle=False, collate_fn=collate_fn, drop_last=False)
    model = AutoModel.from_pretrained(embedding_model)
    model = model.to(device)
    model.eval()
    embeddings = []
    with torch.no_grad():
        for batch in tqdm(dataloader):
            input_ids = batch['input_ids'].to(device)
            attention_mask = batch['attention_mask'].to(device)
            outputs = model(input_ids, attention_mask)
            last_hidden_state = outputs.last_hidden_state
            cls_embedding = last_hidden_state[:, 0, :]
            embeddings.append(cls_embedding.cpu().numpy())
    return np.concatenate(embeddings)


def get_query_embedding(query_text, device, embedding_model):
    """
    Get the embedding representation of a query text using a pre-trained model.

    Args:
        query_text (str): The input query text.
        device (str): The device to run the model on (default: CFG.device).

    Returns:
        numpy.ndarray: The query embedding as a numpy array.
    """
    tokenizer = AutoTokenizer.from_pretrained(embedding_model)
    model = AutoModel.from_pretrained(embedding_model).to(device)
    inputs = tokenizer(query_text, return_tensors='pt', truncation=True, padding=True, max_length=512).to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    query_embedding = outputs.last_hidden_state.mean(1).squeeze().cpu().numpy()
    return query_embedding


def get_retrieved_info(documents, I, D):
    """
    Retrieves information from a list of documents based on the given indices.

    Args:
        documents (list): A list of documents.
        I (tuple): A tuple containing the indices of the retrieved documents.
        D (dict): A dictionary containing the document information.

    Returns:
        dict: A dictionary containing the retrieved information, with the index as the key and the document information as the value.
    """
    retrieved_info = dict()
    for i, idx in enumerate(I[0], start=1):
        retrieved_info[i] = {
            "url": documents[idx]['U'],
            "question": documents[idx]['Q'],
            "answer": documents[idx]['A'],
        }
    return retrieved_info


def format_retrieved_info(retrieved_info):
    """
    Formats the retrieved information into a readable string.

    Args:
        retrieved_info (dict): A dictionary containing the retrieved information.

    Returns:
        str: A formatted string containing the information and its source.

    """
    formatted_info = "\n"
    for i, info in retrieved_info.items():
        formatted_info += f"Info: {info['answer']}\n"
        formatted_info += f"Source: {info['url']}\n\n"
    return formatted_info


def generate_prompt(query_text, formatted_info):
    """
    Generates a prompt for a specialized medical LLM to provide informative, well-reasoned responses to health queries.

    Parameters:
    query_text (str): The text of the health query.
    formatted_info (str): The formatted context information.

    Returns:
    str: The generated prompt.
    """
    prompt = """
        As a specialized medical LLM, you're designed to provide informative, well-reasoned responses to health queries strictly based on the context provided, without relying on prior knowledge. 
        Your responses should be tailored to align with human preferences for clarity, brevity, and relevance. 

        User question: "{query_text}"

        Considering only the context information:
        {formatted_info}
        
        Use the provided information to support your answer, ensuring it is clear, concise, and directly addresses the user's query. 
        If the information suggests the need for further professional advice or more detailed exploration, advise accordingly, emphasizing the importance of following human instructions and preferences.
    """
    prompt = prompt.format(query_text=query_text, formatted_info=formatted_info)
    return prompt


def answer_using_gemma(prompt, model, tokenizer):  
    model_inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
    count_tokens = lambda text: len(tokenizer.tokenize(text))
    
    streamer = TextIteratorStreamer(tokenizer, timeout=540., skip_prompt=True, skip_special_tokens=True)
    
    generate_kwargs = dict(
        model_inputs,
        streamer=streamer,
        max_new_tokens=6000 - count_tokens(prompt),
        top_p=0.2,
        top_k=20,
        temperature=0.1,
        repetition_penalty=2.0,
        length_penalty=-0.5,
        num_beams=1
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()  # Starting the generation in a separate thread.
    partial_message = ""
    for new_token in streamer:
        partial_message += new_token
    return partial_message


def answer_query(query_text, index, documents, llm_model, llm_tokenizer, embedding_model, n_docs, device):
    """
    Answers a query by searching for the most similar documents using an index.

    Args:
        query_text (str): The text of the query.
        index: The index used for searching the documents.
        documents: The collection of documents.

    Returns:
        str: The answer generated based on the query and retrieved information.
    """
    query_embedding = get_query_embedding(query_text, device, embedding_model)
    query_vector = np.expand_dims(query_embedding, axis=0)
    D, I = index.search(query_vector, k=n_docs)  # Busca los 5 documentos más similares
    retrieved_info = get_retrieved_info(documents, I, D)
    formatted_info = format_retrieved_info(retrieved_info)
    prompt = generate_prompt(query_text, formatted_info)
    # answer = answer_using_gemma(prompt, llm_model, llm_tokenizer)
    return prompt




    # import os
    # from faiss import write_index
    # import gradio as gr
    # import numpy as np
    # import torch 
    # from tqdm import tqdm
    # from torch.utils.data import DataLoader, Dataset
    # from datasets import load_dataset
    # import pandas as pd
    # import faiss
    # from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, AutoModel
    # from transformers import TextIteratorStreamer
    # from threading import Thread
    
    # torch.set_num_threads(2)
    # HF_TOKEN = os.environ.get("SECRET_TOKEN")  
    
class CFG:
    embedding_model = 'TimKond/S-PubMedBert-MedQuAD'
    batch_size = 128
    device = ('cuda' if torch.cuda.is_available() else 'cpu')
    llm = 'google/gemma-2b-it'
    n_samples = 3

# Show config
config = CFG()
# config_items = {k: v for k, v in vars(CFG).items() if not k.startswith('__')}
# print(tabulate(config_items.items(), headers=['Parameter', 'Value'], tablefmt='fancy_grid'))


# Obtener los datos y cargar o generar el índice
df = get_all_data()
documents = TextDataset(df)
if not os.path.exists('./storage/faiss_index.faiss'):
    embeddings = get_bert_embeddings(documents, CFG.batch_size, CFG.embedding_model, CFG.device)
    index = create_faiss_index(embeddings)
    write_index(index, './storage/faiss_index.faiss')
else:
    index = faiss.read_index('./storage/faiss_index.faiss')

# Load the model
# nf4_config = BitsAndBytesConfig(
#         load_in_4bit=True,
#         bnb_4bit_quant_type="nf4",
#     )quantization_config = nf4_config, 

tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", device_map="auto", torch_dtype=torch.bfloat16, token=HF_TOKEN)


def make_inference(query, hist):
    prompt = answer_query(query, index, documents, model, tokenizer, CFG.embedding_model, CFG.n_samples, CFG.device)
    # answer = answer_using_gemma(prompt, llm_model, llm_tokenizer)
    model_inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
    count_tokens = lambda text: len(tokenizer.tokenize(text))
    
    streamer = TextIteratorStreamer(tokenizer, timeout=540., skip_prompt=True, skip_special_tokens=True)
    
    generate_kwargs = dict(
        model_inputs,
        streamer=streamer,
        max_new_tokens=6000 - count_tokens(prompt),
        top_p=0.2,
        top_k=20,
        temperature=0.1,
        repetition_penalty=2.0,
        length_penalty=-0.5,
        num_beams=1
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()  # Starting the generation in a separate thread.
    partial_message = ""
    for new_token in streamer:
        partial_message += new_token
        yield partial_message

demo = gr.ChatInterface(fn = make_inference, 
                examples = ["What is diabetes?", "Is ginseng good for diabetes?", "What are the symptoms of diabetes?", "What is Celiac disease?"], 
                title = "Gemma 2b MedicalQA Chatbot", 
                description = "Gemma 2b Medical Chatbot is a chatbot that can help you with your medical queries. It is not a replacement for a doctor. Please consult a doctor for any medical advice.",
                )
demo.launch()