Manail Fatima commited on
Commit
78362e2
·
1 Parent(s): 18544ec

Initial Docker Space

Browse files
Files changed (14) hide show
  1. .gitignore +207 -0
  2. Dockerfile +0 -0
  3. LICENSE +201 -0
  4. app.py +87 -0
  5. requirements.txt +9 -0
  6. research/trials.ipynb +0 -0
  7. setup.py +12 -0
  8. src/__init.py +0 -0
  9. src/helper.py +48 -0
  10. src/prompt.py +9 -0
  11. static/style.css +224 -0
  12. store_index.py +71 -0
  13. template.sh +13 -0
  14. templates/chat.html +81 -0
.gitignore ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
Dockerfile ADDED
File without changes
LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, jsonify, request
2
+ from src.helper import download_embeddings
3
+ from langchain_pinecone import PineconeVectorStore
4
+ from langchain_openai import ChatOpenAI
5
+ from langchain.chains import create_retrieval_chain
6
+ from langchain.chains.combine_documents import create_stuff_documents_chain
7
+ from langchain_core.prompts import ChatPromptTemplate
8
+ from langchain.schema import Document
9
+ from dotenv import load_dotenv
10
+ from src.prompt import *
11
+ import os
12
+ from transformers import pipeline
13
+ from langchain_community.llms import HuggingFacePipeline
14
+
15
+ # Cleaning function for model output
16
+ import re
17
+
18
+ def clean_text(text):
19
+ text = re.sub(r'-\n', '', text)
20
+ text = text.replace('\n', ' ')
21
+ text = re.sub(r' +', ' ', text)
22
+ text = re.sub(r'/C\d+', '', text)
23
+ # Remove lines with 'Reproduced by permission' or 'GALE ENCYCLOPEDIA OF MEDICINE'
24
+ text = re.sub(r'Reproduced by permission[^\.\n]*[\.]?', '', text, flags=re.IGNORECASE)
25
+ text = re.sub(r'GALE ENCYCLOPEDIA OF MEDICINE[^\.\n]*[\.]?', '', text, flags=re.IGNORECASE)
26
+ text = re.sub(r'Researchers,? Inc[^\.\n]*[\.]?', '', text, flags=re.IGNORECASE)
27
+ return text.strip()
28
+
29
+
30
+ app = Flask(__name__)
31
+
32
+ # Add a greeting document to the Pinecone index
33
+
34
+
35
+
36
+ load_dotenv()
37
+
38
+ Pinecone_API_KEY = os.getenv("Pinecone_API_KEY")
39
+ os.environ["PINECONE_API_KEY"] = Pinecone_API_KEY
40
+
41
+ embeddings = download_embeddings()
42
+
43
+ index_name = "doctor-ai"
44
+ docsearch=PineconeVectorStore.from_existing_index(
45
+ index_name=index_name,
46
+ embedding=embeddings
47
+ )
48
+
49
+
50
+ retriever= docsearch.as_retriever(search_type="similarity",search_kwargs={"k": 2})
51
+
52
+
53
+ generator = pipeline( "text2text-generation", model="google/flan-t5-large", device=-1)
54
+ chat_model = HuggingFacePipeline(pipeline=generator)
55
+ #prompt= ChatPromptTemplate.from_messages(
56
+ # [
57
+ # ("system", system_prompt),
58
+ # ("human", "{input}")
59
+ # ]
60
+ #)
61
+
62
+
63
+ prompt = ChatPromptTemplate.from_template(system_prompt)
64
+ question_answer_chain = create_stuff_documents_chain(chat_model, prompt)
65
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
66
+
67
+ #question_answer_chain = create_stuff_documents_chain(chat_model, prompt)
68
+ #rag_chain = create_retrieval_chain(retriever, question_answer_chain)
69
+
70
+
71
+ @app.route('/')
72
+ def index():
73
+ return render_template('chat.html')
74
+
75
+ @app.route('/get', methods=["GET","POST"])
76
+ def chat():
77
+ msg=request.form["msg"]
78
+ input=msg
79
+ print(input)
80
+ response = rag_chain.invoke({"input": input})
81
+ cleaned_answer = clean_text(response["answer"])
82
+ print("Response:", cleaned_answer)
83
+ return str(cleaned_answer)
84
+
85
+
86
+ if __name__ == '__main__':
87
+ app.run(host='0.0.0.0', port=8000, debug=True)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.3.26
2
+ flask==3.1.1
3
+ sentence-transformers==4.1.0
4
+ pypdf==5.6.1
5
+ python-dotenv==1.1.0
6
+ langchain-pinecone==0.2.8
7
+ langchain-openai==0.3.24
8
+ langchain-community==0.3.26
9
+ -e .
research/trials.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
setup.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import find_packages, setup
2
+
3
+ setup(
4
+ name="Medical-Chatbot",
5
+ version="0.1.0",
6
+ author="Manail Fatima",
7
+ author_email="manailfatima2@gmail.com",
8
+
9
+ packages=find_packages(where="src"),
10
+ package_dir={"": "src"},
11
+ install_requires=[]
12
+ )
src/__init.py ADDED
File without changes
src/helper.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from typing import List
4
+ from langchain.schema import Document
5
+ from langchain_community.embeddings import HuggingFaceEmbeddings
6
+
7
+
8
+ def load_pdf_files(data):
9
+ loader = DirectoryLoader(
10
+ data,
11
+ glob="*.pdf",
12
+ loader_cls=PyPDFLoader,
13
+ show_progress=True
14
+ )
15
+
16
+ documents = loader.load()
17
+ return documents
18
+
19
+ def filter_to_minimal_docs(docs: List[Document]) -> List[Document]:
20
+ minimal_docs : List[Document]= []
21
+ for doc in docs:
22
+ source = doc.metadata.get("source")
23
+ minimal_docs.append(
24
+ Document(
25
+ page_content=doc.page_content,
26
+ metadata={
27
+ "source": source}))
28
+ return minimal_docs
29
+
30
+ def text_splitter(minimal_docs):
31
+ text_splitter = RecursiveCharacterTextSplitter(
32
+ chunk_size=500,
33
+ chunk_overlap=20
34
+ )
35
+ text_chunks=text_splitter.split_documents(minimal_docs)
36
+ return text_chunks
37
+
38
+ def download_embeddings():
39
+
40
+ model_name = "sentence-transformers/all-MiniLM-L6-v2"
41
+ embeddings= HuggingFaceEmbeddings(
42
+ model_name=model_name,
43
+
44
+ )
45
+ return embeddings
46
+ embeddings = download_embeddings()
47
+
48
+
src/prompt.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ system_prompt = (
2
+ "You are a helpful medical assistant. Use the following context to answer the user's question conversationally. "
3
+ "If the answer is not in the context, say 'I don't know.' "
4
+ "Please answer concisely and do not repeat information."
5
+ "If the user says hi, hello, or hey, reply with 'Hello, I am a helpful medical assistant, how may I help you today?'.\n\n"
6
+ "Context:\n{context}\n"
7
+ "Question: {input}\n"
8
+ "Answer in 2-3 sentences."
9
+ )
static/style.css ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ body,html{
3
+ height: 100%;
4
+ margin: 0;
5
+ background: rgb(44, 47, 59);
6
+ background: -webkit-linear-gradient(to right, rgb(40, 59, 34), rgb(54, 60, 70), rgb(32, 32, 43));
7
+ background: linear-gradient(to right, rgb(38, 51, 61), rgb(50, 55, 65), rgb(33, 33, 78));
8
+ }
9
+
10
+ .chat{
11
+ margin-top: auto;
12
+ margin-bottom: auto;
13
+ }
14
+ .card{
15
+ height: 500px;
16
+ border-radius: 15px !important;
17
+ background-color: rgba(0,0,0,0.4) !important;
18
+ }
19
+ .contacts_body{
20
+ padding: 0.75rem 0 !important;
21
+ overflow-y: auto;
22
+ white-space: nowrap;
23
+ }
24
+ .msg_card_body{
25
+ overflow-y: auto;
26
+ }
27
+ .card-header{
28
+ border-radius: 15px 15px 0 0 !important;
29
+ border-bottom: 0 !important;
30
+ }
31
+ .card-footer{
32
+ border-radius: 0 0 15px 15px !important;
33
+ border-top: 0 !important;
34
+ }
35
+ .container{
36
+ align-content: center;
37
+ }
38
+ .search{
39
+ border-radius: 15px 0 0 15px !important;
40
+ background-color: rgba(0,0,0,0.3) !important;
41
+ border:0 !important;
42
+ color:white !important;
43
+ }
44
+ .search:focus{
45
+ box-shadow:none !important;
46
+ outline:0px !important;
47
+ }
48
+ .type_msg{
49
+ background-color: rgba(0,0,0,0.3) !important;
50
+ border:0 !important;
51
+ color:white !important;
52
+ height: 60px !important;
53
+ overflow-y: auto;
54
+ }
55
+ .type_msg:focus{
56
+ box-shadow:none !important;
57
+ outline:0px !important;
58
+ }
59
+ .attach_btn{
60
+ border-radius: 15px 0 0 15px !important;
61
+ background-color: rgba(0,0,0,0.3) !important;
62
+ border:0 !important;
63
+ color: white !important;
64
+ cursor: pointer;
65
+ }
66
+ .send_btn{
67
+ border-radius: 0 15px 15px 0 !important;
68
+ background-color: rgba(0,0,0,0.3) !important;
69
+ border:0 !important;
70
+ color: white !important;
71
+ cursor: pointer;
72
+ }
73
+ .search_btn{
74
+ border-radius: 0 15px 15px 0 !important;
75
+ background-color: rgba(0,0,0,0.3) !important;
76
+ border:0 !important;
77
+ color: white !important;
78
+ cursor: pointer;
79
+ }
80
+ .contacts{
81
+ list-style: none;
82
+ padding: 0;
83
+ }
84
+ .contacts li{
85
+ width: 100% !important;
86
+ padding: 5px 10px;
87
+ margin-bottom: 15px !important;
88
+ }
89
+ .active{
90
+ background-color: rgba(0,0,0,0.3);
91
+ }
92
+ .user_img{
93
+ height: 70px;
94
+ width: 70px;
95
+ border:1.5px solid #f5f6fa;
96
+
97
+ }
98
+ .user_img_msg{
99
+ height: 40px;
100
+ width: 40px;
101
+ border:1.5px solid #f5f6fa;
102
+
103
+ }
104
+ .img_cont{
105
+ position: relative;
106
+ height: 70px;
107
+ width: 70px;
108
+ }
109
+ .img_cont_msg{
110
+ height: 40px;
111
+ width: 40px;
112
+ }
113
+ .online_icon{
114
+ position: absolute;
115
+ height: 15px;
116
+ width:15px;
117
+ background-color: #4cd137;
118
+ border-radius: 50%;
119
+ bottom: 0.2em;
120
+ right: 0.4em;
121
+ border:1.5px solid white;
122
+ }
123
+ .offline{
124
+ background-color: #c23616 !important;
125
+ }
126
+ .user_info{
127
+ margin-top: auto;
128
+ margin-bottom: auto;
129
+ margin-left: 15px;
130
+ }
131
+ .user_info span{
132
+ font-size: 20px;
133
+ color: white;
134
+ }
135
+ .user_info p{
136
+ font-size: 10px;
137
+ color: rgba(255,255,255,0.6);
138
+ }
139
+ .video_cam{
140
+ margin-left: 50px;
141
+ margin-top: 5px;
142
+ }
143
+ .video_cam span{
144
+ color: white;
145
+ font-size: 20px;
146
+ cursor: pointer;
147
+ margin-right: 20px;
148
+ }
149
+ .msg_cotainer{
150
+ margin-top: auto;
151
+ margin-bottom: auto;
152
+ margin-left: 10px;
153
+ border-radius: 25px;
154
+ background-color: rgb(82, 172, 255);
155
+ padding: 10px;
156
+ position: relative;
157
+ }
158
+ .msg_cotainer_send{
159
+ margin-top: auto;
160
+ margin-bottom: auto;
161
+ margin-right: 10px;
162
+ border-radius: 25px;
163
+ background-color: #58cc71;
164
+ padding: 10px;
165
+ position: relative;
166
+ }
167
+ .msg_time{
168
+ position: absolute;
169
+ left: 0;
170
+ bottom: -15px;
171
+ color: rgba(255,255,255,0.5);
172
+ font-size: 10px;
173
+ }
174
+ .msg_time_send{
175
+ position: absolute;
176
+ right:0;
177
+ bottom: -15px;
178
+ color: rgba(255,255,255,0.5);
179
+ font-size: 10px;
180
+ }
181
+ .msg_head{
182
+ position: relative;
183
+ }
184
+ #action_menu_btn{
185
+ position: absolute;
186
+ right: 10px;
187
+ top: 10px;
188
+ color: white;
189
+ cursor: pointer;
190
+ font-size: 20px;
191
+ }
192
+ .action_menu{
193
+ z-index: 1;
194
+ position: absolute;
195
+ padding: 15px 0;
196
+ background-color: rgba(0,0,0,0.5);
197
+ color: white;
198
+ border-radius: 15px;
199
+ top: 30px;
200
+ right: 15px;
201
+ display: none;
202
+ }
203
+ .action_menu ul{
204
+ list-style: none;
205
+ padding: 0;
206
+ margin: 0;
207
+ }
208
+ .action_menu ul li{
209
+ width: 100%;
210
+ padding: 10px 15px;
211
+ margin-bottom: 5px;
212
+ }
213
+ .action_menu ul li i{
214
+ padding-right: 10px;
215
+ }
216
+ .action_menu ul li:hover{
217
+ cursor: pointer;
218
+ background-color: rgba(0,0,0,0.2);
219
+ }
220
+ @media(max-width: 576px){
221
+ .contacts_card{
222
+ margin-bottom: 15px !important;
223
+ }
224
+ }
store_index.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_pinecone import PineconeVectorStore
2
+ from datasets import load_dataset
3
+ from langchain.schema import Document
4
+ from src.helper import load_pdf_files, filter_to_minimal_docs, text_splitter, download_embeddings
5
+ from pinecone import Pinecone
6
+ from pinecone import ServerlessSpec
7
+ from dotenv import load_dotenv
8
+ load_dotenv()
9
+ import os
10
+
11
+
12
+ Pinecone_API_KEY = os.getenv("Pinecone_API_KEY")
13
+ os.environ["PINECONE_API_KEY"] = Pinecone_API_KEY
14
+
15
+
16
+ #extracted_data= load_pdf_files("data")
17
+ #minimal_docs = filter_to_minimal_docs(extracted_data)
18
+ #text_chunks = text_splitter(minimal_docs)
19
+
20
+
21
+ embeddings = download_embeddings()
22
+
23
+ pinecone_API_key=Pinecone_API_KEY
24
+ pc = Pinecone(api_key=pinecone_API_key)
25
+
26
+ index_name = "doctor-ai"
27
+ if not pc.has_index(index_name):
28
+ pc.create_index(
29
+ name=index_name,
30
+ dimension=384,
31
+ metric="cosine",
32
+ spec=ServerlessSpec(
33
+ cloud="aws",
34
+ region="us-east-1"
35
+ ))
36
+ index = pc.Index(index_name)
37
+
38
+
39
+
40
+ dataset = load_dataset("lavita/ChatDoctor-HealthCareMagic-100k")
41
+ docs = []
42
+ for item in dataset["train"]:
43
+ question = item.get("instruction", "") + " " + item.get("input", "")
44
+ answer = item.get("output", "")
45
+ docs.append(Document(
46
+ page_content=f"Q: {question}\nA: {answer}",
47
+ metadata={"source": "huggingface_lavita/ChatDoctor-HealthCareMagic-100k"}
48
+ ))
49
+
50
+
51
+
52
+ #docssearch=PineconeVectorStore.from_documents(
53
+ # documents=text_chunks,
54
+ # embedding=embeddings,
55
+ # index_name=index_name
56
+ #)
57
+
58
+ docsearch=PineconeVectorStore.from_existing_index(
59
+ index_name=index_name,
60
+ embedding=embeddings
61
+ )
62
+
63
+
64
+ def chunked_upload(docs, batch_size=50):
65
+ for i in range(0, len(docs), batch_size):
66
+ batch = docs[i:i+batch_size]
67
+ docsearch.add_documents(batch)
68
+ chunked_upload(docs, batch_size=50)
69
+
70
+
71
+ docsearch.add_documents(docs)
template.sh ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mkdir -p src
2
+ mkdir -p research
3
+
4
+ touch src/__init.py
5
+ touch src/helper.py
6
+ touch src/prompt.py
7
+ touch .env
8
+ touch setup.py
9
+ touch app.py
10
+ touch research/trials.ipynb
11
+ touch requirements.txt
12
+
13
+ echo "Created Successfully"
templates/chat.html ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
2
+ <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
3
+ <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
4
+
5
+ <!DOCTYPE html>
6
+ <html>
7
+ <head>
8
+ <title>DoctorAI</title>
9
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
10
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
11
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
12
+ <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css')}}"/>
13
+ </head>
14
+
15
+
16
+ <body>
17
+ <div class="container-fluid h-100">
18
+ <div class="row justify-content-center h-100">
19
+ <div class="col-md-8 col-xl-6 chat">
20
+ <div class="card">
21
+ <div class="card-header msg_head">
22
+ <div class="d-flex bd-highlight">
23
+ <div class="img_cont">
24
+ <img src="https://cdn-icons-png.flaticon.com/512/387/387569.png" class="rounded-circle user_img">
25
+ <!-- <img src="https://www.prdistribution.com/spirit/uploads/pressreleases/2019/newsreleases/d83341deb75c4c4f6b113f27b1e42cd8-chatbot-florence-already-helps-thousands-of-patients-to-remember-their-medication.png" class="rounded-circle user_img"> -->
26
+ <span class="online_icon"></span>
27
+ </div>
28
+ <div class="user_info">
29
+ <span>DoctorAI</span>
30
+ <p>Ask me anything!</p>
31
+ </div>
32
+ </div>
33
+ </div>
34
+ <div id="messageFormeight" class="card-body msg_card_body">
35
+
36
+
37
+ </div>
38
+ <div class="card-footer">
39
+ <form id="messageArea" class="input-group">
40
+ <input type="text" id="text" name="msg" placeholder="Type your message..." autocomplete="off" class="form-control type_msg" required/>
41
+ <div class="input-group-append">
42
+ <button type="submit" id="send" class="input-group-text send_btn"><i class="fas fa-location-arrow"></i></button>
43
+ </div>
44
+ </form>
45
+ </div>
46
+ </div>
47
+ </div>
48
+ </div>
49
+ </div>
50
+
51
+ <script>
52
+ $(document).ready(function() {
53
+ $("#messageArea").on("submit", function(event) {
54
+ const date = new Date();
55
+ const hour = date.getHours();
56
+ const minute = date.getMinutes();
57
+ const str_time = hour+":"+minute;
58
+ var rawText = $("#text").val();
59
+
60
+ var userHtml = '<div class="d-flex justify-content-end mb-4"><div class="msg_cotainer_send">' + rawText + '<span class="msg_time_send">'+ str_time + '</span></div><div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div></div>';
61
+
62
+ $("#text").val("");
63
+ $("#messageFormeight").append(userHtml);
64
+
65
+ $.ajax({
66
+ data: {
67
+ msg: rawText,
68
+ },
69
+ type: "POST",
70
+ url: "/get",
71
+ }).done(function(data) {
72
+ var botHtml = '<div class="d-flex justify-content-start mb-4"><div class="img_cont_msg"><img src="https://cdn-icons-png.flaticon.com/512/387/387569.png" class="rounded-circle user_img_msg"></div><div class="msg_cotainer">' + data + '<span class="msg_time">' + str_time + '</span></div></div>';
73
+ $("#messageFormeight").append($.parseHTML(botHtml));
74
+ });
75
+ event.preventDefault();
76
+ });
77
+ });
78
+ </script>
79
+
80
+ </body>
81
+ </html>