anishas19 commited on
Commit
584e63b
·
verified ·
1 Parent(s): a0219fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -1,36 +1,47 @@
1
- from google.colab import drive
2
- drive.mount("/content/drive")
3
- !pip install langchain sentence-transformers chromadb llama-cpp-python langchain_community pypdf
4
  from langchain_community.document_loaders import PyPDFDirectoryLoader
5
  from langchain_text_splitters import RecursiveCharacterTextSplitter
6
  from langchain_community.embeddings import SentenceTransformerEmbeddings
7
  from langchain.vectorstores import Chroma
8
  from langchain_community.llms import LlamaCpp
9
- from langchain.chains import RetrievalQA, LLMChain
10
- loader=PyPDFDirectoryLoader("/content/drive/MyDrive/BioMistral/Data")
11
- docs=loader.load()
12
- text_splitter=RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
13
- chunks=text_splitter.split_documents(docs)
14
- import os
15
- import os
16
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
19
  vectorstore = Chroma.from_documents(chunks, embeddings)
20
- query="What are the major risk factors of heart disease?"
21
- search_results=vectorstore.similarity_search(query)
22
- search_results
23
- retriever=vectorstore.as_retriever(search_kwargs={"k":5})
24
- retriever.get_relevant_documents(query)
25
- llm=LlamaCpp(
26
- model_path="/content/drive/MyDrive/BioMistral/BioMistral-7B.Q4_K_M.gguf",
27
  temperature=0.2,
28
  max_tokens=2048,
29
  top_p=1
30
  )
31
- template="""
 
 
32
  <|context|>
33
- You are an Medical Assistant that follows the instruction and generate the accurate response based on the query and the context provided.
34
  Please be truthful and give direct answers.
35
  </s>
36
  <|user|>
@@ -38,32 +49,18 @@ Please be truthful and give direct answers.
38
  </s>
39
  <|assistant|>
40
  """
41
- from langchain.schema.runnable import RunnablePassthrough
42
- from langchain.schema.output_parser import StrOutputParser
43
- from langchain.prompts import ChatPromptTemplate
44
- prompt=ChatPromptTemplate.from_template(template)
45
- rag_chain=(
46
- {"context":retriever,"query":RunnablePassthrough()}
47
  | prompt
48
  | llm
49
  | StrOutputParser()
50
- )
51
- response=rag_chain.invoke("query")
52
- response
53
- import sys
54
- while True:
55
- user_input=input(f"Input query: ")
56
- if user_input=='exit':
57
- print("Exiting...")
58
- sys.exit()
59
- if user_input=="":
60
- continue
61
- result=rag_chain.invoke(user_input)
62
- print("Answer: ",result)
63
- !pip install gradio
64
- import gradio as gr
65
 
66
- # Define a function to handle queries
67
  def chatbot_ui(user_query):
68
  if not user_query.strip():
69
  return "Please enter a valid query."
@@ -75,11 +72,11 @@ def chatbot_ui(user_query):
75
 
76
  # Create the Gradio interface
77
  interface = gr.Interface(
78
- fn=chatbot_ui, # Function to process the query
79
  inputs=gr.Textbox(label="Enter your medical query:", placeholder="Ask a medical question here..."),
80
  outputs=gr.Textbox(label="Chatbot Response"),
81
  title="Medical Assistant Chatbot",
82
- description="A chatbot made for heart patients.",
83
  examples=[
84
  ["What are the symptoms of diabetes?"],
85
  ["Explain the risk factors of heart disease."],
@@ -87,5 +84,6 @@ interface = gr.Interface(
87
  ]
88
  )
89
 
90
- # Launch the Gradio interface
91
- interface.launch(share=True)
 
 
1
+ import os
 
 
2
  from langchain_community.document_loaders import PyPDFDirectoryLoader
3
  from langchain_text_splitters import RecursiveCharacterTextSplitter
4
  from langchain_community.embeddings import SentenceTransformerEmbeddings
5
  from langchain.vectorstores import Chroma
6
  from langchain_community.llms import LlamaCpp
7
+ from langchain.prompts import ChatPromptTemplate
8
+ from langchain.schema.runnable import RunnablePassthrough
9
+ from langchain.schema.output_parser import StrOutputParser
10
+ import gradio as gr
11
+
12
+ # Environment variable for Hugging Face API token
 
13
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
14
 
15
+ # Paths for PDFs and model (upload these to the Hugging Face Space)
16
+ PDF_DIR = "./Data" # Replace with the path where you upload your PDFs
17
+ MODEL_PATH = "./BioMistral-7B.Q4_K_M.gguf" # Replace with the model's path in the Space
18
+
19
+ # Load and process PDF documents
20
+ loader = PyPDFDirectoryLoader(PDF_DIR)
21
+ docs = loader.load()
22
+
23
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
24
+ chunks = text_splitter.split_documents(docs)
25
+
26
+ # Create embeddings and vector store
27
  embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
28
  vectorstore = Chroma.from_documents(chunks, embeddings)
29
+
30
+ # Retriever for querying
31
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
32
+
33
+ # Initialize the LLM
34
+ llm = LlamaCpp(
35
+ model_path=MODEL_PATH,
36
  temperature=0.2,
37
  max_tokens=2048,
38
  top_p=1
39
  )
40
+
41
+ # Define the prompt template
42
+ template = """
43
  <|context|>
44
+ You are a Medical Assistant that follows instructions and generates accurate responses based on the query and the context provided.
45
  Please be truthful and give direct answers.
46
  </s>
47
  <|user|>
 
49
  </s>
50
  <|assistant|>
51
  """
52
+
53
+ prompt = ChatPromptTemplate.from_template(template)
54
+
55
+ # Define the RAG chain
56
+ rag_chain = (
57
+ {"context": retriever, "query": RunnablePassthrough()}
58
  | prompt
59
  | llm
60
  | StrOutputParser()
61
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Define a function for the Gradio UI
64
  def chatbot_ui(user_query):
65
  if not user_query.strip():
66
  return "Please enter a valid query."
 
72
 
73
  # Create the Gradio interface
74
  interface = gr.Interface(
75
+ fn=chatbot_ui,
76
  inputs=gr.Textbox(label="Enter your medical query:", placeholder="Ask a medical question here..."),
77
  outputs=gr.Textbox(label="Chatbot Response"),
78
  title="Medical Assistant Chatbot",
79
+ description="A chatbot designed for heart patients, providing accurate and reliable medical information.",
80
  examples=[
81
  ["What are the symptoms of diabetes?"],
82
  ["Explain the risk factors of heart disease."],
 
84
  ]
85
  )
86
 
87
+ # Launch the Gradio app
88
+ if __name__ == "__main__":
89
+ interface.launch()