Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,124 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
import pandas as pd
|
|
|
|
|
5 |
|
6 |
-
# Load the symptom
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
if not match.empty:
|
26 |
-
symptom = match.iloc[0]
|
27 |
-
response = f"It seems like you're experiencing {symptom['Primary Symptom']}. "
|
28 |
-
if pd.notna(symptom["Follow-up Question"]):
|
29 |
-
response += f"{symptom['Follow-up Question']} "
|
30 |
-
response += f"\nPossible conditions: {symptom['Possible Diseases']} \n"
|
31 |
-
response += f"Recommended measures: {symptom['Recommended Measures']}"
|
32 |
-
return symptom['Primary Symptom'], response # Return symptom name and response
|
33 |
-
return None, None
|
34 |
-
|
35 |
-
# Main chatbot response function
|
36 |
-
def chatbot_response(user_input, history):
|
37 |
-
global last_detected_symptom # Maintain previous symptom context
|
38 |
|
39 |
-
if
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Gradio UI
|
76 |
-
|
77 |
-
gr.Markdown("### Symptom Chatbot 🏥")
|
78 |
-
chatbot = gr.Chatbot()
|
79 |
-
user_input = gr.Textbox(placeholder="Type your symptom and get advice...", interactive=True)
|
80 |
-
submit = gr.Button("Send")
|
81 |
-
clear = gr.Button("Clear Chat")
|
82 |
-
|
83 |
-
submit.click(chatbot_response, [user_input, chatbot], [chatbot, user_input])
|
84 |
-
clear.click(clear_chat, [], [chatbot, user_input])
|
85 |
|
86 |
-
|
87 |
-
if __name__ == "__main__":
|
88 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import pandas as pd
|
3 |
+
from collections import defaultdict
|
4 |
+
from transformers import pipeline
|
5 |
|
6 |
+
# Load the Phi-2 model for symptom normalization
|
7 |
+
symptom_normalizer = pipeline("text-classification", model="microsoft/phi-2")
|
8 |
|
9 |
+
# Predefined symptoms
|
10 |
+
symptom_data = {
|
11 |
+
"Shortness of breath": {
|
12 |
+
"questions": [
|
13 |
+
"Do you also have chest pain?",
|
14 |
+
"Do you feel fatigued often?",
|
15 |
+
"Have you noticed swelling in your legs?"
|
16 |
+
],
|
17 |
+
"diseases": ["Atelectasis", "Emphysema", "Edema"],
|
18 |
+
"weights_yes": [30, 30, 40],
|
19 |
+
"weights_no": [10, 20, 30]
|
20 |
+
},
|
21 |
+
"Persistent cough": {
|
22 |
+
"questions": [
|
23 |
+
"Is your cough dry or with mucus?",
|
24 |
+
"Do you experience fever?",
|
25 |
+
"Do you have difficulty breathing?"
|
26 |
+
],
|
27 |
+
"diseases": ["Pneumonia", "Fibrosis", "Infiltration"],
|
28 |
+
"weights_yes": [35, 30, 35],
|
29 |
+
"weights_no": [10, 15, 20]
|
30 |
+
},
|
31 |
+
"Sharp chest pain": {
|
32 |
+
"questions": [
|
33 |
+
"Does it worsen with deep breaths?",
|
34 |
+
"Do you feel lightheaded?",
|
35 |
+
"Have you had recent trauma or surgery?"
|
36 |
+
],
|
37 |
+
"diseases": ["Pneumothorax", "Effusion", "Cardiomegaly"],
|
38 |
+
"weights_yes": [40, 30, 30],
|
39 |
+
"weights_no": [15, 20, 25]
|
40 |
+
},
|
41 |
+
"Fatigue & swelling": {
|
42 |
+
"questions": [
|
43 |
+
"Do you feel breathless when lying down?",
|
44 |
+
"Have you gained weight suddenly?",
|
45 |
+
"Do you experience irregular heartbeat?"
|
46 |
+
],
|
47 |
+
"diseases": ["Edema", "Cardiomegaly"],
|
48 |
+
"weights_yes": [50, 30, 20],
|
49 |
+
"weights_no": [20, 15, 15]
|
50 |
+
},
|
51 |
+
"Chronic wheezing": {
|
52 |
+
"questions": [
|
53 |
+
"Do you have a history of smoking?",
|
54 |
+
"Do you feel tightness in your chest?",
|
55 |
+
"Do you have frequent lung infections?"
|
56 |
+
],
|
57 |
+
"diseases": ["Emphysema", "Fibrosis"],
|
58 |
+
"weights_yes": [40, 30, 30],
|
59 |
+
"weights_no": [15, 25, 20]
|
60 |
+
}
|
61 |
+
}
|
62 |
|
63 |
+
# Global variables to track user state
|
64 |
+
user_state = {}
|
65 |
|
66 |
+
def normalize_symptom(user_input):
|
67 |
+
labels = list(symptom_data.keys())
|
68 |
+
predictions = symptom_normalizer(user_input, labels=labels)
|
69 |
+
return predictions[0]['label']
|
70 |
|
71 |
+
def chatbot(user_input):
|
72 |
+
if "state" not in user_state:
|
73 |
+
user_state["state"] = "greet"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
if user_state["state"] == "greet":
|
76 |
+
user_state["state"] = "ask_symptom"
|
77 |
+
return "Hello! I'm a medical AI assistant. Please describe your primary symptom."
|
78 |
|
79 |
+
elif user_state["state"] == "ask_symptom":
|
80 |
+
normalized_symptom = normalize_symptom(user_input)
|
81 |
+
if normalized_symptom not in symptom_data:
|
82 |
+
return "I don't recognize that symptom. Please enter one of these: " + ", ".join(symptom_data.keys())
|
83 |
+
user_state["symptom"] = normalized_symptom
|
84 |
+
user_state["state"] = "ask_duration"
|
85 |
+
return "How long have you been experiencing this symptom? (Less than a week / More than a week)"
|
86 |
+
|
87 |
+
elif user_state["state"] == "ask_duration":
|
88 |
+
if user_input.lower() == "less than a week":
|
89 |
+
user_state.clear()
|
90 |
+
return "It might be a temporary issue. Please monitor your symptoms and consult a doctor if they persist."
|
91 |
+
elif user_input.lower() == "more than a week":
|
92 |
+
user_state["state"] = "follow_up"
|
93 |
+
user_state["current_question"] = 0
|
94 |
+
user_state["disease_scores"] = defaultdict(int)
|
95 |
+
return symptom_data[user_state["symptom"]]["questions"][0]
|
96 |
+
else:
|
97 |
+
return "Please respond with 'Less than a week' or 'More than a week'."
|
98 |
+
|
99 |
+
elif user_state["state"] == "follow_up":
|
100 |
+
symptom = user_state["symptom"]
|
101 |
+
question_index = user_state["current_question"]
|
102 |
+
|
103 |
+
# Update probabilities
|
104 |
+
if user_input.lower() == "yes":
|
105 |
+
for i, disease in enumerate(symptom_data[symptom]["diseases"]):
|
106 |
+
user_state["disease_scores"][disease] += symptom_data[symptom]["weights_yes"][i]
|
107 |
+
else:
|
108 |
+
for i, disease in enumerate(symptom_data[symptom]["diseases"]):
|
109 |
+
user_state["disease_scores"][disease] += symptom_data[symptom]["weights_no"][i]
|
110 |
+
|
111 |
+
# Move to the next question or finish
|
112 |
+
user_state["current_question"] += 1
|
113 |
+
if user_state["current_question"] < len(symptom_data[symptom]["questions"]):
|
114 |
+
return symptom_data[symptom]["questions"][user_state["current_question"]]
|
115 |
+
|
116 |
+
# Final diagnosis
|
117 |
+
probable_disease = max(user_state["disease_scores"], key=user_state["disease_scores"].get)
|
118 |
+
user_state.clear()
|
119 |
+
return f"Based on your symptoms, the most likely condition is: {probable_disease}. Please consult a doctor for confirmation."
|
120 |
|
121 |
# Gradio UI
|
122 |
+
demo = gr.Interface(fn=chatbot, inputs=gr.Textbox(placeholder="Enter your response..."), outputs="text", title="Symptom Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
demo.launch()
|
|
|
|