Spaces:
Runtime error
Runtime error
Umama-at-Bluchip
commited on
Commit
•
2ec5665
1
Parent(s):
880cee8
Upload 4 files
Browse files- .gitattributes +1 -0
- app.py +84 -0
- medchat_db/index.faiss +3 -0
- medchat_db/index.pkl +3 -0
- requirements.txt +8 -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 |
+
medchat_db/index.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.vectorstores import FAISS
|
2 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain_together import Together
|
5 |
+
from langchain.llms import huggingface_hub
|
6 |
+
import os
|
7 |
+
from langchain.retrievers.document_compressors import EmbeddingsFilter
|
8 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
9 |
+
from langchain.memory import ConversationBufferWindowMemory
|
10 |
+
from langchain.chains import ConversationalRetrievalChain
|
11 |
+
import streamlit as st
|
12 |
+
import time
|
13 |
+
st.set_page_config(page_title="Medical Chat")
|
14 |
+
|
15 |
+
def reset_conversation():
|
16 |
+
st.session_state.messages = []
|
17 |
+
st.session_state.memory.clear()
|
18 |
+
|
19 |
+
if "messages" not in st.session_state:
|
20 |
+
st.session_state.messages = []
|
21 |
+
|
22 |
+
if "memory" not in st.session_state:
|
23 |
+
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
|
24 |
+
|
25 |
+
embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True})
|
26 |
+
db = FAISS.load_local("medchat_db", embeddings)
|
27 |
+
db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
|
28 |
+
|
29 |
+
custom_prompt_template = """Follow these instructions clearly. This is a chat template and you are a medical practitioner chat bot who provides correct medical information. The way you speak should be in a doctor's perspective. You are given the following pieces of information to answer the user's question correctly. You will be given context, chat history and the question. Choose only the required context based on the user's question. If the question is not related to the chat history, then don't use the history. Use chat history when required for similar related questions. While searching for the relevant information always give priority to the context given. If there are multiple medicines same medicine name and different strength mention them. Always take the context related only to the question. Use your won knowledge base and answer the question when the context is not related to the user's question. Utilize the provided knowledge base and search for relevant information from the context. Follow the user's question and the format closely. The answer should be abstract and concise. Understand all the context given here and generate only the answer, don't repeat the chat template in the answer. If you don't know the answer, just say that you don't know, don't try to make up your own questions and answers. Add bullet points and bold text using markdown in the required area if needed, to make it more pleasing to eyes.
|
30 |
+
|
31 |
+
CONTEXT: {context}
|
32 |
+
|
33 |
+
CHAT HISTORY: {chat_history}
|
34 |
+
|
35 |
+
QUESTION: {question}
|
36 |
+
|
37 |
+
ANSWER:
|
38 |
+
"""
|
39 |
+
|
40 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
41 |
+
input_variables=['context', 'question', 'chat_history'])
|
42 |
+
|
43 |
+
|
44 |
+
llm = huggingface_hub(
|
45 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
46 |
+
model_kwargs={"temperature": 0.7, "max_length": 512}
|
47 |
+
)
|
48 |
+
|
49 |
+
embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.80)
|
50 |
+
|
51 |
+
qa = ConversationalRetrievalChain.from_llm(
|
52 |
+
llm=llm,
|
53 |
+
memory=st.session_state.memory,
|
54 |
+
retriever=db_retriever,
|
55 |
+
combine_docs_chain_kwargs={'prompt': prompt}
|
56 |
+
)
|
57 |
+
|
58 |
+
for message in st.session_state.messages:
|
59 |
+
with st.chat_message(message.get("role")):
|
60 |
+
st.write(message.get("content"))
|
61 |
+
|
62 |
+
input_prompt = st.chat_input("Say something")
|
63 |
+
|
64 |
+
if input_prompt:
|
65 |
+
with st.chat_message("user"):
|
66 |
+
st.write(input_prompt)
|
67 |
+
|
68 |
+
st.session_state.messages.append({"role":"user","content":input_prompt})
|
69 |
+
|
70 |
+
with st.chat_message("assistant"):
|
71 |
+
with st.status("Thinking ...",expanded=True):
|
72 |
+
result = qa.invoke(input=input_prompt)
|
73 |
+
|
74 |
+
message_placeholder = st.empty()
|
75 |
+
|
76 |
+
full_response = "**_Note: Information provided may be inaccurate. Consult a qualified doctor for accurate advice._** \n\n\n"
|
77 |
+
for chunk in result["answer"]:
|
78 |
+
full_response+=chunk
|
79 |
+
time.sleep(0.02)
|
80 |
+
|
81 |
+
message_placeholder.markdown(full_response+" ▌")
|
82 |
+
st.button('Reset All Chat', on_click=reset_conversation)
|
83 |
+
|
84 |
+
st.session_state.messages.append({"role":"assistant","content":result["answer"]})
|
medchat_db/index.faiss
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d2c1c68f5f5560911f259db25dcfcf6d6e405191cf220ac4625b111450a3130c
|
3 |
+
size 162260013
|
medchat_db/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2ee2c1b59e0ad1817fbb6a62ebf87ed6d9cd2c441542693ae97a4776e3b22f70
|
3 |
+
size 55123974
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
transformers
|
3 |
+
sentence-transformers
|
4 |
+
accelerate
|
5 |
+
faiss-cpu
|
6 |
+
streamlit
|
7 |
+
langchain-together
|
8 |
+
einops
|