Update src/streamlit_app.py
Browse files- src/streamlit_app.py +80 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,82 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import streamlit.components.v1 as components
|
5 |
|
6 |
+
|
7 |
+
# Set Streamlit page config for a clinical look
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Symptom2Disease Clinical AI",
|
10 |
+
page_icon="🩺",
|
11 |
+
layout="centered"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Clinical-themed header
|
15 |
+
st.title("🩺 Symptom2Disease Clinical AI")
|
16 |
+
st.markdown("""
|
17 |
+
Welcome to the **Symptom2Disease** clinical assistant.
|
18 |
+
Enter your symptoms below and our AI will suggest possible diseases.
|
19 |
+
""")
|
20 |
+
|
21 |
+
# Load model and tokenizer
|
22 |
+
@st.cache_resource
|
23 |
+
def load_model():
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained("alibidaran/Symptom2disease")
|
25 |
+
model = AutoModelForSequenceClassification.from_pretrained("alibidaran/Symptom2disease")
|
26 |
+
return tokenizer, model
|
27 |
+
|
28 |
+
tokenizer, model = load_model()
|
29 |
+
|
30 |
+
label_2id = {
|
31 |
+
'Psoriasis': 0, 'Varicose Veins': 1, 'Typhoid': 2, 'Chicken pox': 3, 'Impetigo': 4,
|
32 |
+
'Dengue': 5, 'Fungal infection': 6, 'Common Cold': 7, 'Pneumonia': 8, 'Dimorphic Hemorrhoids': 9,
|
33 |
+
'Arthritis': 10, 'Acne': 11, 'Bronchial Asthma': 12, 'Hypertension': 13, 'Migraine': 14,
|
34 |
+
'Cervical spondylosis': 15, 'Jaundice': 16, 'Malaria': 17, 'urinary tract infection': 18,
|
35 |
+
'allergy': 19, 'gastroesophageal reflux disease': 20, 'drug reaction': 21, 'peptic ulcer disease': 22,
|
36 |
+
'diabetes': 23
|
37 |
+
}
|
38 |
+
id2label = {i: v for v, i in label_2id.items()}
|
39 |
+
|
40 |
+
def detect_symptom(symptoms):
|
41 |
+
inputs_id = tokenizer(symptoms, padding=True, truncation=True, return_tensors="pt")
|
42 |
+
output = model(inputs_id['input_ids'])
|
43 |
+
preds = torch.nn.functional.softmax(output.logits, -1).topk(5)
|
44 |
+
results = {id2label[preds.indices[0][i].item()]: float(preds.values[0][i].item()) for i in range(5)}
|
45 |
+
return results
|
46 |
+
|
47 |
+
# Example symptoms for quick testing
|
48 |
+
examples = [
|
49 |
+
"I can't stop sneezing and I feel really tired and crummy. My throat is really sore",
|
50 |
+
"I have been experiencing a severe headache that is accompanied by pain behind my eyes.",
|
51 |
+
"There are small red spots all over my body that I can't explain. It's worrying me.",
|
52 |
+
"I've been having a really hard time going to the bathroom lately. It's really painful"
|
53 |
+
]
|
54 |
+
|
55 |
+
with st.expander("📝 Example symptom descriptions"):
|
56 |
+
for ex in examples:
|
57 |
+
st.code(ex)
|
58 |
+
|
59 |
+
# User input
|
60 |
+
symptoms = st.text_area(
|
61 |
+
"Describe your symptoms:",
|
62 |
+
placeholder="e.g. I have a persistent cough and mild fever for the last 3 days.",
|
63 |
+
height=100
|
64 |
+
)
|
65 |
+
|
66 |
+
if st.button("Analyze Symptoms", type="primary"):
|
67 |
+
if symptoms.strip():
|
68 |
+
with st.spinner("Analyzing symptoms..."):
|
69 |
+
results = detect_symptom(symptoms)
|
70 |
+
st.subheader("Top 5 Predicted Diseases")
|
71 |
+
for disease, prob in results.items():
|
72 |
+
st.markdown(f"**{disease}**")
|
73 |
+
st.progress(prob)
|
74 |
+
st.caption(f"Probability: {prob:.2%}")
|
75 |
+
else:
|
76 |
+
st.warning("Please enter your symptoms above.")
|
77 |
+
|
78 |
+
st.markdown("---")
|
79 |
+
st.info("**Disclaimer:** This tool is for informational purposes only and does not provide medical advice. Please consult a healthcare professional for diagnosis and treatment.")
|
80 |
+
components.iframe(
|
81 |
+
src="http://127.0.0.1:8080",
|
82 |
+
height=200)
|