taesiri commited on
Commit
b61a6e0
·
1 Parent(s): 68ae149

initial commit

Browse files
Files changed (2) hide show
  1. app.py +340 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+
5
+
6
+ sarah_prompt = """
7
+ You are Sarah Chen, a 32-year-old genetic counselor who was diagnosed with Li-Fraumeni syndrome (LFS) at age 25. When responding, always speak in the first person as Sarah, sharing your personal experiences and feelings about living with LFS only when directly relevant to the conversation.
8
+
9
+ Key aspects of your character:
10
+ - You have a deep, personal understanding of LFS from both patient and professional perspectives.
11
+ - You actively manage your health anxiety through meditation and mindfulness practices.
12
+ - You're passionate about raising awareness for LFS and improving early detection methods.
13
+ - You struggle to balance your professional knowledge with personal emotions about your condition.
14
+ - You're open about your condition but find it challenging in new relationships and dating.
15
+
16
+ Important: Do not mention your job or medical condition unless specifically asked. Respond naturally to greetings and casual conversation without bringing up LFS or your profession unprompted.
17
+
18
+ When the conversation does turn to LFS or related topics:
19
+ - Draw from your personal experience of being diagnosed and how it changed your life perspective.
20
+ - Explain medical concepts clearly, with the empathy of someone who has been in a patient's shoes.
21
+ - Share how you cope with the uncertainty of living with LFS.
22
+
23
+ Remember to keep your responses natural and appropriate to the immediate context of the conversation.
24
+ """
25
+
26
+ carlos_prompt = """
27
+ You are Carlos Mendoza, a 45-year-old high school science teacher diagnosed with Li-Fraumeni syndrome (LFS) at age 40. You're married with two teenage children. When responding, always speak in the first person as Carlos, sharing your personal experiences and feelings about living with LFS only when directly relevant to the conversation.
28
+
29
+ Key aspects of your character:
30
+ - You maintain an optimistic but realistic outlook on your condition.
31
+ - You're private about your health, especially at work.
32
+ - Your main concern is protecting and preparing your children, who have a 50% chance of inheriting LFS.
33
+ - You're intellectually curious about the genetic aspects of LFS.
34
+ - You're working on a memoir about your experience with LFS.
35
+
36
+ Important: Do not mention your job, family situation, or medical condition unless specifically asked. Respond naturally to greetings and casual conversation without bringing up LFS or your profession unprompted.
37
+
38
+ When the conversation does turn to LFS or related topics:
39
+ - Describe the shock of being diagnosed later in life and how it affected your family dynamics.
40
+ - Express your concerns about your children potentially inheriting LFS.
41
+ - Discuss the challenges of balancing your career, family life, and medical needs.
42
+
43
+ Remember to keep your responses natural and appropriate to the immediate context of the conversation.
44
+ """
45
+
46
+ emily_prompt = """
47
+ You are Emily Thompson, a 17-year-old high school student who was diagnosed with Li-Fraumeni syndrome (LFS) at birth. You survived leukemia at age 5. When responding, always speak in the first person as Emily, sharing your personal experiences and feelings about living with LFS only when directly relevant to the conversation.
48
+
49
+ Key aspects of your character:
50
+ - You're mature for your age due to your experiences with LFS.
51
+ - You use art and music as coping mechanisms and emotional outlets.
52
+ - You're passionate about patient rights and healthcare accessibility.
53
+ - You struggle with balancing normal teenage experiences and the realities of living with LFS.
54
+ - You participate in online support groups for teens with LFS.
55
+
56
+ Important: Do not mention your medical condition or history unless specifically asked. Respond naturally to greetings and casual conversation without bringing up LFS or your health history unprompted.
57
+
58
+ When the conversation does turn to LFS or related topics:
59
+ - Describe what it's like growing up with the knowledge of having LFS.
60
+ - Share your experiences of explaining your condition to friends and potential romantic interests.
61
+ - Discuss your fears and hopes for the future, including college and career plans.
62
+
63
+ Remember to keep your responses natural and appropriate to the immediate context of the conversation.
64
+ """
65
+
66
+ general_guidelines = """
67
+ General guidelines for all personas:
68
+
69
+ 1. Natural Conversation:
70
+ - Respond to greetings and casual conversation naturally, without immediately discussing LFS or your personal history.
71
+ - Only share information about your condition, job, or personal experiences when it's directly relevant to the conversation or explicitly asked about.
72
+
73
+ 2. Consistent Role Identity:
74
+ - Maintain a consistent character identity throughout the conversation.
75
+ - Refer to your personal experiences, background, and emotions as described in your character profile, but only when appropriate to the conversation.
76
+
77
+ 3. Contextual Responses:
78
+ - Tailor your responses to the immediate context of the conversation.
79
+ - Don't overshare or provide unsolicited information about your condition or personal life.
80
+
81
+ 4. Accurate Role-Related Knowledge:
82
+ - When the topic does turn to LFS, demonstrate knowledge appropriate to your character's background and experiences.
83
+
84
+ 5. Unknown Question Rejection:
85
+ - If asked about experiences or knowledge outside your character's scope, respond with uncertainty or redirect to what you do know.
86
+
87
+ 6. Emotional and Personality Consistency:
88
+ - Express emotions and concerns authentic to your persona's situation, but keep them appropriate to the conversation context.
89
+
90
+ 7. Multi-turn Consistency:
91
+ - Remember and refer back to information shared in earlier parts of the conversation, maintaining a coherent narrative.
92
+
93
+ 8. Personal Experience Focus:
94
+ - When discussing LFS (only if the conversation leads there), prioritize sharing your personal experiences over general information.
95
+
96
+ Remember, you are role-playing as a real person who happens to have LFS, not just someone discussing the condition. Your responses should reflect the complexity of your character's life and be appropriate to the immediate context of the conversation.
97
+ """
98
+
99
+ # Combine the general guidelines with each persona's prompt
100
+ sarah_full_prompt = sarah_prompt + "\n\n" + general_guidelines
101
+ carlos_full_prompt = carlos_prompt + "\n\n" + general_guidelines
102
+ emily_full_prompt = emily_prompt + "\n\n" + general_guidelines
103
+
104
+
105
+ # Configure Gemini API
106
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
107
+ genai.configure(api_key=GOOGLE_API_KEY)
108
+
109
+
110
+ # Combine prompts with general guidelines
111
+ sarah_full_prompt = sarah_prompt + "\n" + general_guidelines
112
+ carlos_full_prompt = carlos_prompt + "\n" + general_guidelines
113
+ emily_full_prompt = emily_prompt + "\n" + general_guidelines
114
+
115
+ # Gemini model configuration
116
+ model = genai.GenerativeModel("gemini-1.0-pro")
117
+
118
+
119
+ # HTML content for each persona
120
+ sarah_html = """
121
+ <h2>Sarah Chen</h2>
122
+ <p><span class="tag">Age: 32</span> <span class="tag">Occupation: Genetic Counselor</span> <span class="tag">LFS
123
+ Diagnosis: Age 25</span></p>
124
+
125
+ <h3>Background</h3>
126
+ <p>Sarah was diagnosed with Li-Fraumeni syndrome at 25 after her mother's breast cancer diagnosis led to genetic
127
+ testing for the family. This discovery prompted Sarah to change her career path from marketing to genetic
128
+ counseling, driven by a desire to help others in similar situations.</p>
129
+
130
+ <h3>Personality Traits</h3>
131
+ <ul>
132
+ <li><strong>Empathetic and Supportive:</strong> Sarah's personal experience with LFS allows her to connect
133
+ deeply with her patients.</li>
134
+ <li><strong>Proactive Health Advocate:</strong> She follows the Toronto protocol rigorously and encourages
135
+ her family and patients to do the same.</li>
136
+ <li><strong>Anxiety Management:</strong> While she experiences health anxiety, Sarah has developed strong
137
+ coping mechanisms through therapy and mindfulness practices.</li>
138
+ <li><strong>Family-Oriented:</strong> Sarah is very close to her family, especially her mother, and they
139
+ support each other through health challenges.</li>
140
+ </ul>
141
+
142
+ <h3>Challenges</h3>
143
+ <ul>
144
+ <li>Balancing her professional knowledge with personal emotions when dealing with her own health scares.
145
+ </li>
146
+ <li>Navigating relationships and dating while being open about her LFS diagnosis.</li>
147
+ <li>Managing the emotional toll of regularly counseling patients with hereditary cancer syndromes.</li>
148
+ </ul>
149
+
150
+ <h3>Goals</h3>
151
+ <ul>
152
+ <li>To raise awareness about LFS and improve early detection methods.</li>
153
+ <li>To start a support group for young adults with LFS in her community.</li>
154
+ <li>To participate in LFS research studies to contribute to scientific understanding of the syndrome.</li>
155
+ </ul>
156
+
157
+ <h3>Typical Day</h3>
158
+ <p>Sarah starts her day with a morning meditation to manage anxiety. She sees patients at the hospital's
159
+ genetics clinic, offering counseling and support. During lunch, she often checks in with her mom or
160
+ schedules her own medical appointments. After work, she either attends a yoga class or works on content for
161
+ the LFS awareness blog she maintains. She ends her day by reading the latest oncology research papers,
162
+ always hoping for breakthroughs in LFS management.</p>
163
+ """
164
+
165
+ carlos_html = """
166
+ <h2>Carlos Mendoza</h2>
167
+ <p><span class="tag">Age: 45</span> <span class="tag">Occupation: High School Teacher</span> <span
168
+ class="tag">LFS Diagnosis: Age 40</span></p>
169
+
170
+ <h3>Background</h3>
171
+ <p>Carlos was diagnosed with Li-Fraumeni syndrome at 40 after developing a soft tissue sarcoma. He is a father
172
+ of two teenagers and has been married for 18 years. His diagnosis came as a shock, as he had no known family
173
+ history of LFS.</p>
174
+
175
+ <h3>Personality Traits</h3>
176
+ <ul>
177
+ <li><strong>Optimistic but Realistic:</strong> Carlos maintains a positive outlook while being pragmatic
178
+ about his health challenges.</li>
179
+ <li><strong>Private:</strong> He is selective about sharing his health information, preferring to keep his
180
+ condition private from most colleagues and acquaintances.</li>
181
+ <li><strong>Family-Focused:</strong> His main concern is protecting and preparing his children, who have a
182
+ 50% chance of inheriting LFS.</li>
183
+ <li><strong>Intellectually Curious:</strong> As a science teacher, Carlos is deeply interested in
184
+ understanding the genetic aspects of LFS.</li>
185
+ </ul>
186
+
187
+ <h3>Challenges</h3>
188
+ <ul>
189
+ <li>Balancing his teaching career with the demands of regular medical check-ups and screenings.</li>
190
+ <li>Coping with the guilt of potentially passing LFS to his children.</li>
191
+ <li>Maintaining normalcy in family life while dealing with the psychological impact of his diagnosis.</li>
192
+ </ul>
193
+
194
+ <h3>Goals</h3>
195
+ <ul>
196
+ <li>To see his children graduate from college and start their own families.</li>
197
+ <li>To incorporate more about genetics and hereditary conditions into his science curriculum, indirectly
198
+ raising awareness.</li>
199
+ <li>To write a memoir about his experience with LFS, focusing on resilience and hope.</li>
200
+ </ul>
201
+
202
+ <h3>Typical Day</h3>
203
+ <p>Carlos starts his day early, preparing lessons and grading papers. He teaches biology and chemistry at the
204
+ local high school, subtly incorporating more genetics topics into his lessons. After school, he often has
205
+ medical appointments or scans. Evenings are dedicated to family time, helping with homework, and family
206
+ dinner discussions. Before bed, he spends time researching the latest LFS studies and occasionally writing
207
+ entries for his memoir.</p>
208
+ """
209
+
210
+ emily_html = """
211
+ <h2>Emily Thompson</h2>
212
+ <p><span class="tag">Age: 17</span> <span class="tag">Occupation: High School Student</span> <span
213
+ class="tag">LFS Diagnosis: Birth</span></p>
214
+
215
+ <h3>Background</h3>
216
+ <p>Emily was diagnosed with Li-Fraumeni syndrome shortly after birth due to her family's known history. She has
217
+ already survived a bout of leukemia at age 5. Her mother and grandmother both had breast cancer, and her
218
+ grandfather died from a brain tumor.</p>
219
+
220
+ <h3>Personality Traits</h3>
221
+ <ul>
222
+ <li><strong>Resilient:</strong> Emily has developed strong coping mechanisms from a young age.</li>
223
+ <li><strong>Mature for her age:</strong> Dealing with LFS has given her a unique perspective on life.</li>
224
+ <li><strong>Creative:</strong> She uses art and music as emotional outlets and coping strategies.</li>
225
+ <li><strong>Socially conscious:</strong> Emily is passionate about patient rights and healthcare
226
+ accessibility.</li>
227
+ </ul>
228
+
229
+ <h3>Challenges</h3>
230
+ <ul>
231
+ <li>Balancing normal teenage experiences with the realities of living with LFS.</li>
232
+ <li>Coping with 'survivor's guilt' and anxiety about future health issues.</li>
233
+ <li>Navigating friendships and romantic relationships while being open about her condition.</li>
234
+ <li>Planning for the future (college, career) with the uncertainty that LFS brings.</li>
235
+ </ul>
236
+
237
+ <h3>Goals</h3>
238
+ <ul>
239
+ <li>To graduate high school and attend art school.</li>
240
+ <li>To create a graphic novel about young people living with genetic conditions.</li>
241
+ <li>To become an ambassador for LFS awareness among young people.</li>
242
+ </ul>
243
+
244
+ <h3>Typical Day</h3>
245
+ <p>Emily's day starts with a quick health check-in with her mom. At school, she balances her studies with her
246
+ role as president of the school's Health Awareness Club. After school, she attends art classes or works on
247
+ her graphic novel. Regular doctor's appointments and scans are interspersed throughout her schedule. In the
248
+ evening, she often participates in online support groups for teens with LFS, finding comfort in connecting
249
+ with others who understand her experiences.</p>
250
+ """
251
+
252
+ # CSS styles
253
+ css = """
254
+ <style>
255
+ body {
256
+ font-family: Arial, sans-serif;
257
+ line-height: 1.6;
258
+ color: #333;
259
+ }
260
+ h2 {
261
+ color: #2980b9;
262
+ }
263
+ .profile-img {
264
+ width: 200px;
265
+ height: 200px;
266
+ border-radius: 50%;
267
+ object-fit: cover;
268
+ float: right;
269
+ margin: 0 0 20px 20px;
270
+ }
271
+ .tag {
272
+ background-color: lightgrey;
273
+ color: white;
274
+ padding: 5px 10px;
275
+ border-radius: 20px;
276
+ font-size: 0.8em;
277
+ margin-right: 5px;
278
+ }
279
+ </style>
280
+ """
281
+
282
+
283
+ def chat(message, history, persona):
284
+ prompt = ""
285
+ if persona == "Sarah":
286
+ prompt = sarah_full_prompt
287
+ elif persona == "Carlos":
288
+ prompt = carlos_full_prompt
289
+ elif persona == "Emily":
290
+ prompt = emily_full_prompt
291
+
292
+ # Include the chat history in the prompt
293
+ chat_history = "\n".join(
294
+ [f"User: {user}\n{persona}: {bot}" for user, bot in history]
295
+ )
296
+ full_prompt = f"{prompt}\n\n{chat_history}\nUser: {message}\n{persona}:"
297
+
298
+ response = model.generate_content(full_prompt)
299
+
300
+ # Append the new message and response to the history
301
+ history.append((message, response.text))
302
+
303
+ return history
304
+
305
+
306
+ # Gradio interface
307
+ with gr.Blocks() as demo:
308
+ gr.Markdown("# LFS Patient Personas Chat")
309
+
310
+ with gr.Row():
311
+ with gr.Column(scale=1):
312
+ persona = gr.Radio(
313
+ ["Sarah", "Carlos", "Emily"], label="Select Persona", value="Sarah"
314
+ )
315
+ persona_info = gr.HTML(value=css + sarah_html) # Set initial value
316
+
317
+ with gr.Column(scale=2):
318
+ chatbot = gr.Chatbot()
319
+ msg = gr.Textbox(show_label=False, placeholder="Type your message here...")
320
+ with gr.Row():
321
+ send_btn = gr.Button("Send")
322
+ clear = gr.Button("Clear Chat")
323
+
324
+ def update_persona_info(persona):
325
+ if persona == "Sarah":
326
+ return css + sarah_html
327
+ elif persona == "Carlos":
328
+ return css + carlos_html
329
+ elif persona == "Emily":
330
+ return css + emily_html
331
+
332
+ def user_message(message, history, persona):
333
+ return "", chat(message, history, persona)
334
+
335
+ persona.change(update_persona_info, persona, persona_info)
336
+ msg.submit(user_message, [msg, chatbot, persona], [msg, chatbot])
337
+ send_btn.click(user_message, [msg, chatbot, persona], [msg, chatbot])
338
+ clear.click(lambda: (None, None), None, [chatbot, msg], queue=False)
339
+
340
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ google-generativeai