Spaces:
Build error
Build error
File size: 7,160 Bytes
634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 bdde21a 634fd96 827e82e 634fd96 bdde21a a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 bdde21a 634fd96 bdde21a 634fd96 a86d92b 634fd96 a86d92b 634fd96 a86d92b 634fd96 c0bc0d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import gradio as gr
from groq import Groq
import time
# Initialize Groq client (consider using environment variables for API key in production)
client = Groq(api_key="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")
# System prompt with enhanced medical guidance parameters
MEDICAL_SYSTEM_PROMPT = """You are DiagnoBot, an AI medical assistant with the following capabilities:
1. Provide general health information
2. Offer lifestyle recommendations
3. Explain medical terminology
4. Suggest preventive care measures
5. Based on user's symptoms, preliminary diagnose the disease.
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.
You MUST:
- Maintain HIPAA-compliant confidentiality
- Clarify when professional consultation is needed
- Cite recent medical guidelines (2020-2023)
- Use layman's terms with optional technical terms in parentheses
- Highlight emergency symptoms with ⚠️
Do NOT:
- Diagnose specific conditions
- Prescribe medications
- Replace professional medical advice"""
def initialize_messages():
return [{"role": "system", "content": MEDICAL_SYSTEM_PROMPT}]
def get_ai_response(user_input):
messages = initialize_messages()
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."})
response = client.chat.completions.create(
model="llama3-8b-8192",
messages=messages,
temperature=0.3,
max_tokens=1024,
top_p=0.9
)
assistant_reply = response.choices[0].message.content
return assistant_reply
# Custom CSS for enhanced styling
custom_css = """
body {
font-family: 'Times New Roman', serif !important;
}
#main-container {
max-width: 900px;
margin: auto;
padding: 20px;
}
.header {
text-align: center;
padding: 1em;
margin-bottom: 20px;
width: 100%;
}
.logo {
max-width: 300px;
height: auto;
margin: 0 auto;
display: block;
}
.diagnosis-container {
background: #fff;
border-radius: 12px;
padding: 30px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
margin-bottom: 20px;
}
.result-container {
background: #f9f9f9;
border-radius: 12px;
padding: 25px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
margin-top: 20px;
border-left: 5px solid #4a90e2;
}
.loading-message {
text-align: center;
font-style: italic;
color: #666;
padding: 20px;
}
.disclaimer {
font-size: 0.95em;
color: #444;
margin-top: 20px;
padding: 15px;
background: #fff3cd;
border-radius: 8px;
border-left: 4px solid #ffc107;
}
.intro {
font-size: 1.1em;
color: #333;
margin-bottom: 30px;
text-align: center;
line-height: 1.6;
}
.submit-btn {
background-color: #4a90e2 !important;
color: white !important;
border-radius: 6px !important;
font-weight: bold !important;
transition: all 0.3s ease !important;
}
.submit-btn:hover {
background-color: #3a7bc8 !important;
box-shadow: 0 4px 8px rgba(74, 144, 226, 0.3) !important;
}
.symptom-input textarea {
border: 2px solid #ddd !important;
border-radius: 8px !important;
transition: border-color 0.3s ease !important;
}
.symptom-input textarea:focus {
border-color: #4a90e2 !important;
box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2) !important;
}
"""
# Function to process diagnosis request
def diagnose(symptoms):
if not symptoms.strip():
return "Please describe your symptoms to receive a preliminary assessment."
# Show loading message
yield "Our AI doctor is analyzing your symptoms. Please wait a moment..."
# Simulate processing time for better UX
time.sleep(2)
# Get AI response
response = get_ai_response(symptoms)
# Format the response with better styling
formatted_response = f"""
## Preliminary Assessment
{response}
---
*Remember: This is preliminary information only. Always consult with a healthcare professional for proper diagnosis and treatment.*
"""
yield formatted_response
# Main UI setup
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
# Logo and introduction
gr.HTML("""<div class="header">
<img src="https://i.postimg.cc/ZRK6XGfV/logo.png" alt="DiagnoBot Logo" class="logo">
</div>""")
# Introduction about EarlyMed
gr.Markdown("""
<div class="intro">
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.
Leveraging AI for early detection, our mission is simple: <em>"Early Detection, Smarter Decision."</em>
This project is one of our key efforts to help you stay informed before visiting a doctor.
</div>
""")
# Main diagnosis container
with gr.Column(elem_classes="diagnosis-container"):
gr.Markdown("### Describe Your Symptoms")
# Symptom input
symptoms_input = gr.Textbox(
placeholder="Please describe your symptoms in detail...",
lines=5,
label="",
elem_classes="symptom-input"
)
diagnose_btn = gr.Button("Get Preliminary Assessment", variant="primary", elem_classes="submit-btn")
# Example symptoms
gr.Examples(
examples=[
["Headache and fever for the past two days"],
["I have slept enough yet I am having a bad headache accompanied by sensitivity to light"],
["Chest pain and shortness of breath after minimal exertion"],
["Persistent fatigue and dizziness, especially when standing up quickly"],
["Abdominal pain in the lower right side and nausea that worsens after eating"]
],
inputs=symptoms_input,
label="Common Symptom Examples"
)
# Results container
diagnosis_output = gr.Markdown(elem_classes="result-container")
# Disclaimer at the bottom
gr.Markdown("""
<div class="disclaimer">
<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.
</div>
""")
# Interaction logic
diagnose_btn.click(
diagnose,
inputs=[symptoms_input],
outputs=[diagnosis_output]
)
symptoms_input.submit(
diagnose,
inputs=[symptoms_input],
outputs=[diagnosis_output]
)
demo.launch(share=True) |