vmshankar86 commited on
Commit
48b048a
Β·
verified Β·
1 Parent(s): eabed86

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain_community.embeddings import SentenceTransformerEmbeddings
4
+ from langchain.vectorstores import Chroma
5
+ from langchain_community.llms import LlamaCpp
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.schema.runnable import RunnablePassthrough
8
+ from langchain.schema.output_parser import StrOutputParser
9
+ from langchain.prompts import ChatPromptTemplate
10
+ import gradio as gr
11
+ import os
12
+
13
+ # Set your Hugging Face API key
14
+ os.environ['HF_API_KEY']
15
+ api_key = os.getenv('HF_API_KEY')
16
+ if api_key is None:
17
+ raise ValueError("Hugging Face API key is not set. Please set it in your environment.")
18
+
19
+ # Load documents from a directory
20
+ loader = PyPDFDirectoryLoader("/content/drive/MyDrive/HealthCareData/")
21
+ docs = loader.load()
22
+
23
+ # Split documents into chunks
24
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
25
+ chunks = text_splitter.split_documents(docs)
26
+
27
+ # Create embeddings and vector store
28
+ embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
29
+ vectorstore = Chroma.from_documents(chunks, embeddings)
30
+
31
+ # Create a retriever
32
+ retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
33
+
34
+ # Load the LLM (LlamaCpp)
35
+ llm = LlamaCpp(
36
+ model_path='/content/drive/MyDrive/HealthCareData/mistral-7b-instruct-v0.1.Q6_K.gguf',
37
+ temperature=0.2,
38
+ max_tokens=2048,
39
+ top_p=1
40
+ )
41
+
42
+ # Define the prompt template
43
+ template = """
44
+ You are a Medical Assistant that follows the instructions and generates accurate responses based on the query and the context provided. Please be truthful and give direct answers.
45
+
46
+ {query}
47
+ """
48
+
49
+ prompt = ChatPromptTemplate.from_template(template)
50
+
51
+ # Create the retrieval chain
52
+ retrieval_chain = (
53
+ {"context": retriever, "query": RunnablePassthrough()}
54
+ | prompt
55
+ | llm
56
+ | StrOutputParser()
57
+ )
58
+
59
+ # Define the chat function for Gradio
60
+ def chat(user_input):
61
+ if user_input.lower() == 'exit':
62
+ return "Exiting..."
63
+ if not user_input.strip():
64
+ return "Please enter a valid query."
65
+ result = retrieval_chain.invoke(user_input)
66
+ return result
67
+
68
+ # Create Gradio interface with improved design
69
+ iface = gr.Interface(
70
+ fn=chat,
71
+ inputs=gr.Textbox(label="Your Query", placeholder="Type your question here...", lines=2),
72
+ outputs=gr.Textbox(label="Response"),
73
+ title="🩺 BioMistral Medical Chatbot",
74
+ description="πŸ€– Ask me any healthcare or biology-related queries!",
75
+ theme="soft",
76
+ live=True,
77
+ css="""
78
+ body {
79
+ background-color: #f0f4f8;
80
+ color: #333;
81
+ }
82
+ .gradio-container {
83
+ border-radius: 12px;
84
+ box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
85
+ background: #ffffff;
86
+ padding: 20px;
87
+ }
88
+ input, textarea {
89
+ border-radius: 8px;
90
+ border: 1px solid #ddd;
91
+ padding: 10px;
92
+ }
93
+ button {
94
+ background-color: #007bff;
95
+ color: white;
96
+ border-radius: 8px;
97
+ padding: 10px 15px;
98
+ border: none;
99
+ transition: 0.3s;
100
+ }
101
+ button:hover {
102
+ background-color: #0056b3;
103
+ }
104
+ """
105
+ )
106
+
107
+ # Launch the Gradio app
108
+ iface.launch()