Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-38.pyc +0 -0
- app.py +156 -0
- requirements.txt +5 -0
- symptom.webp +0 -0
- symptomes.json +97 -0
- symptoms.webp +0 -0
- utils.py +50 -0
__pycache__/utils.cpython-310.pyc
ADDED
Binary file (2.87 kB). View file
|
|
__pycache__/utils.cpython-38.pyc
ADDED
Binary file (2.91 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from utils import get_data, diseases, metrics_table
|
3 |
+
from transformers import pipeline
|
4 |
+
import pandas as pd
|
5 |
+
import io
|
6 |
+
import json
|
7 |
+
from openai import OpenAI
|
8 |
+
import tensorflow as tf
|
9 |
+
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
|
10 |
+
|
11 |
+
|
12 |
+
# Load model
|
13 |
+
model_name = "Zabihin/Symptom_to_Diagnosis"
|
14 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
|
18 |
+
def get_data(file:str):
|
19 |
+
"""
|
20 |
+
Reads data from a JSON file and returns the loaded data.
|
21 |
+
|
22 |
+
Parameters:
|
23 |
+
file (str): The path to the JSON file to be read.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
dict: The data loaded from the specified JSON file.
|
27 |
+
"""
|
28 |
+
with open(file) as files:
|
29 |
+
data = json.load(files)
|
30 |
+
return data
|
31 |
+
|
32 |
+
|
33 |
+
def call_gpt3(api,prompt:str):
|
34 |
+
client = OpenAI(api_key=api)
|
35 |
+
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
model="gpt-3.5-turbo",
|
38 |
+
messages=[
|
39 |
+
{"role": "system", "content": "You are a doctor expert in drugs and diseases."},
|
40 |
+
{"role": "user", "content": prompt}
|
41 |
+
])
|
42 |
+
|
43 |
+
generated_text = response.choices[0].message.content
|
44 |
+
|
45 |
+
return generated_text
|
46 |
+
|
47 |
+
|
48 |
+
# Define functions
|
49 |
+
def get_predictions(text):
|
50 |
+
inputs = tokenizer(text, return_tensors="tf", truncation=True, max_length=512)
|
51 |
+
outputs = model(inputs)
|
52 |
+
probabilities = tf.nn.softmax(outputs.logits, axis=-1)
|
53 |
+
predictions = []
|
54 |
+
for idx, prob in enumerate(probabilities[0]):
|
55 |
+
label = model.config.id2label[idx]
|
56 |
+
predictions.append((label, prob.numpy()))
|
57 |
+
return predictions
|
58 |
+
|
59 |
+
def analyze_probabilities(predictions):
|
60 |
+
probabilities = [prob for _, prob in predictions]
|
61 |
+
max_prob = max(probabilities)
|
62 |
+
high_prob_label = next(label for label, prob in predictions if prob == max_prob)
|
63 |
+
return high_prob_label,max_prob
|
64 |
+
|
65 |
+
|
66 |
+
def main():
|
67 |
+
# import symptoms data
|
68 |
+
symptoms = get_data('symptomes.json')
|
69 |
+
|
70 |
+
# Create sidebar and pages content
|
71 |
+
tabs = ["Home", "About Us", "Model Details & Evaluations"]
|
72 |
+
st.sidebar.header("Welcome to the Symptom checker app ! This app provide diagnosis based on your symptoms. Feel free to try it out !")
|
73 |
+
st.sidebar.divider()
|
74 |
+
active_tab = st.sidebar.radio("Select Tab", tabs)
|
75 |
+
if active_tab == "Home":
|
76 |
+
st.header("Symptom Checker and Diagnosis App", divider = 'violet')
|
77 |
+
image_path = "symptom.webp"
|
78 |
+
col1, col2 = st.columns(2)
|
79 |
+
with col1:
|
80 |
+
st.image(image_path, use_column_width='auto')
|
81 |
+
with col2:
|
82 |
+
st.subheader("👩⚕️ Enter your symptoms:")
|
83 |
+
st.subheader("get your diagnosis and useful advices")
|
84 |
+
|
85 |
+
input_text = ""
|
86 |
+
for category, symptoms in symptoms.items():
|
87 |
+
st.sidebar.write(f"### {category}")
|
88 |
+
category_symptoms = st.sidebar.multiselect(f"Select Symptoms in {category}", [symptom[0] for symptom in symptoms])
|
89 |
+
if category_symptoms:
|
90 |
+
input_text += f""
|
91 |
+
for selected_symptom in category_symptoms:
|
92 |
+
symptom_description = next((symptom[1] for symptom in symptoms if symptom[0] == selected_symptom), "")
|
93 |
+
input_text += f"{symptom_description}\n"
|
94 |
+
|
95 |
+
# Display the updated input_text
|
96 |
+
manual_input=st.text_area("Symptoms", value=input_text, height=200)
|
97 |
+
|
98 |
+
st.warning("If you want to know more, please enter your api key and click on submit: ")
|
99 |
+
with st.expander("Click here to enter your api"):
|
100 |
+
api=st.text_input("API KEY", value="",type='password')
|
101 |
+
|
102 |
+
# Button to submit and get the predicted label
|
103 |
+
if st.button("Submit"):
|
104 |
+
predictions = get_predictions(manual_input)
|
105 |
+
predicted_label,probability=analyze_probabilities(predictions)
|
106 |
+
|
107 |
+
# Condition to display only high probability deseases
|
108 |
+
if probability > 0.6:
|
109 |
+
st.success(f"Based on your symptoms, there's a {100 * probability:.2f}% probability that you might have {predicted_label}.")
|
110 |
+
|
111 |
+
if api:
|
112 |
+
|
113 |
+
# Try using the provided API key to call GPT-3
|
114 |
+
gpt3_prompt = f"Please provide a list of medications for {predicted_label}, along with a brief explanation of this disease."
|
115 |
+
#gpt3_response = call_gpt3(api, gpt3_prompt)
|
116 |
+
|
117 |
+
try :
|
118 |
+
gpt3_response = call_gpt3(api,gpt3_prompt)
|
119 |
+
# Display the GPT-3 response if successful
|
120 |
+
st.header(f'Information about the **{predicted_label}**')
|
121 |
+
st.info('Please note: This information is AI-generated and does not replace professional medical advice.')
|
122 |
+
st.write(gpt3_response)
|
123 |
+
|
124 |
+
except:
|
125 |
+
# Handle case where API call fails
|
126 |
+
st.warning("Unable to retrieve information using the provided API key. Please try another API key if available.")
|
127 |
+
else:
|
128 |
+
st.write('If you want to know more, please enter your api key')
|
129 |
+
else:
|
130 |
+
st.warning("The symptoms you've described do not strongly indicate any of the 22 diseases in our database with a high probability. It's recommended to consult a healthcare professional for a more accurate diagnosis.")
|
131 |
+
# Expander to show the list of diseases
|
132 |
+
with st.expander("Click here to view the list of diseases"):
|
133 |
+
for disease in diseases:
|
134 |
+
st.write(disease)
|
135 |
+
|
136 |
+
elif active_tab == "About Us":
|
137 |
+
st.title("GZ-Health")
|
138 |
+
|
139 |
+
st.markdown("**[Zahra ZABIHINPOUR](https://www.linkedin.com/in/zahra-zabihinpour/)**")
|
140 |
+
st.markdown("**[Kevin GOUPIL](https://www.linkedin.com/in/kevin-goupil/)**")
|
141 |
+
st.markdown(" We are a dynamic duo of data scientists collaborating to enhance our skills and stay at the forefront of the latest developments. With backgrounds in science and experience working with health data, we bring a unique blend of expertise to our data science projects. Our shared passion and commitment drive us to showcase and elevate our capabilities through innovative and impactful initiatives. Join us on this journey of continuous improvement and exploration in the world of data science. ")
|
142 |
+
st.markdown(" ")
|
143 |
+
|
144 |
+
elif active_tab == "Model Details & Evaluations":
|
145 |
+
st.subheader("Model Overview:")
|
146 |
+
st.write("This model is a fine-tuned adaptation of the bert-base-cased architecture, specifically designed for text classification tasks associated with diagnosing diseases based on symptoms. The primary goal is to scrutinize natural language symptom descriptions and accurately predict one of 22 potential diagnoses.")
|
147 |
+
st.subheader("Dataset Information:")
|
148 |
+
st.write("The model was trained on the Gretel/symptom_to_diagnosis dataset, which consists of 1,065 symptom descriptions in English, each labeled with one of the 22 possible diagnoses. This dataset focuses on detailed, fine-grained, single-domain diagnosis, making it suitable for tasks requiring nuanced symptom classification. For those interested in utilizing the model, the Symptom Checker and Diagnosis App, or the Inference API, are accessible at [https://huggingface.co/Zabihin/Symptom_to_Diagnosis](https://huggingface.co/Zabihin/Symptom_to_Diagnosis).")
|
149 |
+
st.subheader("Model Performance Metrics:")
|
150 |
+
metrics_data = pd.read_csv(io.StringIO(metrics_table), sep="|").dropna()
|
151 |
+
st.table(metrics_data)
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
if __name__ == "__main__":
|
156 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==0.94.1
|
2 |
+
transformers==5.0.1
|
3 |
+
pandas==1.3.3
|
4 |
+
openai==0.27.0
|
5 |
+
tensorflow==2.7.0
|
symptom.webp
ADDED
symptomes.json
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"General Symptoms (Body)": [
|
3 |
+
["Fever", "I have an elevated body temperature."],
|
4 |
+
["Nausea", "I feel the urge to vomit."],
|
5 |
+
["Vomiting", "I am expelling stomach contents through my mouth."],
|
6 |
+
["Dizziness", "I experience a sensation of lightheadedness or unsteadiness."],
|
7 |
+
["Weight loss", "I have lost a significant amount of body weight."],
|
8 |
+
["Fatigue", "I feel extreme tiredness and lack of energy."],
|
9 |
+
["Excessive sweating", "I am sweating profusely."],
|
10 |
+
["Anemia", "I have a deficiency of red blood cells, leading to fatigue."],
|
11 |
+
["Chills", "I experience shivering or feeling cold."],
|
12 |
+
["Bulging veins", "My veins appear swollen or protruded."],
|
13 |
+
["Body aches", "I feel discomfort or pain throughout my body."]
|
14 |
+
],
|
15 |
+
"Head/Neck": [
|
16 |
+
["Head pain", "I have pain in my head."],
|
17 |
+
["Neck pain", "I experience pain in my neck."],
|
18 |
+
["Headache on one side", "I feel a headache concentrated on one side of my head."],
|
19 |
+
["Confusion", "I am disoriented or have difficulty understanding things."],
|
20 |
+
["Blurred vision", "My vision is unclear or fuzzy."],
|
21 |
+
["Distorted vision", "I see images in an altered or abnormal way."],
|
22 |
+
["Sensitivity to light and sound", "I am more sensitive to light and sound than usual."],
|
23 |
+
["Stiff neck", "My neck feels stiff and limited in movement."]
|
24 |
+
],
|
25 |
+
"Eyes": [
|
26 |
+
["Itchy eyes", "My eyes are experiencing itching."],
|
27 |
+
["Watery eyes", "My eyes are producing excessive tears."],
|
28 |
+
["Red eyes", "The whites of my eyes appear red."],
|
29 |
+
["Eye pain", "I am experiencing pain or discomfort in my eyes."],
|
30 |
+
["Yellow eyes", "The whites of my eyes have a yellowish tint."],
|
31 |
+
["Blurred vision", "My vision is unclear or fuzzy."],
|
32 |
+
["Sensitivity to light", "I am more sensitive to light than usual."]
|
33 |
+
],
|
34 |
+
"Digestive System": [
|
35 |
+
["Heartburn", "I feel a burning sensation in my chest or throat."],
|
36 |
+
["Upper abdominal or chest pain", "I experience pain in the upper abdomen or chest."],
|
37 |
+
["Difficulty swallowing", "I have trouble moving food from my mouth to my stomach."],
|
38 |
+
["Sensation of a lump in the throat", "I feel like there is something stuck in my throat."],
|
39 |
+
["Excessive thirst", "I am very thirsty."],
|
40 |
+
["Abdominal pain", "I have pain or discomfort in my abdomen."],
|
41 |
+
["Severe diarrhea", "I am experiencing frequent, watery bowel movements."],
|
42 |
+
["Vomiting", "I am expelling stomach contents through my mouth."],
|
43 |
+
["Nausea", "I feel the urge to vomit."],
|
44 |
+
["Bloating", "I have a feeling of fullness and tightness in the abdomen."],
|
45 |
+
["Belching", "I am expelling gas from the stomach through the mouth."],
|
46 |
+
["Decreased appetite", "I have a reduced desire to eat."],
|
47 |
+
["Indigestion", "I am experiencing discomfort or pain in the upper abdomen."]
|
48 |
+
],
|
49 |
+
"Skin": [
|
50 |
+
["Changes in skin color", "There are alterations in the color of my skin."],
|
51 |
+
["Red/itchy sores", "I have red and itchy sores on my skin."],
|
52 |
+
["Yellow or honey-colored scabs", "Scabs on my skin have a yellow or honey-colored appearance."],
|
53 |
+
["Warm, red skin", "My skin feels warm and appears red."],
|
54 |
+
["Scaly skin", "My skin is dry and covered with scales."],
|
55 |
+
["Rash", "I have an outbreak of red, raised, and often itchy skin."],
|
56 |
+
["Scabs", "I have dried blood or pus over a healing wound."],
|
57 |
+
["Fluid-filled blisters", "Blisters on my skin contain clear fluid."],
|
58 |
+
["Itching", "I experience a sensation that prompts me to scratch my skin."],
|
59 |
+
["Dry skin", "My skin lacks moisture and feels rough or flaky."],
|
60 |
+
["Swelling", "There is an abnormal enlargement of body parts or areas."]
|
61 |
+
],
|
62 |
+
"Urinary Tract": [
|
63 |
+
["Pain during urination", "I feel pain or discomfort while urinating."],
|
64 |
+
["Burning sensation during urination", "I experience a burning or stinging feeling during urination."],
|
65 |
+
["Frequent urination", "I need to urinate more often than usual."],
|
66 |
+
["Cloudy urine", "My urine appears cloudy or murky."],
|
67 |
+
["Blood in urine", "There is blood visible in my urine."],
|
68 |
+
["Difficulty controlling bladder", "I have trouble controlling my bladder, leading to leakage."],
|
69 |
+
["Difficulty controlling bowels", "I have trouble controlling my bowels, leading to leakage."],
|
70 |
+
["Dark urine", "My urine has a darker color than usual."],
|
71 |
+
["Pale or clay-colored stools", "My stools have a pale or clay-like color."],
|
72 |
+
["Urinary urgency", "I feel a strong and sudden need to urinate."]
|
73 |
+
],
|
74 |
+
"Muscle/Skeletal System": [
|
75 |
+
["Joint pain", "I have pain or discomfort in the joints."],
|
76 |
+
["Restricted movement", "There is a limitation in the normal range of motion."],
|
77 |
+
["Weakness", "I feel a lack of strength or energy."],
|
78 |
+
["Muscle wasting", "My muscles are shrinking or losing mass."],
|
79 |
+
["Nighttime leg cramps", "I experience cramping in my legs during the night."],
|
80 |
+
["Swelling in joints", "There is an abnormal enlargement of joints."],
|
81 |
+
["Stiffness", "I feel difficulty in moving certain body parts."],
|
82 |
+
["Muscle spasms", "I experience involuntary contractions of muscles."]
|
83 |
+
],
|
84 |
+
"Respiratory System (Lungs)": [
|
85 |
+
["Sneezing", "I forcefully expel air through my nose."],
|
86 |
+
["Nasal congestion", "My nasal passages are blocked or congested."],
|
87 |
+
["Coughing", "I am expelling air from the lungs with a sudden sharp sound."],
|
88 |
+
["Runny nose", "My nose is producing excess mucus."],
|
89 |
+
["Sore throat", "I have pain or irritation in the throat."],
|
90 |
+
["Wheezing", "I produce a whistling sound while breathing."],
|
91 |
+
["Coughing attacks", "I experience sudden and severe bouts of coughing."],
|
92 |
+
["Shortness of breath", "I find it difficult to breathe and feel breathless."],
|
93 |
+
["Chest tightness", "I feel a squeezing or pressure in my chest."],
|
94 |
+
["Rapid breathing", "I am breathing at a faster rate than normal."]
|
95 |
+
]
|
96 |
+
}
|
97 |
+
|
symptoms.webp
ADDED
utils.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
def get_data(file:str):
|
4 |
+
"""
|
5 |
+
Reads data from a JSON file and returns the loaded data.
|
6 |
+
|
7 |
+
Parameters:
|
8 |
+
file (str): The path to the JSON file to be read.
|
9 |
+
|
10 |
+
Returns:
|
11 |
+
dict: The data loaded from the specified JSON file.
|
12 |
+
"""
|
13 |
+
with open(file) as files:
|
14 |
+
data = json.load(files)
|
15 |
+
return data
|
16 |
+
|
17 |
+
diseases = [
|
18 |
+
"Allergy", "Arthritis", "Bronchial Asthma", "Cervical Spondylosis",
|
19 |
+
"Chicken Pox", "Common Cold", "Dengue", "Diabetes", "Drug Reaction",
|
20 |
+
"Fungal Infection", "Gastroesophageal Reflux Disease", "Hypertension",
|
21 |
+
"Impetigo", "Jaundice", "Malaria", "Migraine", "Peptic Ulcer Disease",
|
22 |
+
"Pneumonia", "Psoriasis", "Typhoid", "Urinary Tract Infection",
|
23 |
+
"Varicose Veins"
|
24 |
+
]
|
25 |
+
|
26 |
+
metrics_table = """
|
27 |
+
Disease | Precision | Recall | F1-Score
|
28 |
+
Allergy | 0.91 | 1.00 | 0.95
|
29 |
+
Arthritis | 1.00 | 1.00 | 1.00
|
30 |
+
Bronchial Asthma | 1.00 | 1.00 | 1.00
|
31 |
+
Cervical Spondylosis | 0.91 | 1.00 | 0.95
|
32 |
+
Chicken Pox | 1.00 | 1.00 | 1.00
|
33 |
+
Common Cold | 1.00 | 1.00 | 1.00
|
34 |
+
Dengue | 1.00 | 0.90 | 0.95
|
35 |
+
Diabetes | 1.00 | 0.80 | 0.89
|
36 |
+
Drug Reaction | 0.80 | 1.00 | 0.89
|
37 |
+
Fungal Infection | 1.00 | 1.00 | 1.00
|
38 |
+
Gastroesophageal Reflux Disease | 1.00 | 0.90 | 0.95
|
39 |
+
Hypertension | 0.91 | 1.00 | 0.95
|
40 |
+
Impetigo | 1.00 | 1.00 | 1.00
|
41 |
+
Jaundice | 1.00 | 1.00 | 1.00
|
42 |
+
Malaria | 1.00 | 1.00 | 1.00
|
43 |
+
Migraine | 1.00 | 0.90 | 0.95
|
44 |
+
Peptic Ulcer Disease | 1.00 | 1.00 | 1.00
|
45 |
+
Pneumonia | 1.00 | 1.00 | 1.00
|
46 |
+
Psoriasis | 1.00 | 0.90 | 0.95
|
47 |
+
Typhoid | 1.00 | 1.00 | 1.00
|
48 |
+
Urinary Tract Infection | 0.90 | 1.00 | 0.95
|
49 |
+
Varicose Veins | 1.00 | 1.00 | 1.00
|
50 |
+
"""
|