rukeshpaudel commited on
Commit
3409ff5
Β·
1 Parent(s): 82e46d5

main branch updated

Browse files
Files changed (4) hide show
  1. app.py +106 -60
  2. jupytermeroHealthAI.ipynb +152 -0
  3. utils/doc_contact.py +21 -0
  4. utils/docsnearme.py +43 -0
app.py CHANGED
@@ -1,66 +1,112 @@
1
- import gradio as gr
2
- import os
3
  import time
4
-
5
- # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
6
-
7
-
8
- def print_like_dislike(x: gr.LikeData):
9
- print(x.index, x.value, x.liked)
10
-
11
-
12
- def add_text(history, text):
13
- history = history + [(text, None)]
14
- return history, gr.Textbox(value="", interactive=False)
15
-
16
-
17
- def add_file(history, file):
18
- history = history + [((file.name,), None)]
19
- return history
20
-
21
-
22
- def bot(history):
23
- response = "**That's cool!**"
24
- history[-1][1] = ""
25
- for character in response:
26
- history[-1][1] += character
27
- time.sleep(0.05)
28
- yield history
29
-
30
-
31
- with gr.Blocks() as demo:
32
- with gr.Tab("Chatbot"):
33
- chatbot = gr.Chatbot(
34
- [],
35
- elem_id="chatbot",
36
- bubble_full_width=False,
37
- avatar_images=(None, "utils\images\health_assitant_chatbot_icon.png"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  )
39
 
40
- with gr.Row():
41
- txt = gr.Textbox(
42
- scale=4,
43
- show_label=False,
44
- placeholder="Enter text and press enter, or upload an image",
45
- container=False,
46
- )
47
- btn = gr.UploadButton("πŸ“", file_types=["image", "video", "audio"])
48
-
49
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
50
- bot, chatbot, chatbot, api_name="bot_response"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  )
52
- txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
53
- file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
54
- bot, chatbot, chatbot
55
- )
56
-
57
- chatbot.like(print_like_dislike, None, None)
58
-
59
- with gr.Tab("Medical Report storage"):
60
- with gr.Row("Upload your document here:"):
61
 
62
-
63
- demo.queue()
64
  if __name__ == "__main__":
65
- demo.launch(server_port=7860)
66
-
 
 
 
1
  import time
2
+ import os
3
+ import gradio as gr
4
+ from utils.doc_contact import Doctor
5
+ from openai import OpenAI
6
+
7
+ client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
8
+
9
+ # Initialize the client
10
+ # Set your OpenAI API key
11
+
12
+ """file = client.files.create(
13
+ file=open("songs.txt", "rb"),
14
+ purpose='assistants'
15
+ )"""
16
+
17
+ # Step 1: Create an Assistant
18
+ # assistant = client.beta.assistants.create(
19
+ # name="MeroHealthAI",
20
+ # instructions="You are a highly qualified and skilled doctor \
21
+ # who can ask all the right questions to the patient \
22
+ # and create an engaging and interesting conversation \
23
+ # and make patients let out all the diseases they are \
24
+ # suffering from. Then you will create a medical report \
25
+ # based on the symptoms. If you are 100% sure, you can also\
26
+ # predict the disease else just report the symptoms in a formal \
27
+ # formatted diagnosis report. Make sure to include all the vital \
28
+ # informations by asking the patients. Ask their name, address and\
29
+ # other personal details information before beginning asking for symptoms. \
30
+ # Also ask their weight and height, calculate BMI index, ask if they have the details\
31
+ # of the test they've previously taken. If they have any previous medical reports, ask \
32
+ # for their sugar level, blood pressure and other necessary information that are done in a whole \
33
+ # body checkup. Ask one question at a time so that the user doesn't feel overwhelmed. After completing asking the \
34
+ # symptoms, automatically generate the symptoms in a medical report like format along with the patient's information.",
35
+ # model="gpt-3.5-turbo",
36
+ # # file_ids=[file.id],
37
+ # #tools=[{"type": "retrieval"}]
38
+ # )
39
+ # Step 2: Create a Thread
40
+ thread = client.beta.threads.create()
41
+
42
+
43
+ def create_thread():
44
+ thread = client.beta.threads.create()
45
+ return thread.id
46
+
47
+
48
+ def main(query, history):
49
+ # Step 3: Add a Message to a Thread
50
+ history = (history,)
51
+ message = client.beta.threads.messages.create(
52
+ thread_id=thread.id, role="user", content=query
53
+ )
54
+
55
+ # Step 4: Run the Assistant
56
+ run = client.beta.threads.runs.create(
57
+ thread_id=thread.id,
58
+ assistant_id="asst_x34DeLtsxGXQBZCwN5aZhfBf",
59
+ instructions="User is a health patient, who is suffering from {disease}. You are supposed to 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.\
60
+ 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.",
61
+ )
62
+
63
+ while True:
64
+ # Wait for 5 seconds
65
+ time.sleep(0.5)
66
+
67
+ # Retrieve the run status
68
+ run_status = client.beta.threads.runs.retrieve(
69
+ thread_id=thread.id, run_id=run.id
70
  )
71
 
72
+ # If run is completed, get messages
73
+ if run_status.status == "completed":
74
+ messages = client.beta.threads.messages.list(thread_id=thread.id)
75
+ response = ""
76
+
77
+ data = messages.data
78
+ first_thread_message = data[0]
79
+ content = first_thread_message.content
80
+ response = content[0].text.value
81
+ return response
82
+ else:
83
+ continue
84
+
85
+
86
+ # Create a Gradio Interface
87
+ with gr.Blocks() as iface:
88
+ with gr.Tab("MeroHealthAI Chatbot"):
89
+ gr.Markdown("MeroHealthAI is an AI assited chatbot that gathers symptoms from the user, documents it and sends it to the nearest most relevant doctor available. Our app also suppors medical report analysis")
90
+ symptom_chatbot = gr.ChatInterface(
91
+ main
92
+ ) # , description="MeroHealthAI is an AI assited chatbot that gathers symptoms from the user, documents it and sends it to the nearest most relevant doctor available. Our app also suppors medical report analysis",\
93
+ # examples=["How can I find the right doctor for my ailment?",\
94
+ # "How do I contact a doctor without making an appointment?",\
95
+
96
+ # "I don't understant this medical report, can you describe it to me?", \
97
+ # "I have been having severe panic and anxiety attack, what could be the reason behind it?"]).queue()
98
+
99
+ symptom_chatbot
100
+ with gr.Tab("Contact Doctor "):
101
+ profession_key = gr.Textbox(label="Profession", interactive=True)
102
+ doc_info = gr.Textbox(label="Doctor Information") # Define textbox globally
103
+
104
+ gr.Button("Display_profession").click(
105
+ # Pass textbox element directly
106
+ Doctor.display_profession,
107
+ inputs=profession_key,
108
+ outputs=doc_info,
109
  )
 
 
 
 
 
 
 
 
 
110
 
 
 
111
  if __name__ == "__main__":
112
+ iface.launch()
 
jupytermeroHealthAI.ipynb ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 5,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from openai import OpenAI \n",
10
+ "import os\n",
11
+ "\n",
12
+ "client = OpenAI(\n",
13
+ " api_key=os.environ['QUALZ_OPEN_API_KEY']\n",
14
+ ") \n",
15
+ "\n",
16
+ "assistant = client.beta.assistants.create(\n",
17
+ " name=\"meroHeatlhAI\",\n",
18
+ " 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.\",\n",
19
+ " tools=[{\"type\": \"code_interpreter\"}],\n",
20
+ " model=\"gpt-3.5-turbo\"\n",
21
+ ")"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 6,
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "thread = client.beta.threads.create()"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 7,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "message = client.beta.threads.messages.create(\n",
40
+ " thread_id=thread.id,\n",
41
+ " role=\"user\",\n",
42
+ " content=\"I am having panic attack. Can you help me?\"\n",
43
+ ")"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": 8,
49
+ "metadata": {},
50
+ "outputs": [],
51
+ "source": [
52
+ "run = client.beta.threads.runs.create(\n",
53
+ " thread_id=thread.id,\n",
54
+ " assistant_id=assistant.id,\n",
55
+ " instructions=\"Please address the user as Jane Doe. The user has a premium account.\"\n",
56
+ ")"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 9,
62
+ "metadata": {},
63
+ "outputs": [],
64
+ "source": [
65
+ "run = client.beta.threads.runs.retrieve(\n",
66
+ " thread_id=thread.id,\n",
67
+ " run_id=run.id\n",
68
+ ")"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": 10,
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "messages = client.beta.threads.messages.list(\n",
78
+ " thread_id=thread.id\n",
79
+ ")"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 11,
85
+ "metadata": {},
86
+ "outputs": [
87
+ {
88
+ "name": "stdout",
89
+ "output_type": "stream",
90
+ "text": [
91
+ "SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_VlngIDB8OA8W0yXPkV9LX90J', assistant_id='asst_5iXApo7o4K2tQNxV9ZVL3kca', content=[MessageContentText(text=Text(annotations=[], value=\"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.\"), type='text')], created_at=1707376241, file_ids=[], metadata={}, object='thread.message', role='assistant', run_id='run_wuW9vVaYIPRLWNM3V0alAErM', thread_id='thread_CBuF63Y1dOLSJMDBl7f8wfVi'), ThreadMessage(id='msg_eC0sVacO4speeWSzF8KeJdzt', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='I am having panic attack. Can you help me?'), type='text')], created_at=1707376237, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_CBuF63Y1dOLSJMDBl7f8wfVi')], object='list', first_id='msg_VlngIDB8OA8W0yXPkV9LX90J', last_id='msg_eC0sVacO4speeWSzF8KeJdzt', has_more=False)\n"
92
+ ]
93
+ }
94
+ ],
95
+ "source": [
96
+ "print(messages)"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 14,
102
+ "metadata": {},
103
+ "outputs": [
104
+ {
105
+ "name": "stdout",
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
+ ],
112
+ "source": [
113
+ "print(messages.data[0].content[0].text.value)"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": null,
119
+ "metadata": {},
120
+ "outputs": [],
121
+ "source": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": null,
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": []
129
+ }
130
+ ],
131
+ "metadata": {
132
+ "kernelspec": {
133
+ "display_name": ".venv",
134
+ "language": "python",
135
+ "name": "python3"
136
+ },
137
+ "language_info": {
138
+ "codemirror_mode": {
139
+ "name": "ipython",
140
+ "version": 3
141
+ },
142
+ "file_extension": ".py",
143
+ "mimetype": "text/x-python",
144
+ "name": "python",
145
+ "nbconvert_exporter": "python",
146
+ "pygments_lexer": "ipython3",
147
+ "version": "3.11.4"
148
+ }
149
+ },
150
+ "nbformat": 4,
151
+ "nbformat_minor": 2
152
+ }
utils/doc_contact.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ class Doctor():
4
+
5
+ doctor_professions = {
6
+ "neurologist": "Dr. Ram Hari",
7
+ "psychiatrist": "Dr. Sita",
8
+ "cardiologist": "Dr. Deals",
9
+ "dermatologist": "Dr. Hari",
10
+ }
11
+
12
+ def display_profession(profession_key):
13
+ # Handle invalid key (same as previous example)
14
+ if profession_key not in Doctor.doctor_professions:
15
+ return "Invalid profession. Please choose from available options."
16
+
17
+ doctor_data = Doctor.doctor_professions[profession_key]
18
+ # Build desired output format (adjust to your preference)
19
+ output = f"\n**Doctor details:**\n - Name: {doctor_data}\n - Profession: {profession_key}"
20
+
21
+ return output
utils/docsnearme.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import googlemaps
3
+ from geopy.geocoders import Nominatim
4
+ import os
5
+ # Initialize Google Maps API client
6
+ gmaps = googlemaps.Client(key="AIzaSyB0gKa0rMc_OqxK0KvtDRDbghy8IRssjlY")
7
+
8
+ # Function to find nearest doctors
9
+ def find_nearest_doctors(location):
10
+ # Convert address to coordinates
11
+ geolocator = Nominatim(user_agent="geoapiExercises")
12
+ loc = geolocator.geocode(location)
13
+ latitude = loc.latitude
14
+ longitude = loc.longitude
15
+
16
+ # Find nearby doctors (you might need to adjust the radius)
17
+ places = gmaps.places_nearby(location=(latitude, longitude), radius=1000, type='doctor')
18
+
19
+ # Extract doctors' locations
20
+ doctors_locations = [(place['name'], place['geometry']['location']['lat'], place['geometry']['location']['lng']) for place in places['results']]
21
+
22
+ return doctors_locations
23
+
24
+ # Create Gradio interface
25
+ def nearest_doctors(location):
26
+ doctors_locations = find_nearest_doctors(location)
27
+
28
+ # Generate HTML for Google Maps display
29
+ map_html = f"<iframe width='600' height='500' src='https://maps.google.com/maps?q={location}&output=embed'></iframe>"
30
+
31
+ # Add markers for doctors on the map
32
+ for doctor in doctors_locations:
33
+ map_html += f"<p>{doctor[0]}</p><iframe width='600' height='500' src='https://www.google.com/maps/embed/v1/place?q={doctor[1]},{doctor[2]}&key=YOUR_API_KEY'></iframe>"
34
+
35
+ return map_html
36
+
37
+ # Create Gradio interface
38
+ iface = gr.Interface(fn=nearest_doctors,
39
+ inputs="text",
40
+ outputs="html",
41
+ title="Find Nearest Doctors",
42
+ description="Enter your location to find nearest doctors.")
43
+ iface.launch(share=True)