Spaces:
Build error
Build error
Commit Β·
b468877
1
Parent(s): e611af1
chatbot updated
Browse files- app.py +77 -59
- jupytermeroHealthAI.ipynb +30 -9
- utils/chatbot.py +8 -9
app.py
CHANGED
|
@@ -1,66 +1,84 @@
|
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
placeholder="Enter text and press enter, or upload an image",
|
| 45 |
-
container=False,
|
| 46 |
)
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import time
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
|
| 9 |
+
|
| 10 |
+
# Initialize the client
|
| 11 |
+
# Set your OpenAI API key
|
| 12 |
+
|
| 13 |
+
'''file = client.files.create(
|
| 14 |
+
file=open("songs.txt", "rb"),
|
| 15 |
+
purpose='assistants'
|
| 16 |
+
)'''
|
| 17 |
+
|
| 18 |
+
# Step 1: Create an Assistant
|
| 19 |
+
assistant = client.beta.assistants.create(
|
| 20 |
+
name="MeroHealthAI",
|
| 21 |
+
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.",
|
| 22 |
+
model="gpt-3.5-turbo",
|
| 23 |
+
# file_ids=[file.id],
|
| 24 |
+
tools=[{"type": "retrieval"}]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Step 2: Create a Thread
|
| 28 |
+
thread = client.beta.threads.create()
|
| 29 |
+
|
| 30 |
+
def main(query, history):
|
| 31 |
+
# Step 3: Add a Message to a Thread
|
| 32 |
+
history=history,
|
| 33 |
+
message = client.beta.threads.messages.create(
|
| 34 |
+
thread_id=thread.id,
|
| 35 |
+
role="user",
|
| 36 |
+
content=query
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Step 4: Run the Assistant
|
| 40 |
+
run = client.beta.threads.runs.create(
|
| 41 |
+
thread_id=thread.id,
|
| 42 |
+
assistant_id=assistant.id,
|
| 43 |
+
instructions="The user is a humanitarian worker who is going through digital transformation"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
while True:
|
| 47 |
+
# Wait for 5 seconds
|
| 48 |
+
time.sleep(0.5)
|
| 49 |
+
|
| 50 |
+
# Retrieve the run status
|
| 51 |
+
run_status = client.beta.threads.runs.retrieve(
|
| 52 |
+
thread_id=thread.id,
|
| 53 |
+
run_id=run.id
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# If run is completed, get messages
|
| 57 |
+
if run_status.status == 'completed':
|
| 58 |
+
messages = client.beta.threads.messages.list(
|
| 59 |
+
thread_id=thread.id
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
+
response = ""
|
| 62 |
+
|
| 63 |
+
data = messages.data
|
| 64 |
+
first_thread_message = data[0]
|
| 65 |
+
content = first_thread_message.content
|
| 66 |
+
response = content[0].text.value
|
| 67 |
+
return response
|
| 68 |
+
else:
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
# Create a Gradio Interface
|
| 72 |
+
|
| 73 |
+
iface = gr.ChatInterface(main, title="SAFe Specialist",\
|
| 74 |
+
description="SAFe Specialist guiding transitions with realistic and \
|
| 75 |
+
optimistic advice towards a product centric approach",\
|
| 76 |
+
examples=["How can I shift from project to product mode?",\
|
| 77 |
+
"What are the key SAFe principles for my organization?",\
|
| 78 |
+
"Can you provide options for agile practices in my setting?",\
|
| 79 |
+
"How do I deal with cultural resistance in SAFe adoption?", \
|
| 80 |
+
"What's your advice for an org with many different digital solutions?",\
|
| 81 |
+
"Could you walk me through the step-by-step process of moving into SAFe?"]).queue()
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|
|
|
jupytermeroHealthAI.ipynb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
-
"execution_count":
|
| 6 |
"metadata": {},
|
| 7 |
"outputs": [],
|
| 8 |
"source": [
|
|
@@ -17,13 +17,13 @@
|
|
| 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-
|
| 21 |
")"
|
| 22 |
]
|
| 23 |
},
|
| 24 |
{
|
| 25 |
"cell_type": "code",
|
| 26 |
-
"execution_count":
|
| 27 |
"metadata": {},
|
| 28 |
"outputs": [],
|
| 29 |
"source": [
|
|
@@ -32,7 +32,7 @@
|
|
| 32 |
},
|
| 33 |
{
|
| 34 |
"cell_type": "code",
|
| 35 |
-
"execution_count":
|
| 36 |
"metadata": {},
|
| 37 |
"outputs": [],
|
| 38 |
"source": [
|
|
@@ -45,7 +45,7 @@
|
|
| 45 |
},
|
| 46 |
{
|
| 47 |
"cell_type": "code",
|
| 48 |
-
"execution_count":
|
| 49 |
"metadata": {},
|
| 50 |
"outputs": [],
|
| 51 |
"source": [
|
|
@@ -58,7 +58,7 @@
|
|
| 58 |
},
|
| 59 |
{
|
| 60 |
"cell_type": "code",
|
| 61 |
-
"execution_count":
|
| 62 |
"metadata": {},
|
| 63 |
"outputs": [],
|
| 64 |
"source": [
|
|
@@ -70,7 +70,7 @@
|
|
| 70 |
},
|
| 71 |
{
|
| 72 |
"cell_type": "code",
|
| 73 |
-
"execution_count":
|
| 74 |
"metadata": {},
|
| 75 |
"outputs": [],
|
| 76 |
"source": [
|
|
@@ -81,14 +81,14 @@
|
|
| 81 |
},
|
| 82 |
{
|
| 83 |
"cell_type": "code",
|
| 84 |
-
"execution_count":
|
| 85 |
"metadata": {},
|
| 86 |
"outputs": [
|
| 87 |
{
|
| 88 |
"name": "stdout",
|
| 89 |
"output_type": "stream",
|
| 90 |
"text": [
|
| 91 |
-
"SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='
|
| 92 |
]
|
| 93 |
}
|
| 94 |
],
|
|
@@ -96,6 +96,27 @@
|
|
| 96 |
"print(messages)"
|
| 97 |
]
|
| 98 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
{
|
| 100 |
"cell_type": "code",
|
| 101 |
"execution_count": null,
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
+
"execution_count": 5,
|
| 6 |
"metadata": {},
|
| 7 |
"outputs": [],
|
| 8 |
"source": [
|
|
|
|
| 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": [
|
|
|
|
| 32 |
},
|
| 33 |
{
|
| 34 |
"cell_type": "code",
|
| 35 |
+
"execution_count": 7,
|
| 36 |
"metadata": {},
|
| 37 |
"outputs": [],
|
| 38 |
"source": [
|
|
|
|
| 45 |
},
|
| 46 |
{
|
| 47 |
"cell_type": "code",
|
| 48 |
+
"execution_count": 8,
|
| 49 |
"metadata": {},
|
| 50 |
"outputs": [],
|
| 51 |
"source": [
|
|
|
|
| 58 |
},
|
| 59 |
{
|
| 60 |
"cell_type": "code",
|
| 61 |
+
"execution_count": 9,
|
| 62 |
"metadata": {},
|
| 63 |
"outputs": [],
|
| 64 |
"source": [
|
|
|
|
| 70 |
},
|
| 71 |
{
|
| 72 |
"cell_type": "code",
|
| 73 |
+
"execution_count": 10,
|
| 74 |
"metadata": {},
|
| 75 |
"outputs": [],
|
| 76 |
"source": [
|
|
|
|
| 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 |
],
|
|
|
|
| 96 |
"print(messages)"
|
| 97 |
]
|
| 98 |
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "code",
|
| 101 |
+
"execution_count": 13,
|
| 102 |
+
"metadata": {},
|
| 103 |
+
"outputs": [
|
| 104 |
+
{
|
| 105 |
+
"ename": "AttributeError",
|
| 106 |
+
"evalue": "'list' object has no attribute 'text'",
|
| 107 |
+
"output_type": "error",
|
| 108 |
+
"traceback": [
|
| 109 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
| 110 |
+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 111 |
+
"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",
|
| 112 |
+
"\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'text'"
|
| 113 |
+
]
|
| 114 |
+
}
|
| 115 |
+
],
|
| 116 |
+
"source": [
|
| 117 |
+
"print(messages.data[0].content[0].text.value)"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
{
|
| 121 |
"cell_type": "code",
|
| 122 |
"execution_count": null,
|
utils/chatbot.py
CHANGED
|
@@ -10,23 +10,21 @@ 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-
|
| 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=
|
| 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(
|
|
@@ -37,13 +35,14 @@ def chat_response():
|
|
| 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
|
|
|
|
| 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(
|
|
|
|
| 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
|