Baldezo313 commited on
Commit
d5fd2c1
·
verified ·
1 Parent(s): f9ca13e

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. Medical_Book.pdf +3 -0
  3. README.md +9 -12
  4. app.py +96 -0
  5. requirements.txt +13 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ Medical_Book.pdf filter=lfs diff=lfs merge=lfs -text
Medical_Book.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:753cd53b7a3020bbd91f05629b0e3ddcfb6a114d7bbedb22c2298b66f5dd00cc
3
+ size 16127037
README.md CHANGED
@@ -1,13 +1,10 @@
1
- ---
2
- title: Medical Chatbot Space
3
- emoji: 📊
4
- colorFrom: green
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 5.34.2
8
- app_file: app.py
9
- pinned: false
10
- short_description: A medical assistant chatbot
11
- ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
1
+ # 🩺 Medical Chatbot using Mistral LLM
 
 
 
 
 
 
 
 
 
 
2
 
3
+ A medical assistant chatbot using LangChain, FAISS, HuggingFace LoRA model, and Gradio.
4
+
5
+ ## Features
6
+ - Retrieval-Augmented Generation (RAG)
7
+ - Conversational context
8
+ - PDF ingestion (Medical_Book.pdf)
9
+
10
+ Built and deployed by Mamadou Saidou Baldé.
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import gradio as gr
4
+
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, pipeline
6
+ from langchain.document_loaders import PyPDFLoader
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain.vectorstores import FAISS
9
+ from langchain_huggingface import HuggingFaceEmbeddings
10
+ from langchain.llms import HuggingFacePipeline
11
+ from langchain.chains import ConversationalRetrievalChain
12
+ from langchain.prompts import PromptTemplate
13
+ from langchain.chains.question_answering import load_qa_chain
14
+ from langchain.chains.llm import LLMChain
15
+
16
+ # Load PDF
17
+ loader = PyPDFLoader("Medical_Book.pdf")
18
+ documents = loader.load()
19
+
20
+ # Split text
21
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
22
+ all_splits = text_splitter.split_documents(documents)
23
+
24
+ # Embeddings
25
+ model_name = "sentence-transformers/all-mpnet-base-v2"
26
+ embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={"device": "cpu"})
27
+ vectorstores = FAISS.from_documents(all_splits, embeddings)
28
+
29
+ # Load LLM with quantization
30
+ llm_model = "ritvik77/Medical_Doctor_AI_LoRA-Mistral-7B-Instruct_FullModel"
31
+ bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16)
32
+ model = AutoModelForCausalLM.from_pretrained(llm_model, quantization_config=bnb_config, trust_remote_code=True, use_cache=True, device_map="auto")
33
+ tokenizer = AutoTokenizer.from_pretrained(llm_model, trust_remote_code=True)
34
+ tokenizer.pad_token = tokenizer.eos_token
35
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=150, temperature=0.7, top_p=0.9, do_sample=True)
36
+ llm = HuggingFacePipeline(pipeline=pipe)
37
+
38
+ # Prompt templates
39
+ condense_prompt = PromptTemplate(
40
+ input_variables=["question", "chat_history"],
41
+ template="""
42
+ You are a helpful medical assistant. Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question.
43
+
44
+ Chat History:
45
+ {chat_history}
46
+
47
+ Follow-up Question:
48
+ {question}
49
+
50
+ Standalone Question:"""
51
+ )
52
+ qa_prompt = PromptTemplate(
53
+ input_variables=["context", "question"],
54
+ template="""
55
+ Use the following context to answer the question.
56
+
57
+ Context:
58
+ {context}
59
+
60
+ Question:
61
+ {question}
62
+
63
+ Answer:
64
+ """
65
+ )
66
+ question_generator = LLMChain(llm=llm, prompt=condense_prompt)
67
+ combine_docs_chain = load_qa_chain(llm=llm, chain_type="stuff", prompt=qa_prompt)
68
+
69
+ def chatbot_response(user_input, max_new_tokens, temperature, context_length):
70
+ pipe.model.config.max_new_tokens = int(max_new_tokens)
71
+ pipe.model.config.temperature = float(temperature)
72
+ pipe.model.config.context_length = int(context_length)
73
+ chain = ConversationalRetrievalChain(retriever=vectorstores.as_retriever(), combine_docs_chain=combine_docs_chain, question_generator=question_generator, return_source_documents=True)
74
+ chat_history = []
75
+ result = chain({"question": user_input, "chat_history": chat_history})
76
+ return f"<div style='max-height: 400px; overflow-y: auto;'>{result['answer']}</div>"
77
+
78
+ interface = gr.Interface(
79
+ fn=chatbot_response,
80
+ inputs=[
81
+ gr.Textbox(lines=2, placeholder="Type your question here...", label="Your Question", interactive=True),
82
+ gr.Slider(label="Max New Tokens", minimum=1, maximum=2000, value=150, step=1, interactive=True),
83
+ gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.7, step=0.01, interactive=True),
84
+ gr.Slider(label="Context Length", minimum=100, maximum=4000, value=2000, step=1, interactive=True)
85
+ ],
86
+ outputs=gr.HTML(label="Chatbot Response"),
87
+ title="🩺 MEDICAL Chatbot",
88
+ description="""
89
+ <div style='text-align: center;'>
90
+ <img src='https://cdn.dribbble.com/users/29678/screenshots/2407580/media/34ee4b818fd4ddb3a616c91ccf4d9cfc.png' alt='Medical Bot' width='100'>
91
+ <p>Check the responses from the Medical Llama 3-8B model!</p>
92
+ </div>
93
+ """
94
+ )
95
+
96
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ langchain
5
+ langchain_community
6
+ langchain-huggingface
7
+ sentence-transformers
8
+ pypdf
9
+ faiss-cpu
10
+ bitsandbytes
11
+ accelerate
12
+ scikit-learn
13
+ typer==0.10.0