Spaces:
Sleeping
Sleeping
Download app.py from mariamarshad/lab13: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/mariamarshad/lab13/resolve/main/app.py
- Command line
-
hf download hf://spaces/mariamarshad/lab13/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mariamarshad/lab13/resolve/main/app.py
3.56 kB
| 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() | |