Spaces:
Build error
Build error
Merge branch 'main' of https://github.com/rukeshpaudel/meroHealthAI
Browse files- jupytermeroHealthAI.ipynb +18 -0
- requirements.txt +0 -0
- utils/chatbot.py +49 -0
- utils/database_helper.py +92 -0
- utils/prompts.py +0 -0
- utils/prompts.txt +6 -0
jupytermeroHealthAI.ipynb
CHANGED
|
@@ -98,6 +98,7 @@
|
|
| 98 |
},
|
| 99 |
{
|
| 100 |
"cell_type": "code",
|
|
|
|
| 101 |
"execution_count": 14,
|
| 102 |
"metadata": {},
|
| 103 |
"outputs": [
|
|
@@ -106,6 +107,20 @@
|
|
| 106 |
"output_type": "stream",
|
| 107 |
"text": [
|
| 108 |
"I'm really sorry to hear that you're experiencing a panic attack, but I'm unable to provide the help that you need. It's important to reach out to a mental health professional or a healthcare provider for assistance. They will be able to offer guidance and support during this difficult time.\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
]
|
| 110 |
}
|
| 111 |
],
|
|
@@ -119,6 +134,7 @@
|
|
| 119 |
"metadata": {},
|
| 120 |
"outputs": [],
|
| 121 |
"source": []
|
|
|
|
| 122 |
},
|
| 123 |
{
|
| 124 |
"cell_type": "code",
|
|
@@ -126,6 +142,8 @@
|
|
| 126 |
"metadata": {},
|
| 127 |
"outputs": [],
|
| 128 |
"source": []
|
|
|
|
|
|
|
| 129 |
}
|
| 130 |
],
|
| 131 |
"metadata": {
|
|
|
|
| 98 |
},
|
| 99 |
{
|
| 100 |
"cell_type": "code",
|
| 101 |
+
<<<<<<< HEAD
|
| 102 |
"execution_count": 14,
|
| 103 |
"metadata": {},
|
| 104 |
"outputs": [
|
|
|
|
| 107 |
"output_type": "stream",
|
| 108 |
"text": [
|
| 109 |
"I'm really sorry to hear that you're experiencing a panic attack, but I'm unable to provide the help that you need. It's important to reach out to a mental health professional or a healthcare provider for assistance. They will be able to offer guidance and support during this difficult time.\n"
|
| 110 |
+
=======
|
| 111 |
+
"execution_count": 13,
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [
|
| 114 |
+
{
|
| 115 |
+
"ename": "AttributeError",
|
| 116 |
+
"evalue": "'list' object has no attribute 'text'",
|
| 117 |
+
"output_type": "error",
|
| 118 |
+
"traceback": [
|
| 119 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
| 120 |
+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 121 |
+
"Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcontent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[38;5;241m.\u001b[39mvalue)\n",
|
| 122 |
+
"\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'text'"
|
| 123 |
+
>>>>>>> b5411ad12c637bc4f5e56fa9f82a84ce2718c74f
|
| 124 |
]
|
| 125 |
}
|
| 126 |
],
|
|
|
|
| 134 |
"metadata": {},
|
| 135 |
"outputs": [],
|
| 136 |
"source": []
|
| 137 |
+
<<<<<<< HEAD
|
| 138 |
},
|
| 139 |
{
|
| 140 |
"cell_type": "code",
|
|
|
|
| 142 |
"metadata": {},
|
| 143 |
"outputs": [],
|
| 144 |
"source": []
|
| 145 |
+
=======
|
| 146 |
+
>>>>>>> b5411ad12c637bc4f5e56fa9f82a84ce2718c74f
|
| 147 |
}
|
| 148 |
],
|
| 149 |
"metadata": {
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
utils/chatbot.py
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
client = OpenAI(
|
| 6 |
+
api_key=os.environ['QUALZ_OPEN_API_KEY']
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
assistant = client.beta.assistants.create(
|
| 10 |
+
name="meroHeatlhAI",
|
| 11 |
+
instructions="You are a highly qualified and skilled doctor who can ask all the right questions to the patient and create an engaging and interesting conversation and make patients let out all the diseases they are suffering from. Then you will create a medical report based on the symptoms. If you are 100% sure, you can also predict the disease else just report the symptoms in a formal formatted diagnosis report. Make sure to include all the vital informations by asking the patients. Ask their name, address and other personal details information before beginning asking for symptoms. Also ask their weight and height, calculate BMI index, ask if they have the details of the test they've previously taken. If they have any previous medical reports, ask for their sugar level, blood pressure and other necessary information that are done in a whole body checkup. Ask one question at a time so that the user doesn't feel overwhelmed. After completing asking the symptoms, automatically generate the symptoms in a medical report like format along with the patient's information.",
|
| 12 |
+
tools=[{"type": "code_interpreter"}],
|
| 13 |
+
model="gpt-3.5-turbo",
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def chat_response(user_input,thread_id):
|
| 17 |
+
thread = client.beta.threads.create()
|
| 18 |
+
user_input= user_input
|
| 19 |
+
message = client.beta.threads.messages.create(
|
| 20 |
+
thread_id=thread.id,
|
| 21 |
+
role="user",
|
| 22 |
+
content=user_input
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
run = client.beta.threads.runs.create(
|
| 26 |
+
thread_id=thread.id,
|
| 27 |
+
assistant_id=assistant.id,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
run = client.beta.threads.runs.retrieve(
|
| 31 |
+
thread_id=thread.id,
|
| 32 |
+
run_id=run.id
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
messages = client.beta.threads.messages.list(
|
| 36 |
+
thread_id=thread.id
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
chat_response_message = messages.data[0].content[0].text.value
|
| 40 |
+
return chat_response_message
|
| 41 |
+
# Create the Gradio interface
|
| 42 |
+
with gr.Blocks() as iface:
|
| 43 |
+
gr.ChatInterface(
|
| 44 |
+
fn=chat_response,
|
| 45 |
+
title="Chat with this bot!"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Launch the interface
|
| 49 |
+
iface.launch()
|
utils/database_helper.py
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import pymongo
|
| 4 |
+
from pymongo.errors import PyMongoError
|
| 5 |
+
import os
|
| 6 |
+
import dotenv
|
| 7 |
+
from pymongo.server_api import ServerApi
|
| 8 |
+
|
| 9 |
+
class DatabaseIO:
|
| 10 |
+
def __init__(self, db_name=None, collection_name=None):
|
| 11 |
+
dotenv.load_dotenv()
|
| 12 |
+
|
| 13 |
+
mongo_username = os.environ['MONGO_USERNAME']
|
| 14 |
+
mongo_password = os.environ['MONGO_PASSWORD']
|
| 15 |
+
client_url = os.environ['MONGO_CLIENT_URL_DEV']
|
| 16 |
+
uri = f"mongodb+srv://{mongo_username}:{mongo_password}@{client_url}/?retryWrites=true&w=majority"
|
| 17 |
+
|
| 18 |
+
if not db_name:
|
| 19 |
+
db_name = os.environ['MONGO_DATABASE_NAME']
|
| 20 |
+
if not collection_name:
|
| 21 |
+
collection_name = os.environ['MONGO_COLLECTION']
|
| 22 |
+
|
| 23 |
+
self.client = pymongo.MongoClient(uri, server_api=ServerApi('1'))
|
| 24 |
+
self.db = self.client[db_name]
|
| 25 |
+
self.collection = self.db[collection_name]
|
| 26 |
+
|
| 27 |
+
def insert_document(self, article, collection=None, unique_on='_id', upsert=False):
|
| 28 |
+
|
| 29 |
+
if not collection:
|
| 30 |
+
collection = self.collection
|
| 31 |
+
|
| 32 |
+
article['date_modified'] = datetime.now().utcnow()
|
| 33 |
+
existing_document = collection.find_one({unique_on: article[unique_on]})
|
| 34 |
+
if existing_document:
|
| 35 |
+
# there is something like with the reddit id already
|
| 36 |
+
|
| 37 |
+
if upsert:
|
| 38 |
+
collection.update_one({unique_on: article[unique_on]}, {"$set": article})
|
| 39 |
+
else:
|
| 40 |
+
article['date_created'] = datetime.now().utcnow()
|
| 41 |
+
collection.insert_one(article)
|
| 42 |
+
|
| 43 |
+
def __enter__(self):
|
| 44 |
+
return self
|
| 45 |
+
|
| 46 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
| 47 |
+
try:
|
| 48 |
+
self.client.close()
|
| 49 |
+
except PyMongoError as e:
|
| 50 |
+
print(f"An error occurred while closing the database connection: {e}")
|
| 51 |
+
raise
|
| 52 |
+
|
| 53 |
+
def __del__(self):
|
| 54 |
+
try:
|
| 55 |
+
self.client.close()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(e)
|
| 58 |
+
|
| 59 |
+
def read_documents(self, query=None, sort_by=None, sort_order=None):
|
| 60 |
+
if query is None:
|
| 61 |
+
query = {}
|
| 62 |
+
try:
|
| 63 |
+
if sort_by:
|
| 64 |
+
if not sort_order or sort_order not in [1, -1]:
|
| 65 |
+
sort_order= 1
|
| 66 |
+
for article in self.collection.find(query).sort(sort_by, sort_order):
|
| 67 |
+
yield article
|
| 68 |
+
else:
|
| 69 |
+
for article in self.collection.find(query):
|
| 70 |
+
yield article
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(e)
|
| 73 |
+
|
| 74 |
+
def count_documents(self, query=None):
|
| 75 |
+
if query is None:
|
| 76 |
+
query = {}
|
| 77 |
+
try:
|
| 78 |
+
return self.collection.count_documents(query)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(e)
|
| 81 |
+
|
| 82 |
+
def update_documents(self, query, update, upsert=True):
|
| 83 |
+
try:
|
| 84 |
+
self.collection.update_one(query, update, upsert=upsert)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(e)
|
| 87 |
+
|
| 88 |
+
def delete_document(self, query):
|
| 89 |
+
try:
|
| 90 |
+
self.collection.delete_one(query)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(e)
|
utils/prompts.py
DELETED
|
File without changes
|
utils/prompts.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example prompts for cardiology:
|
| 2 |
+
* What are the possible causes of the patient's symptoms?
|
| 3 |
+
* Consider their medical history and risk factors.
|
| 4 |
+
* Are there any diagnostic tests you recommend?
|
| 5 |
+
* What treatment options could be appropriate?
|
| 6 |
+
* Remember, I cannot provide medical advice, only information.
|