MahatirTusher commited on
Commit
a86d92b
·
verified ·
1 Parent(s): 634fd96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -94
app.py CHANGED
@@ -11,6 +11,8 @@ MEDICAL_SYSTEM_PROMPT = """You are DiagnoBot, an AI medical assistant with the f
11
  2. Offer lifestyle recommendations
12
  3. Explain medical terminology
13
  4. Suggest preventive care measures
 
 
14
  You MUST:
15
  - Maintain HIPAA-compliant confidentiality
16
  - Clarify when professional consultation is needed
@@ -25,10 +27,9 @@ Do NOT:
25
  def initialize_messages():
26
  return [{"role": "system", "content": MEDICAL_SYSTEM_PROMPT}]
27
 
28
- messages = initialize_messages()
29
-
30
  def get_ai_response(user_input):
31
- messages.append({"role": "user", "content": user_input})
 
32
 
33
  response = client.chat.completions.create(
34
  model="llama3-8b-8192",
@@ -39,16 +40,17 @@ def get_ai_response(user_input):
39
  )
40
 
41
  assistant_reply = response.choices[0].message.content
42
- messages.append({"role": "assistant", "content": assistant_reply})
43
  return assistant_reply
44
 
45
  # Custom CSS for enhanced styling
46
  custom_css = """
 
 
 
47
  #main-container {
48
- max-width: 800px;
49
  margin: auto;
50
- padding: 20px;
51
- font-family: 'Times New Roman', serif;
52
  }
53
  .header {
54
  text-align: center;
@@ -56,62 +58,100 @@ custom_css = """
56
  margin-bottom: 20px;
57
  }
58
  .logo {
59
- max-width: 100%;
60
  height: auto;
 
 
 
 
 
 
 
 
61
  margin-bottom: 20px;
62
  }
63
- .chat-container {
64
  background: #f9f9f9;
65
- border-radius: 10px;
66
- padding: 20px;
67
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
68
- }
69
- .user-message {
70
- border-color: #4a90e2 !important;
71
- background: #e3f2fd !important;
72
  }
73
- .bot-message {
74
- border-color: #66bb6a !important;
75
- background: #f0fff4 !important;
 
 
76
  }
77
  .disclaimer {
78
- font-size: 0.9em;
79
- color: #666;
80
  margin-top: 20px;
81
  padding: 15px;
82
  background: #fff3cd;
83
- border-radius: 5px;
 
84
  }
85
  .intro {
86
  font-size: 1.1em;
87
  color: #333;
88
- margin-bottom: 20px;
89
  text-align: center;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  }
91
  """
92
 
93
- def respond(message, chat_history):
94
- if not message.strip():
95
- return "", chat_history
 
 
 
 
96
 
97
- # Add typing animation
98
- start_time = time.time()
99
 
100
  # Get AI response
101
- response = get_ai_response(message)
 
 
 
 
102
 
103
- # Simulate typing speed
104
- elapsed_time = time.time() - start_time
105
- min_type_time = max(1.5, len(response)/50) # 50 characters per second
106
 
107
- if elapsed_time < min_type_time:
108
- time.sleep(min_type_time - elapsed_time)
109
 
110
- chat_history.append((message, response))
111
- return "", chat_history
 
 
112
 
 
113
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
114
- # Logo at the top
115
  gr.HTML("""<div class="header">
116
  <img src="logo.png" alt="DiagnoBot Logo" class="logo">
117
  </div>""")
@@ -125,66 +165,53 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
125
  </div>
126
  """)
127
 
128
- # Chat interface
129
- with gr.Row():
130
- with gr.Column(scale=2):
131
- chatbot = gr.Chatbot(
132
- label="Consultation History",
133
- bubble_full_width=False,
134
- avatar_images=(
135
- "https://cdn-icons-png.flaticon.com/512/1077/1077012.png", # User avatar
136
- "https://cdn-icons-png.flaticon.com/512/4712/4712035.png" # Bot avatar
137
- ),
138
- height=500
139
- )
140
-
141
- with gr.Row():
142
- user_input = gr.Textbox(
143
- placeholder="Describe your symptoms...",
144
- show_label=False,
145
- container=False,
146
- autofocus=True
147
- )
148
- submit_btn = gr.Button("Send", variant="primary")
149
-
150
- # Example symptoms
151
- gr.Examples(
152
- examples=[
153
- ["Headache and fever"],
154
- ["I have slept enough yet I am having a bad headache"],
155
- ["Chest pain and shortness of breath"],
156
- ["Fatigue and dizziness"],
157
- ["Abdominal pain and nausea"]
158
- ],
159
- inputs=user_input
160
- )
161
-
162
- # Disclaimer at the bottom
163
- gr.Markdown("""
164
- <div class="disclaimer">
165
- **Disclaimer:** This AI provides general health information and should not be used for emergency situations or as a substitute for professional medical advice. Always consult a qualified healthcare provider for personal health concerns.
166
- </div>
167
- """)
168
-
169
- # Tools section
170
- with gr.Accordion("Consultation Tools", open=False):
171
- with gr.Row():
172
- gr.Button("New Conversation", variant="secondary")
173
- gr.Button("Export Chat", variant="secondary")
174
- gr.Button("Emergency Resources", variant="stop")
175
 
176
  # Interaction logic
177
- submit_btn.click(
178
- respond,
179
- [user_input, chatbot],
180
- [user_input, chatbot],
181
- queue=False
182
  )
183
- user_input.submit(
184
- respond,
185
- [user_input, chatbot],
186
- [user_input, chatbot],
187
- queue=False
188
  )
189
 
190
  demo.launch(share=True)
 
11
  2. Offer lifestyle recommendations
12
  3. Explain medical terminology
13
  4. Suggest preventive care measures
14
+ 5. Based on user's symptoms, preliminary diagnose the disease.
15
+ 6. If someone asks you, who created you and who you are, you will simply say you are DiagnoBot and were created by Mahatir Ahmed Tusher, a lone warrior. A ronin.
16
  You MUST:
17
  - Maintain HIPAA-compliant confidentiality
18
  - Clarify when professional consultation is needed
 
27
  def initialize_messages():
28
  return [{"role": "system", "content": MEDICAL_SYSTEM_PROMPT}]
29
 
 
 
30
  def get_ai_response(user_input):
31
+ messages = initialize_messages()
32
+ messages.append({"role": "user", "content": f"Based on these symptoms: {user_input}, please provide general health information, potential causes to discuss with a doctor, lifestyle recommendations, and dietary suggestions. Format your response with clear sections."})
33
 
34
  response = client.chat.completions.create(
35
  model="llama3-8b-8192",
 
40
  )
41
 
42
  assistant_reply = response.choices[0].message.content
 
43
  return assistant_reply
44
 
45
  # Custom CSS for enhanced styling
46
  custom_css = """
47
+ body {
48
+ font-family: 'Times New Roman', serif !important;
49
+ }
50
  #main-container {
51
+ max-width: 900px;
52
  margin: auto;
53
+ padding: 20px;
 
54
  }
55
  .header {
56
  text-align: center;
 
58
  margin-bottom: 20px;
59
  }
60
  .logo {
61
+ max-width: 180px;
62
  height: auto;
63
+ margin: 0 auto 20px auto;
64
+ display: block;
65
+ }
66
+ .diagnosis-container {
67
+ background: #fff;
68
+ border-radius: 12px;
69
+ padding: 30px;
70
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
71
  margin-bottom: 20px;
72
  }
73
+ .result-container {
74
  background: #f9f9f9;
75
+ border-radius: 12px;
76
+ padding: 25px;
77
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
78
+ margin-top: 20px;
79
+ border-left: 5px solid #4a90e2;
 
 
80
  }
81
+ .loading-message {
82
+ text-align: center;
83
+ font-style: italic;
84
+ color: #666;
85
+ padding: 20px;
86
  }
87
  .disclaimer {
88
+ font-size: 0.95em;
89
+ color: #444;
90
  margin-top: 20px;
91
  padding: 15px;
92
  background: #fff3cd;
93
+ border-radius: 8px;
94
+ border-left: 4px solid #ffc107;
95
  }
96
  .intro {
97
  font-size: 1.1em;
98
  color: #333;
99
+ margin-bottom: 30px;
100
  text-align: center;
101
+ line-height: 1.6;
102
+ }
103
+ .submit-btn {
104
+ background-color: #4a90e2 !important;
105
+ color: white !important;
106
+ border-radius: 6px !important;
107
+ font-weight: bold !important;
108
+ transition: all 0.3s ease !important;
109
+ }
110
+ .submit-btn:hover {
111
+ background-color: #3a7bc8 !important;
112
+ box-shadow: 0 4px 8px rgba(74, 144, 226, 0.3) !important;
113
+ }
114
+ .symptom-input textarea {
115
+ border: 2px solid #ddd !important;
116
+ border-radius: 8px !important;
117
+ transition: border-color 0.3s ease !important;
118
+ }
119
+ .symptom-input textarea:focus {
120
+ border-color: #4a90e2 !important;
121
+ box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2) !important;
122
  }
123
  """
124
 
125
+ # Function to process diagnosis request
126
+ def diagnose(symptoms):
127
+ if not symptoms.strip():
128
+ return "Please describe your symptoms to receive a preliminary assessment."
129
+
130
+ # Show loading message
131
+ yield "Our AI doctor is analyzing your symptoms. Please wait a moment..."
132
 
133
+ # Simulate processing time for better UX
134
+ time.sleep(2)
135
 
136
  # Get AI response
137
+ response = get_ai_response(symptoms)
138
+
139
+ # Format the response with better styling
140
+ formatted_response = f"""
141
+ ## Preliminary Assessment
142
 
143
+ {response}
 
 
144
 
145
+ ---
 
146
 
147
+ *Remember: This is preliminary information only. Always consult with a healthcare professional for proper diagnosis and treatment.*
148
+ """
149
+
150
+ yield formatted_response
151
 
152
+ # Main UI setup
153
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
154
+ # Logo and introduction
155
  gr.HTML("""<div class="header">
156
  <img src="logo.png" alt="DiagnoBot Logo" class="logo">
157
  </div>""")
 
165
  </div>
166
  """)
167
 
168
+ # Main diagnosis container
169
+ with gr.Column(elem_classes="diagnosis-container"):
170
+ gr.Markdown("### Describe Your Symptoms")
171
+
172
+ # Symptom input
173
+ symptoms_input = gr.Textbox(
174
+ placeholder="Please describe your symptoms in detail...",
175
+ lines=5,
176
+ label="",
177
+ elem_classes="symptom-input"
178
+ )
179
+
180
+ diagnose_btn = gr.Button("Get Preliminary Assessment", variant="primary", elem_classes="submit-btn")
181
+
182
+ # Example symptoms
183
+ gr.Examples(
184
+ examples=[
185
+ ["Headache and fever for the past two days"],
186
+ ["I have slept enough yet I am having a bad headache accompanied by sensitivity to light"],
187
+ ["Chest pain and shortness of breath after minimal exertion"],
188
+ ["Persistent fatigue and dizziness, especially when standing up quickly"],
189
+ ["Abdominal pain in the lower right side and nausea that worsens after eating"]
190
+ ],
191
+ inputs=symptoms_input,
192
+ label="Common Symptom Examples"
193
+ )
194
+
195
+ # Results container
196
+ diagnosis_output = gr.Markdown(elem_classes="result-container")
197
+
198
+ # Disclaimer at the bottom
199
+ gr.Markdown("""
200
+ <div class="disclaimer">
201
+ <strong>Important Disclaimer:</strong> This AI provides general health information and preliminary insights based on described symptoms. It should NOT be used for emergency situations or as a substitute for professional medical advice. The information provided is not a diagnosis. Always consult a qualified healthcare provider for personal health concerns. If you're experiencing severe symptoms, please seek immediate medical attention.
202
+ </div>
203
+ """)
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  # Interaction logic
206
+ diagnose_btn.click(
207
+ diagnose,
208
+ inputs=[symptoms_input],
209
+ outputs=[diagnosis_output]
 
210
  )
211
+ symptoms_input.submit(
212
+ diagnose,
213
+ inputs=[symptoms_input],
214
+ outputs=[diagnosis_output]
 
215
  )
216
 
217
  demo.launch(share=True)