rukeshpaudel commited on
Commit
e611af1
Β·
1 Parent(s): 76e438b

Jupyter notebook added

Browse files
Files changed (2) hide show
  1. jupytermeroHealthAI.ipynb +128 -0
  2. utils/chatbot.py +36 -41
jupytermeroHealthAI.ipynb ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
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-4-turbo-preview\"\n",
21
+ ")"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 2,
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "thread = client.beta.threads.create()"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 3,
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": 4,
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": 5,
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": 6,
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": 7,
85
+ "metadata": {},
86
+ "outputs": [
87
+ {
88
+ "name": "stdout",
89
+ "output_type": "stream",
90
+ "text": [
91
+ "SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_NWYcOPsTRpXx6jMFScbullxL', assistant_id='asst_F16PfBMjlfT9Mokhwp455i2u', content=[MessageContentText(text=Text(annotations=[], value=\"I'm truly sorry to hear that you're experiencing a panic attack. While I'm not a licensed therapist, I can offer some general strategies that many find helpful in managing the symptoms. Please know that if you are feeling overwhelmed, reaching out to a professional or a trusted individual in your life for support is a good step. Here are some techniques you might try:\\n\\n1. **Focused Breathing:** Focus on taking slow, deep breaths. Inhale slowly through your nose, hold for a few seconds, and then exhale slowly through your mouth. This helps calm your nervous system and can make you feel more grounded.\\n\\n2. **5-4-3-2-1 Grounding Technique:** This involves identifying and acknowledging different objects around you to help ground yourself in the present moment.\\n - Find 5 things you can see.\\n - Find 4 things you can touch.\\n - Find 3 things you can hear.\\n - Find 2 things you can smell or like the smell of.\\n - Find 1 thing you can taste or like the taste of.\\n\\n3. **Progressive Muscle Relaxation (PMR):** Tense each muscle group in your body (but not to the point of strain) and then relax it. Start from your toes and work your way up to your head. This can help reduce the feelings of physical tension that often accompany panic attacks.\\n\\n4. **Mindfulness or Meditation:** Using a mindfulness or meditation app, or simply sitting quietly and observing your thoughts as they come and go without judgment, can be helpful.\\n\\n5. **Distraction:** Sometimes, distracting yourself with an activity you enjoy (like watching a favorite TV show, listening to music, or engaging in a hobby) can help take your mind off your anxiety.\\n\\nRemember, panic attacks, although intensely uncomfortable, are not usually dangerous. They are temporary and will pass. It's important to be kind to yourself and acknowledge that experiencing anxiety and panic attacks is a part of being human.\\n\\nWould you like to try a guided breathing exercise together, or is there something specific you think might help in this moment?\"), type='text')], created_at=1707326498, file_ids=[], metadata={}, object='thread.message', role='assistant', run_id='run_gIB5I7HlpJ89oyEDJcgTPQkn', thread_id='thread_OJzbkwO7qRGpyeABr9F6tuxV'), ThreadMessage(id='msg_r7FY81pqxT9p9wxHvcI60asR', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='I am having panic attack. Can you help me?'), type='text')], created_at=1707326436, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_OJzbkwO7qRGpyeABr9F6tuxV')], object='list', first_id='msg_NWYcOPsTRpXx6jMFScbullxL', last_id='msg_r7FY81pqxT9p9wxHvcI60asR', has_more=False)\n"
92
+ ]
93
+ }
94
+ ],
95
+ "source": [
96
+ "print(messages)"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": null,
102
+ "metadata": {},
103
+ "outputs": [],
104
+ "source": []
105
+ }
106
+ ],
107
+ "metadata": {
108
+ "kernelspec": {
109
+ "display_name": ".venv",
110
+ "language": "python",
111
+ "name": "python3"
112
+ },
113
+ "language_info": {
114
+ "codemirror_mode": {
115
+ "name": "ipython",
116
+ "version": 3
117
+ },
118
+ "file_extension": ".py",
119
+ "mimetype": "text/x-python",
120
+ "name": "python",
121
+ "nbconvert_exporter": "python",
122
+ "pygments_lexer": "ipython3",
123
+ "version": "3.11.4"
124
+ }
125
+ },
126
+ "nbformat": 4,
127
+ "nbformat_minor": 2
128
+ }
utils/chatbot.py CHANGED
@@ -1,55 +1,50 @@
1
- from openai import OpenAI
2
- import gradio as gr
3
  import os
 
4
 
5
  client = OpenAI(
6
- # This is the default and can be omitted
7
- api_key=os.environ.get("OPENAI_API_KEY"),
 
 
 
 
 
 
8
  )
9
 
10
- def chat_response(prompt, history=[]):
11
- """
12
- Provides a chatbot response based on the given prompt and history.
13
-
14
- Args:
15
- prompt (str): The user's input prompt.
16
- history (list, optional): List of previous interactions (user and assistant messages). Defaults to [].
17
-
18
- Returns:
19
- str: The assistant's response.
20
- """
21
-
22
- # Update history with the user's prompt
23
- history.append({"role": "user", "content": prompt})
24
-
25
- # Generate response using OpenAI's chat completion API
26
- response = client.chat.completions.create(
27
- model="gpt-3.5-turbo",
28
- messages=[{"role": "user", "content": prompt}],
29
- max_tokens=150,
30
- temperature=0.7,
31
- top_p=1,
32
- frequency_penalty=0,
33
- presence_penalty=0.6,
34
- stop=None,
35
- n=1
36
  )
37
- assistant_response = response.choices[0].message.content
38
 
39
- # Update history with the assistant's response
40
- history.append({"role": "assistant", "content": assistant_response})
 
 
 
41
 
42
- return assistant_response, history
 
 
 
43
 
 
 
 
44
  # Create the Gradio interface
45
- interface = gr.Interface(
 
 
46
  fn=chat_response,
47
- inputs=gr.Textbox(lines=3, label="Enter your prompt:"),
48
- outputs="text",
49
- title="OpenAI Chatbot: Ask Me Anything!",
50
- #theme="dark",
51
- #use_legacy_return=True, # Ensure all outputs are returned to Gradio
52
  )
53
 
54
  # Launch the interface
55
- interface.launch()
 
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-4-turbo-preview"
14
  )
15
 
16
+ def chat_response():
17
+
18
+ thread = client.beta.threads.create()
19
+
20
+ message = client.beta.threads.messages.create(
21
+ thread_id=thread.id,
22
+ role="user",
23
+ content="I am having panic attack. Can you help me?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
 
25
 
26
+ run = client.beta.threads.runs.create(
27
+ thread_id=thread.id,
28
+ assistant_id=assistant.id,
29
+ instructions="Please address the user as Jane Doe. The user has a premium account."
30
+ )
31
 
32
+ run = client.beta.threads.runs.retrieve(
33
+ thread_id=thread.id,
34
+ run_id=run.id
35
+ )
36
 
37
+ messages = client.beta.threads.messages.list(
38
+ thread_id=thread.id
39
+ )
40
  # Create the Gradio interface
41
+ with gr.Blocks() as iface:
42
+ gr.Textbox(lines=3, label="Set the context and ask your question:")
43
+ gr.ChatInterface(
44
  fn=chat_response,
45
+ title="Chat with this bot!",
46
+
 
 
 
47
  )
48
 
49
  # Launch the interface
50
+ iface.launch()