Spaces:
Paused
Paused
Update tabs/symptom_checker.py
Browse files- tabs/symptom_checker.py +95 -0
tabs/symptom_checker.py
CHANGED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import torch
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
|
| 6 |
+
# Load symptom embeddings (from models/)
|
| 7 |
+
with open("models/symptom_embeddings.pkl", "rb") as f:
|
| 8 |
+
symptom_data = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
# Function to identify disease
|
| 11 |
+
def identify_disease(user_symptom):
|
| 12 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 13 |
+
input_embedding = embedding_model.encode(user_symptom, convert_to_tensor=True)
|
| 14 |
+
similarities = util.pytorch_cos_sim(input_embedding, symptom_data['embeddings'])[0]
|
| 15 |
+
idx = similarities.argmax().item()
|
| 16 |
+
return symptom_data['diseases'][idx], symptom_data['treatments'][idx]
|
| 17 |
+
|
| 18 |
+
# Clear button logic
|
| 19 |
+
def clear_symptoms():
|
| 20 |
+
return "", "", ""
|
| 21 |
+
|
| 22 |
+
# Main UI layout for Symptom Checker tab
|
| 23 |
+
def symptom_checker_tab():
|
| 24 |
+
gr.Markdown("## 🧠 TherapyBot++", elem_classes="centered-text")
|
| 25 |
+
gr.Markdown("Describe your symptoms to get a possible diagnosis and treatment.", elem_classes="centered-text")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
with gr.Column(scale=1):
|
| 29 |
+
symptom_input = gr.Textbox(placeholder="e.g., I have a fever and rash", label="Enter your symptoms", lines=2)
|
| 30 |
+
symptom_btn = gr.Button("Check", elem_id="symptom-btn")
|
| 31 |
+
symptom_clear = gr.Button("Clear")
|
| 32 |
+
with gr.Column(scale=1):
|
| 33 |
+
predicted_disease = gr.Textbox(label="Predicted Disease", interactive=False, lines=2.2)
|
| 34 |
+
suggested_treatment = gr.Textbox(label="Suggested Treatment", interactive=False, lines=2.2)
|
| 35 |
+
|
| 36 |
+
symptom_btn.click(identify_disease, symptom_input, outputs=[predicted_disease, suggested_treatment])
|
| 37 |
+
symptom_clear.click(clear_symptoms, outputs=[symptom_input, predicted_disease, suggested_treatment])
|
| 38 |
+
|
| 39 |
+
gr.Markdown("""
|
| 40 |
+
<!-- Scrolling Example Symptom Statements -->
|
| 41 |
+
<div style="height: auto; min-height: 100px; width: 100%; max-width: 65%; margin: 15px auto; overflow: hidden; position: relative; background: linear-gradient(to right, #1c2f3a 0%, #000000 50%, #1c2f3a 100%); border-radius: 12px; box-shadow: 0 2px 12px rgba(0,0,0,0.4); text-align: center; padding: 16px 24px; box-sizing: border-box;">
|
| 42 |
+
<div style="font-size: 15px; color: #e0e0e0; font-weight: 600; margin-bottom: 14px; position: relative; z-index: 2;">
|
| 43 |
+
📌 <em>You may refer to these example symptom statements:</em>
|
| 44 |
+
</div>
|
| 45 |
+
<div class="scroll-wrapper">
|
| 46 |
+
<div class="scroll-content">
|
| 47 |
+
<div class="scroll-inner">
|
| 48 |
+
<div>I have chest pain.</div>
|
| 49 |
+
<div>I've been vomiting.</div>
|
| 50 |
+
<div>I'm feeling a lot of tiredness.</div>
|
| 51 |
+
<div>I'm experiencing nausea.</div>
|
| 52 |
+
<div>I have diarrhea.</div>
|
| 53 |
+
<div>I've developed a rash.</div>
|
| 54 |
+
<div>I have a headache.</div>
|
| 55 |
+
<div>I'm experiencing muscle pain.</div>
|
| 56 |
+
<div>I have a fever.</div>
|
| 57 |
+
<div>I'm feeling short of breath.</div>
|
| 58 |
+
<div>I have joint pain.</div>
|
| 59 |
+
<div>I have a sore throat.</div>
|
| 60 |
+
<div>I have a cough.</div>
|
| 61 |
+
</div>
|
| 62 |
+
</div>
|
| 63 |
+
</div>
|
| 64 |
+
</div>
|
| 65 |
+
<style>
|
| 66 |
+
.scroll-wrapper {
|
| 67 |
+
height: 50px;
|
| 68 |
+
overflow: hidden;
|
| 69 |
+
position: relative;
|
| 70 |
+
}
|
| 71 |
+
.scroll-content {
|
| 72 |
+
height: auto;
|
| 73 |
+
width: 100%;
|
| 74 |
+
position: absolute;
|
| 75 |
+
animation: scroll-vertical 40s linear infinite;
|
| 76 |
+
will-change: transform;
|
| 77 |
+
}
|
| 78 |
+
.scroll-inner {
|
| 79 |
+
display: flex;
|
| 80 |
+
flex-direction: column;
|
| 81 |
+
gap: 20px;
|
| 82 |
+
color: #bbbbbb;
|
| 83 |
+
font-size: 13.8px;
|
| 84 |
+
line-height: 1.5;
|
| 85 |
+
}
|
| 86 |
+
@keyframes scroll-vertical {
|
| 87 |
+
0% {
|
| 88 |
+
transform: translateY(0);
|
| 89 |
+
}
|
| 90 |
+
100% {
|
| 91 |
+
transform: translateY(-60%);
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
</style>
|
| 95 |
+
""", elem_classes="centered-text")
|