vishalj0501 commited on
Commit
2b4408c
·
1 Parent(s): 60b407a
Files changed (2) hide show
  1. app.py +137 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import re
3
+
4
+ from langchain.embeddings.openai import OpenAIEmbeddings
5
+ from langchain.vectorstores import Pinecone
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.chains import RetrievalQA
8
+ from langchain.output_parsers import OutputFixingParser
9
+ from langchain.schema import OutputParserException
10
+
11
+ import pinecone
12
+ import os
13
+ from dotenv import load_dotenv
14
+ import json
15
+
16
+ model_name = 'text-embedding-ada-002'
17
+
18
+ load_dotenv()
19
+
20
+
21
+
22
+
23
+ st.title("ICD DEMO")
24
+
25
+
26
+
27
+ gpt_api_key = st.text_input(label='Enter GPT KEY',placeholder="Enter GPT-API-KEY",label_visibility="collapsed")
28
+ pinecone_api_key=st.text_input(label='Enter PINECONE KEY',placeholder="Enter Pinecone API-KEY",label_visibility="collapsed")
29
+
30
+ embeddings = OpenAIEmbeddings(
31
+ model=model_name,
32
+ openai_api_key=gpt_api_key,
33
+ )
34
+
35
+
36
+ pinecone.init(
37
+ api_key=pinecone_api_key,
38
+ environment='gcp-starter',
39
+ )
40
+ index = pinecone.Index('vectordb')
41
+
42
+
43
+ vectorstore = Pinecone(
44
+ index=index,
45
+ embedding_function=embeddings.embed_query,
46
+ text_key='text',
47
+ )
48
+
49
+
50
+
51
+ def get_response(instruction, query):
52
+ """
53
+ This function takes in an instruction and a query, and returns a response and a list of results.
54
+ instruction: str
55
+ query: str
56
+ Returns: str, list
57
+ """
58
+
59
+ results = vectorstore.similarity_search(query, k=5)
60
+ llm = ChatOpenAI(
61
+ openai_api_key=os.environ.get('OPENAI_API_KEY'),
62
+ model_name='gpt-3.5-turbo',
63
+ temperature=0.0,
64
+ request_timeout=1000
65
+ )
66
+ qa = RetrievalQA.from_chain_type(
67
+ llm=llm,
68
+ chain_type='stuff',
69
+ retriever=vectorstore.as_retriever(),
70
+ )
71
+ response = qa.run(str(instruction) + str(query))
72
+
73
+ return response, results
74
+
75
+
76
+ instruction = """Given the progress notes below, your task is to carefully identify and list all the diagnosis, paying attention to the specific details such as laterality, severity, type, cause, and progression stage, that could influence to find the corresponding International Classification of Diseases (ICD) codes.
77
+ Please exclude any conditions that the patient explicitly denies (e.g., phrases like 'denies,' 'negative for,' etc).
78
+ Following the extraction process, compile the identified conditions in a list, prioritizing conditions of higher severity or urgency at the top, and present the data in a JSON format in descending order based on their priority or severity.
79
+ For example, below is the sample output:
80
+ {
81
+ "Diseases": [
82
+ {
83
+ "Disease": "Fatty Liver",
84
+ "Laterality": "Not specified",
85
+ "Severity": "Not specified",
86
+ "Type": "Not specified",
87
+ "Cause": "Alcholic",
88
+ "Progression Stage": "Not specified",
89
+ "ICD" : "<ICD for Fattly Liver>"
90
+ },
91
+ {
92
+ "Disease": "Leg Fracture",
93
+ "Laterality": "Right",
94
+ "Severity": "Not specified",
95
+ "Type": "Not specified",
96
+ "Cause": "Accident",
97
+ "Progression Stage": "Not specified",
98
+ "ICD" : "<ICD for Leg Fracture>
99
+ }
100
+ ]
101
+ }
102
+ """
103
+
104
+ def iterate_output(lis):
105
+ text=""
106
+ for i in lis:
107
+ text += str(json.dumps(i, indent = 3))+",\n"
108
+ return text
109
+
110
+ def stringFormatterByAlok(inputString):
111
+ newData = ""
112
+ for i in inputString:
113
+ newData += i + "\n"
114
+ return newData
115
+
116
+
117
+ if "messages" not in st.session_state:
118
+ st.session_state.messages = []
119
+
120
+ for message in st.session_state.messages:
121
+ with st.chat_message(message["role"]):
122
+ st.markdown(message["content"])
123
+
124
+
125
+ if prompt := st.chat_input("Enter the progress note here"):
126
+ st.chat_message("user").markdown(prompt)
127
+ st.session_state.messages.append({"role": "user", "content": prompt})
128
+ response = get_response(instruction, prompt)
129
+
130
+ with st.chat_message("assistant"):
131
+ output=json.loads(response[0])["Diseases"]
132
+ output_json_dump = json.dumps(output, indent=4)
133
+
134
+ for i in eval(output_json_dump):
135
+ st.write(i)
136
+
137
+ st.session_state.messages.append({"role": "assistant", "content": response})
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ langchain==0.0.315
2
+ pinecone_client==2.2.4
3
+ python-dotenv==1.0.0
4
+ streamlit==1.24.1