Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345645678991011121313141516171718192021212223242525262728292930313233333435363737383940414142434445454647484949505152535354555657585960616263646465666768696566676869707172727374757677787374757677787980818283828384851 unmodified line878889919091929394959697979899991001011021031041021031041051061071081091101081111121131141151161171181191201141151161211221231241251262 unmodified lines129130131125132133134135129130131132136137138139140141142136137138139143144145146147148149150151152144153154146147148155156157158159151152153154155156157158159160161162160161162163164import gradio as grfrom fpdf import FPDF
# -----------------------------# Disease & Symptom Database# -----------------------------# ----------------------------------------# Disease Database# ----------------------------------------conditions = { "COVID-19": { "symptoms": {"Fever": 2, "Cough": 2, "Shortness of Breath": 3, "Fatigue": 1, "Body Ache": 1}, "symptoms": ["Fever", "Cough", "Shortness of Breath", "Fatigue", "Body Ache"], "recommendation": "Get tested immediately and isolate." }, "Flu": { "symptoms": {"Fever": 2, "Cough": 2, "Headache": 1, "Fatigue": 1, "Body Ache": 2}, "symptoms": ["Fever", "Cough", "Headache", "Fatigue", "Body Ache"], "recommendation": "Rest, hydration, and OTC flu medicine." }, "Pneumonia": { "symptoms": {"Fever": 2, "Cough": 2, "Shortness of Breath": 3, "Chest Pain": 3, "Fatigue": 1}, "symptoms": ["Fever", "Cough", "Shortness of Breath", "Chest Pain", "Fatigue"], "recommendation": "Seek medical evaluation immediately." }, "Sinus Infection": { "symptoms": {"Headache": 2, "Nasal Congestion": 3, "Sore Throat": 1}, "symptoms": ["Headache", "Nasal Congestion", "Sore Throat"], "recommendation": "Steam inhalation and nasal spray." }, "Allergy": { "symptoms": {"Nasal Congestion": 2, "Sore Throat": 1, "Fatigue": 1}, "symptoms": ["Nasal Congestion", "Sore Throat", "Fatigue"], "recommendation": "Take antihistamines if needed." }, "Migraine": { "symptoms": {"Headache": 3, "Nausea": 2, "Fatigue": 1}, "symptoms": ["Headache", "Nausea", "Fatigue"], "recommendation": "Rest in a dark room and hydrate." }, "Food Poisoning": { "symptoms": {"Nausea": 2, "Diarrhea": 3, "Fever": 1}, "symptoms": ["Nausea", "Diarrhea", "Fever"], "recommendation": "Hydrate and avoid solid foods temporarily." }, "Dengue": { "symptoms": {"Fever": 3, "Body Ache": 3, "Headache": 2, "Nausea": 2}, "symptoms": ["Fever", "Body Ache", "Headache", "Nausea"], "recommendation": "Seek medical care (avoid ibuprofen)." }, "Malaria": { "symptoms": {"Fever": 3, "Fatigue": 2, "Nausea": 2, "Body Ache": 3}, "symptoms": ["Fever", "Fatigue", "Nausea", "Body Ache"], "recommendation": "Urgent medical testing required." }, "Gastritis": { "symptoms": {"Nausea": 2, "Abdominal Pain": 3, "Fatigue": 1}, "symptoms": ["Nausea", "Abdominal Pain", "Fatigue"], "recommendation": "Avoid spicy foods and caffeine." }, "Common Cold": { "symptoms": {"Cough": 1, "Sore Throat": 1, "Nasal Congestion": 2}, "symptoms": ["Cough", "Sore Throat", "Nasal Congestion"], "recommendation": "Rest, warm fluids, and vitamin C." }, "Stress / Fatigue": { "symptoms": {"Fatigue": 3, "Headache": 1, "Body Ache": 1}, "symptoms": ["Fatigue", "Headache", "Body Ache"], "recommendation": "Rest, hydrate, relax." }}
# All symptom optionssymptom_list = [ "Fever", "Cough", "Headache", "Fatigue", "Sore Throat", "Shortness of Breath", "Nausea", "Body Ache", "Diarrhea", "Chest Pain", "Nasal Congestion", "Abdominal Pain"]
# -----------------------------# Diagnosis Function# -----------------------------def diagnose(*values): # values = list of slider severities in same order as symptom_list user_symptoms = dict(zip(symptom_list, values))# ----------------------------------------# Diagnosis Logic# ----------------------------------------def diagnose(selected_symptoms): if not selected_symptoms: return "β οΈ Please select at least one symptom.", ""
scores = {} for cond, info in conditions.items(): total_weight = sum(info["symptoms"].values()) score = 0 for symptom, weight in info["symptoms"].items(): score += user_symptoms.get(symptom, 0) * weight
scores[cond] = round((score / total_weight) * 100, 1) for disease, info in conditions.items(): total = len(info["symptoms"]) match = len(set(selected_symptoms) & set(info["symptoms"])) score = round((match / total) * 100, 1) # Now max = 100% scores[disease] = score
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True) best_disease, best_score = sorted_scores[0]
best_cond, best_score = sorted_scores[0]
# Risk Levels if best_score < 25: risk = "π’ LOW RISK" elif best_score < 60:1 unmodified line else: risk = "π΄ HIGH RISK"
output = f"## π©Ί Diagnosis Results\n" output = "## π©Ί Diagnosis Results\n" output += f"### Overall Risk Level: **{risk}**\n\n" output += "### Top Possible Conditions:\n"
for cond, score in sorted_scores[:4]: output += f"- **{cond}** β {score}%\n"
output += f"\n### Recommendation:\n**{conditions[best_cond]['recommendation']}**" output += f"\n### Recommendation:\n**{conditions[best_disease]['recommendation']}**"
# Save full text for PDF return output, output
# -----------------------------# Chatbot Function# -----------------------------# ----------------------------------------# Chatbot Logic# ----------------------------------------def doctor_chat(history, message): if not message: return history, ""
reply = { "role": "assistant", "content": f"Based on your earlier diagnosis, here is my advice:\n\n{message}\n\nAlways consult a real doctor if symptoms worsen." "content": "Based on your condition: " + "\n\n" + message + "\n\nβοΈ Always consult a real doctor for serious symptoms." }
history.append({"role": "user", "content": message}) history.append(reply) return history, ""
# -----------------------------# PDF Report# -----------------------------# ----------------------------------------# PDF Generator# ----------------------------------------def generate_pdf(text): pdf = FPDF() pdf.add_page()2 unmodified lines for line in text.split("\n"): pdf.multi_cell(0, 8, line)
path = "/tmp/report.pdf" path = "/tmp/diagnosis_report.pdf" pdf.output(path) return path
# -----------------------------# Gradio Interface# -----------------------------with gr.Blocks() as app:
# ----------------------------------------# Gradio App UI# ----------------------------------------with gr.Blocks() as app: gr.Markdown("# π₯ Advanced Medical Diagnosis System")
# Sliders sliders = [] for s in symptom_list: sliders.append(gr.Slider(0, 3, step=1, label=s)) symptom_box = gr.CheckboxGroup( symptom_list, label="Select Symptoms", info="Choose all symptoms that apply." )
diagnose_btn = gr.Button("π Diagnose") diagnosis_md = gr.Markdown()
pdf_btn = gr.Button("π Download PDF Report") pdf_file = gr.File(label="Your PDF Diagnosis") pdf_file = gr.File(label="Your PDF")
# Chatbot chatbot = gr.Chatbot(type="messages", label="AI Medical Assistant") chat_input = gr.Textbox(placeholder="Ask a question...") gr.Markdown("## π€ AI Medical Assistant Chat") chatbot = gr.Chatbot(type="messages") chat_input = gr.Textbox(placeholder="Ask the AI doctor...") send_btn = gr.Button("Send")
# Logic diagnose_btn.click( diagnose, inputs=sliders, outputs=[diagnosis_md, gr.State()] )
pdf_btn.click( generate_pdf, inputs=diagnosis_md, outputs=pdf_file ) diagnose_btn.click(diagnose, inputs=symptom_box, outputs=[diagnosis_md, gr.State()]) pdf_btn.click(generate_pdf, inputs=diagnosis_md, outputs=pdf_file)
send_btn.click( doctor_chat,app.py+64-65