Spaces:
Runtime error
Runtime error
added redis backend
Browse files
.env
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
OPENAI_API_KEY=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
|
2 |
-
ELEVEN_API_KEY=5db7eb7ac620c89ec32e72de98128417
|
|
|
|
|
|
1 |
OPENAI_API_KEY=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
|
2 |
+
ELEVEN_API_KEY=5db7eb7ac620c89ec32e72de98128417
|
3 |
+
REDIS_HOST=localhost
|
4 |
+
REDIS_PORT=6379
|
README.md
CHANGED
@@ -7,4 +7,10 @@ sdk: "gradio"
|
|
7 |
sdk_version: "3.30.0"
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
sdk_version: "3.30.0"
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
To run the playground
|
13 |
+
|
14 |
+
```
|
15 |
+
jupyter notebook
|
16 |
+
```
|
app.py
CHANGED
@@ -14,8 +14,16 @@ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
|
14 |
from langchain.chat_models import ChatOpenAI
|
15 |
from langchain.prompts import HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
16 |
from langchain.schema import AIMessage, BaseMessage, HumanMessage, SystemMessage
|
17 |
-
|
18 |
from callback import QueueCallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
MODELS_NAMES = ["gpt-3.5-turbo", "gpt-4"]
|
21 |
DEFAULT_TEMPERATURE = 0.7
|
@@ -25,8 +33,12 @@ ChatHistory = List[str]
|
|
25 |
logging.basicConfig(
|
26 |
format="[%(asctime)s %(levelname)s]: %(message)s", level=logging.INFO
|
27 |
)
|
|
|
|
|
28 |
# load up our system prompt
|
29 |
-
system_message_prompt = SystemMessagePromptTemplate.from_template(
|
|
|
|
|
30 |
# for the human, we will just inject the text
|
31 |
human_message_prompt_template = HumanMessagePromptTemplate.from_template("{text}")
|
32 |
|
@@ -35,6 +47,7 @@ with open("data/patients.json") as f:
|
|
35 |
|
36 |
patients_names = [el["name"] for el in patiens]
|
37 |
|
|
|
38 |
def message_handler(
|
39 |
chat: Optional[ChatOpenAI],
|
40 |
message: str,
|
@@ -92,6 +105,16 @@ def on_clear_click() -> Tuple[str, List, List]:
|
|
92 |
return "", [], []
|
93 |
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
def on_apply_settings_click(model_name: str, temperature: float):
|
96 |
logging.info(
|
97 |
f"Applying settings: model_name={model_name}, temperature={temperature}"
|
@@ -112,9 +135,20 @@ def on_drop_down_change(selected_item, messages):
|
|
112 |
patient = patiens[index]
|
113 |
messages = [system_message_prompt.format(patient=patient)]
|
114 |
print(f"You selected: {selected_item}", index)
|
115 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
|
|
|
118 |
# some css why not, "borrowed" from https://huggingface.co/spaces/ysharma/Gradio-demo-streaming/blob/main/app.py
|
119 |
with gr.Blocks(
|
120 |
css="""#col_container {width: 700px; margin-left: auto; margin-right: auto;}
|
@@ -124,12 +158,21 @@ with gr.Blocks(
|
|
124 |
messages = gr.State([system_message_prompt.format(patient=patiens[0])])
|
125 |
# same thing for the chat, we want one chat per use so callbacks are unique I guess
|
126 |
chat = gr.State(None)
|
127 |
-
|
128 |
patient = gr.State(patiens[0])
|
|
|
|
|
129 |
|
130 |
with gr.Column(elem_id="col_container"):
|
131 |
-
gr.Markdown("# Welcome to
|
132 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
chatbot = gr.Chatbot()
|
135 |
with gr.Column():
|
@@ -140,12 +183,19 @@ with gr.Blocks(
|
|
140 |
[chat, message, chatbot, messages],
|
141 |
queue=True,
|
142 |
)
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
with gr.Row():
|
150 |
with gr.Column():
|
151 |
clear = gr.Button("Clear")
|
@@ -181,13 +231,13 @@ with gr.Blocks(
|
|
181 |
interactive=True,
|
182 |
label="Patient",
|
183 |
)
|
184 |
-
|
185 |
-
|
186 |
-
)
|
187 |
dropdown.change(
|
188 |
fn=on_drop_down_change,
|
189 |
inputs=[dropdown, messages],
|
190 |
-
outputs=[
|
191 |
)
|
192 |
-
|
193 |
-
demo.queue()
|
|
|
|
14 |
from langchain.chat_models import ChatOpenAI
|
15 |
from langchain.prompts import HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
16 |
from langchain.schema import AIMessage, BaseMessage, HumanMessage, SystemMessage
|
17 |
+
from js import get_window_url_params
|
18 |
from callback import QueueCallback
|
19 |
+
from db import (
|
20 |
+
User,
|
21 |
+
Chat,
|
22 |
+
create_user,
|
23 |
+
get_client,
|
24 |
+
get_user_by_username,
|
25 |
+
add_chat_by_uid,
|
26 |
+
)
|
27 |
|
28 |
MODELS_NAMES = ["gpt-3.5-turbo", "gpt-4"]
|
29 |
DEFAULT_TEMPERATURE = 0.7
|
|
|
33 |
logging.basicConfig(
|
34 |
format="[%(asctime)s %(levelname)s]: %(message)s", level=logging.INFO
|
35 |
)
|
36 |
+
# load redis client
|
37 |
+
client = get_client()
|
38 |
# load up our system prompt
|
39 |
+
system_message_prompt = SystemMessagePromptTemplate.from_template(
|
40 |
+
Path("prompts/system.prompt").read_text()
|
41 |
+
)
|
42 |
# for the human, we will just inject the text
|
43 |
human_message_prompt_template = HumanMessagePromptTemplate.from_template("{text}")
|
44 |
|
|
|
47 |
|
48 |
patients_names = [el["name"] for el in patiens]
|
49 |
|
50 |
+
|
51 |
def message_handler(
|
52 |
chat: Optional[ChatOpenAI],
|
53 |
message: str,
|
|
|
105 |
return "", [], []
|
106 |
|
107 |
|
108 |
+
def on_done_click(
|
109 |
+
chatbot_messages: ChatHistory, patient: str, user: User
|
110 |
+
) -> Tuple[str, List, List]:
|
111 |
+
logging.info(f"Saving chat for user={user}")
|
112 |
+
add_chat_by_uid(
|
113 |
+
client, Chat(patient=patient, messages=chatbot_messages), user["uid"]
|
114 |
+
)
|
115 |
+
return on_clear_click()
|
116 |
+
|
117 |
+
|
118 |
def on_apply_settings_click(model_name: str, temperature: float):
|
119 |
logging.info(
|
120 |
f"Applying settings: model_name={model_name}, temperature={temperature}"
|
|
|
135 |
patient = patiens[index]
|
136 |
messages = [system_message_prompt.format(patient=patient)]
|
137 |
print(f"You selected: {selected_item}", index)
|
138 |
+
return patient, patient, [], messages
|
139 |
+
|
140 |
+
|
141 |
+
def on_demo_load(url_params):
|
142 |
+
username = url_params["username"]
|
143 |
+
logging.info(f"Getting user for username={username}")
|
144 |
+
create_user(client, User(username=username, uid=None))
|
145 |
+
user = get_user_by_username(client, username)
|
146 |
+
logging.info(f"User {user}")
|
147 |
+
print(f"got url_params: {url_params}")
|
148 |
+
return user, f"Nice to see you {user['username']} π"
|
149 |
|
150 |
|
151 |
+
url_params = gr.JSON({}, visible=False, label="URL Params")
|
152 |
# some css why not, "borrowed" from https://huggingface.co/spaces/ysharma/Gradio-demo-streaming/blob/main/app.py
|
153 |
with gr.Blocks(
|
154 |
css="""#col_container {width: 700px; margin-left: auto; margin-right: auto;}
|
|
|
158 |
messages = gr.State([system_message_prompt.format(patient=patiens[0])])
|
159 |
# same thing for the chat, we want one chat per use so callbacks are unique I guess
|
160 |
chat = gr.State(None)
|
161 |
+
user = gr.State(None)
|
162 |
patient = gr.State(patiens[0])
|
163 |
+
# see here https://github.com/gradio-app/gradio/discussions/2949#discussioncomment-5278991
|
164 |
+
url_params.render()
|
165 |
|
166 |
with gr.Column(elem_id="col_container"):
|
167 |
+
gr.Markdown("# Welcome to OscePal! π¨ββοΈπ§ββοΈ")
|
168 |
+
welcome_markdown = gr.Markdown("")
|
169 |
+
|
170 |
+
demo.load(
|
171 |
+
fn=on_demo_load,
|
172 |
+
inputs=[url_params],
|
173 |
+
outputs=[user, welcome_markdown],
|
174 |
+
_js=get_window_url_params,
|
175 |
+
)
|
176 |
|
177 |
chatbot = gr.Chatbot()
|
178 |
with gr.Column():
|
|
|
183 |
[chat, message, chatbot, messages],
|
184 |
queue=True,
|
185 |
)
|
186 |
+
with gr.Row():
|
187 |
+
submit = gr.Button("Send Message", variant="primary")
|
188 |
+
submit.click(
|
189 |
+
message_handler,
|
190 |
+
[chat, message, chatbot, messages],
|
191 |
+
[chat, message, chatbot, messages],
|
192 |
+
)
|
193 |
+
done = gr.Button("Done")
|
194 |
+
done.click(
|
195 |
+
on_done_click,
|
196 |
+
[chatbot, patient, user],
|
197 |
+
[message, chatbot, messages],
|
198 |
+
)
|
199 |
with gr.Row():
|
200 |
with gr.Column():
|
201 |
clear = gr.Button("Clear")
|
|
|
231 |
interactive=True,
|
232 |
label="Patient",
|
233 |
)
|
234 |
+
|
235 |
+
patient_card = gr.JSON(patient.value, visible=True, label="Patient card")
|
|
|
236 |
dropdown.change(
|
237 |
fn=on_drop_down_change,
|
238 |
inputs=[dropdown, messages],
|
239 |
+
outputs=[patient_card, patient, chatbot, messages],
|
240 |
)
|
241 |
+
|
242 |
+
demo.queue()
|
243 |
+
demo.launch()
|
playground.ipynb
CHANGED
@@ -61,7 +61,7 @@
|
|
61 |
},
|
62 |
{
|
63 |
"cell_type": "code",
|
64 |
-
"execution_count":
|
65 |
"id": "c83d506e",
|
66 |
"metadata": {},
|
67 |
"outputs": [],
|
@@ -132,7 +132,7 @@
|
|
132 |
},
|
133 |
{
|
134 |
"cell_type": "code",
|
135 |
-
"execution_count":
|
136 |
"id": "8405efd6",
|
137 |
"metadata": {},
|
138 |
"outputs": [
|
@@ -141,17 +141,17 @@
|
|
141 |
"output_type": "stream",
|
142 |
"text": [
|
143 |
"π¨ββοΈ:What brings you to the emergency department today?\n",
|
144 |
-
"π€:I have been experiencing severe abdominal pain for the past
|
145 |
-
"π€:\
|
146 |
"π¨ββοΈ:Tell me more about your symptoms\n",
|
147 |
-
"π€:The pain is mostly in the upper part of my abdomen,
|
148 |
-
"π€:\
|
149 |
"π¨ββοΈ:Are there any other symptoms apart from the main symptoms?\n",
|
150 |
-
"π€:No,
|
151 |
-
"π€:\
|
152 |
"π¨ββοΈ:What is your past medical history?\n",
|
153 |
-
"π€:I don't have any significant
|
154 |
-
"π€:\
|
155 |
]
|
156 |
}
|
157 |
],
|
|
|
61 |
},
|
62 |
{
|
63 |
"cell_type": "code",
|
64 |
+
"execution_count": 46,
|
65 |
"id": "c83d506e",
|
66 |
"metadata": {},
|
67 |
"outputs": [],
|
|
|
132 |
},
|
133 |
{
|
134 |
"cell_type": "code",
|
135 |
+
"execution_count": 47,
|
136 |
"id": "8405efd6",
|
137 |
"metadata": {},
|
138 |
"outputs": [
|
|
|
141 |
"output_type": "stream",
|
142 |
"text": [
|
143 |
"π¨ββοΈ:What brings you to the emergency department today?\n",
|
144 |
+
"π€:I have been experiencing severe abdominal pain for the past two days, and it has been getting worse. I also feel nauseous and have been vomiting. I thought it would go away on its own, but it hasn't.\n",
|
145 |
+
"π€:\tI provided a clear and concise answer to the doctor's question, stating my presenting complaint and how long it has been going on. I also mentioned other symptoms that could be related to my condition.\n",
|
146 |
"π¨ββοΈ:Tell me more about your symptoms\n",
|
147 |
+
"π€:The pain is mostly in the upper part of my abdomen, and it feels like a burning sensation. It gets worse after I eat or drink something. I also feel bloated and have been burping a lot. The nausea comes and goes, but I have been vomiting every few hours. I haven't had a fever or diarrhea, though.\n",
|
148 |
+
"π€:\tSince you asked for more information about my symptoms, I provided more details about the location and type of pain, as well as other symptoms like bloating and burping. I also mentioned that I haven't had a fever or diarrhea, which could help rule out some conditions.\n",
|
149 |
"π¨ββοΈ:Are there any other symptoms apart from the main symptoms?\n",
|
150 |
+
"π€:No, these are the main symptoms I have been experiencing.\n",
|
151 |
+
"π€:\tI answered the doctor's question honestly and concisely, stating that I have not experienced any other symptoms apart from the ones I have already mentioned.\n",
|
152 |
"π¨ββοΈ:What is your past medical history?\n",
|
153 |
+
"π€:I don't have any significant medical history. I have never had any surgeries or been hospitalized before. I don't take any medications regularly, and I have no known allergies.\n",
|
154 |
+
"π€:\tI provided a clear and concise answer to the doctor's question, stating that I have no significant medical history, surgeries, or hospitalizations. I also mentioned that I don't take any medications regularly and have no known allergies, which could be relevant information for my current condition.\n"
|
155 |
]
|
156 |
}
|
157 |
],
|