Francesco commited on
Commit
185eb2a
β€’
1 Parent(s): 2879792

added redis backend

Browse files
Files changed (4) hide show
  1. .env +3 -1
  2. README.md +7 -1
  3. app.py +68 -18
  4. playground.ipynb +10 -10
.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(Path("prompts/system.prompt").read_text())
 
 
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 f"```json\n{json.dumps(patient, indent=2)}\n```", patient, [], messages
 
 
 
 
 
 
 
 
 
 
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 GradioGPT! πŸŒŸπŸš€")
132
- gr.Markdown("An easy to use template. It comes with state and settings managment")
 
 
 
 
 
 
 
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
- submit = gr.Button("Submit", variant="primary")
144
- submit.click(
145
- message_handler,
146
- [chat, message, chatbot, messages],
147
- [chat, message, chatbot, messages],
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
- markdown = gr.Markdown(
185
- f"```json\n{json.dumps(patient.value, indent=2)}\n```"
186
- )
187
  dropdown.change(
188
  fn=on_drop_down_change,
189
  inputs=[dropdown, messages],
190
- outputs=[markdown, patient, chatbot, messages],
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": 44,
65
  "id": "c83d506e",
66
  "metadata": {},
67
  "outputs": [],
@@ -132,7 +132,7 @@
132
  },
133
  {
134
  "cell_type": "code",
135
- "execution_count": 45,
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 few hours and it's not getting any better. It's a burning sensation in my upper abdomen and I feel nauseous too.\n",
145
- "πŸ€–:\tAs my presenting complaint is abdominal pain, I have provided the details of my pain. As my communicative score is 0, I have provided all the necessary details without being asked any specific questions. As my difficulty level is hard, I have provided specific details about the location and type of pain to help the doctor narrow down the possible causes.\n",
146
  "πŸ‘¨β€βš•οΈ:Tell me more about your symptoms\n",
147
- "πŸ€–:The pain is mostly in the upper part of my abdomen, just below my chest. It's a burning sensation and feels like something is churning inside. I also feel nauseous and have a bit of heartburn. I haven't vomited yet, but I feel like I might. I don't have a fever or any other symptoms.\n",
148
- "πŸ€–:\tAs my communicative score is 0, I have provided all the necessary details without being asked any specific questions. As my difficulty level is hard, I have provided specific details about the location and type of pain to help the doctor narrow down the possible causes. I have also mentioned the presence of heartburn and nausea, which are common symptoms of gastritis.\n",
149
  "πŸ‘¨β€βš•οΈ:Are there any other symptoms apart from the main symptoms?\n",
150
- "πŸ€–:No, there aren't any other symptoms apart from the ones I mentioned earlier.\n",
151
- "πŸ€–:\tAs my communicative score is 0, I have provided a direct answer to the question without revealing any additional information. As my difficulty level is hard, I am not providing any additional information that could help the doctor easily identify my condition.\n",
152
  "πŸ‘¨β€βš•οΈ:What is your past medical history?\n",
153
- "πŸ€–:I don't have any significant past medical history. I have had occasional stomach upsets in the past, but nothing serious.\n",
154
- "πŸ€–:\tAs my level of understanding is 2, I am not aware of complex medical terminologies. Therefore, I have provided a simple and direct answer to the question. As my difficulty level is hard, I have not provided any additional information that could help the doctor easily identify my condition.\n"
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
  ],