Spaces:
Build error
Build error
Update app.py
Browse files
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
|
|
|
|
| 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:
|
| 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:
|
| 60 |
height: auto;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
margin-bottom: 20px;
|
| 62 |
}
|
| 63 |
-
.
|
| 64 |
background: #f9f9f9;
|
| 65 |
-
border-radius:
|
| 66 |
-
padding:
|
| 67 |
-
box-shadow: 0 4px
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
border-color: #4a90e2 !important;
|
| 71 |
-
background: #e3f2fd !important;
|
| 72 |
}
|
| 73 |
-
.
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
.disclaimer {
|
| 78 |
-
font-size: 0.
|
| 79 |
-
color: #
|
| 80 |
margin-top: 20px;
|
| 81 |
padding: 15px;
|
| 82 |
background: #fff3cd;
|
| 83 |
-
border-radius:
|
|
|
|
| 84 |
}
|
| 85 |
.intro {
|
| 86 |
font-size: 1.1em;
|
| 87 |
color: #333;
|
| 88 |
-
margin-bottom:
|
| 89 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
|
| 100 |
# Get AI response
|
| 101 |
-
response = get_ai_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
elapsed_time = time.time() - start_time
|
| 105 |
-
min_type_time = max(1.5, len(response)/50) # 50 characters per second
|
| 106 |
|
| 107 |
-
|
| 108 |
-
time.sleep(min_type_time - elapsed_time)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
|
|
|
| 113 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 114 |
-
# Logo
|
| 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 |
-
#
|
| 129 |
-
with gr.
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 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 |
-
|
| 178 |
-
|
| 179 |
-
[
|
| 180 |
-
[
|
| 181 |
-
queue=False
|
| 182 |
)
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
[
|
| 186 |
-
[
|
| 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)
|