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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# Initialize Groq client (consider using environment variables for API key in production)
|
| 6 |
+
client = Groq(api_key="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")
|
| 7 |
+
|
| 8 |
+
# System prompt with enhanced medical guidance parameters
|
| 9 |
+
MEDICAL_SYSTEM_PROMPT = """You are DiagnoBot, an AI medical assistant with the following capabilities:
|
| 10 |
+
1. Provide general health information
|
| 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
|
| 17 |
+
- Cite recent medical guidelines (2020-2023)
|
| 18 |
+
- Use layman's terms with optional technical terms in parentheses
|
| 19 |
+
- Highlight emergency symptoms with ⚠️
|
| 20 |
+
Do NOT:
|
| 21 |
+
- Diagnose specific conditions
|
| 22 |
+
- Prescribe medications
|
| 23 |
+
- Replace professional medical advice"""
|
| 24 |
+
|
| 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",
|
| 35 |
+
messages=messages,
|
| 36 |
+
temperature=0.3,
|
| 37 |
+
max_tokens=1024,
|
| 38 |
+
top_p=0.9
|
| 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;
|
| 55 |
+
padding: 1em;
|
| 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="file/logo.png" alt="DiagnoBot Logo" class="logo">
|
| 117 |
+
</div>""")
|
| 118 |
+
|
| 119 |
+
# Introduction about EarlyMed
|
| 120 |
+
gr.Markdown("""
|
| 121 |
+
<div class="intro">
|
| 122 |
+
DiagnoBot is a side project of <strong>EarlyMed</strong>—an initiative by our team at VIT-AP University dedicated to empowering you with early health insights.
|
| 123 |
+
Leveraging AI for early detection, our mission is simple: <em>"Early Detection, Smarter Decision."</em>
|
| 124 |
+
Our Advanced Alzheimer's Detection System project is one of our key efforts to help you stay informed before visiting a doctor.
|
| 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)
|
logo.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.12.0
|
| 2 |
+
groq==0.1.0
|
| 3 |
+
requests==2.31.0
|
| 4 |
+
typing_extensions==4.8.0
|
| 5 |
+
numpy==1.24.4
|
| 6 |
+
pydantic==2.5.2
|