Spaces:
Sleeping
Sleeping
File size: 3,564 Bytes
dcc76b9 a4c9eb7 bc09c61 a4c9eb7 bc09c61 a4c9eb7 858d126 a4c9eb7 bc09c61 a4c9eb7 bc09c61 a4c9eb7 bc09c61 a4c9eb7 dcc76b9 a4c9eb7 bc09c61 858d126 dcc76b9 | 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 | import gradio as gr
# Define conditions and associated symptoms with weights
conditions = {
"COVID-19": {
"symptoms": {
"Fever": 2,
"Cough": 2,
"Shortness of Breath": 3,
"Fatigue": 1,
"Sore Throat": 1,
"Body Ache": 1,
"Nausea": 1,
"Diarrhea": 0.5
},
"recommendation": "Get tested immediately, isolate yourself, and consult a doctor."
},
"Flu": {
"symptoms": {
"Fever": 2,
"Cough": 2,
"Headache": 1.5,
"Fatigue": 1,
"Body Ache": 2,
"Sore Throat": 1
},
"recommendation": "Rest, hydrate, and consult a doctor if symptoms worsen."
},
"Migraine": {
"symptoms": {
"Headache": 3,
"Nausea": 1.5,
"Fatigue": 1
},
"recommendation": "Rest in a quiet, dark room and take pain relief medication."
},
"Food Poisoning": {
"symptoms": {
"Nausea": 2,
"Diarrhea": 3,
"Fever": 1,
"Fatigue": 1
},
"recommendation": "Stay hydrated and consult a doctor if symptoms are severe."
},
"Stress / Mild Infection": {
"symptoms": {
"Fatigue": 2,
"Headache": 1.5,
"Body Ache": 1
},
"recommendation": "Rest, hydrate, and reduce stress."
}
}
# List of all symptoms
all_symptoms = ["Fever", "Cough", "Headache", "Fatigue", "Sore Throat",
"Shortness of Breath", "Nausea", "Body Ache", "Diarrhea"]
def dynamic_medical_diagnosis(
fever, cough, headache, fatigue, sore_throat, shortness_of_breath, nausea, body_ache, diarrhea
):
user_symptoms = {
"Fever": fever,
"Cough": cough,
"Headache": headache,
"Fatigue": fatigue,
"Sore Throat": sore_throat,
"Shortness of Breath": shortness_of_breath,
"Nausea": nausea,
"Body Ache": body_ache,
"Diarrhea": diarrhea
}
scores = {}
for condition, info in conditions.items():
score = 0
total_weight = sum(info["symptoms"].values())
for symptom, weight in info["symptoms"].items():
if user_symptoms.get(symptom, False):
score += weight
scores[condition] = round((score / total_weight) * 100, 1)
sorted_conditions = sorted(scores.items(), key=lambda x: x[1], reverse=True)
output = "### 🩺 Diagnosis Results\n"
output += "**Reported Symptoms:**\n"
for symptom, present in user_symptoms.items():
status = "✅ Yes" if present else "❌ No"
output += f"- {symptom}: {status}\n"
output += "\n**Top Possible Conditions:**\n"
for cond, score in sorted_conditions[:3]:
if score > 0:
rec = conditions[cond]["recommendation"]
output += f"- **{cond}**: {score}% likelihood\n 💡 Recommendation: {rec}\n"
if all(score == 0 for cond, score in sorted_conditions):
output += "\n✅ No significant symptoms detected. You seem healthy."
return output
# Gradio interface
interface = gr.Interface(
fn=dynamic_medical_diagnosis,
inputs=[gr.Checkbox(label=symptom) for symptom in all_symptoms],
outputs=gr.Markdown(),
title="🩺 Advanced Medical Diagnosis System",
description="Select all symptoms you are experiencing and click Submit to see the most likely conditions with recommendations."
)
if __name__ == "__main__":
interface.launch()
|