Chinez-dev commited on
Commit
9a1a960
·
verified ·
1 Parent(s): d04e3ac

Update app.py

Browse files

updated version with more tools

Files changed (1) hide show
  1. app.py +134 -25
app.py CHANGED
@@ -18,39 +18,148 @@ from Gradio_UI import GradioUI
18
  # """
19
  # return "What magic will you build ?"
20
 
 
 
 
 
21
  @tool
22
- def med_tool(role: str, symptoms: str) -> str:
23
- # It's important to specify the return type
24
- """A tool for symptom collection (nurse) and diagnosis (doctor).
25
-
26
  Args:
27
- role: The role of the healthcare provider ('nurse' or 'doctor')
28
- symptoms: The symptoms described by the patient
29
-
30
  Returns:
31
- A response based on the role provided.
32
  """
33
-
34
- if role.lower() == "nurse":
35
- follow_up_questions = [
 
 
 
 
36
  "How long have you had these symptoms?",
37
  "Do you have any allergies?",
38
- "Are you on any medication?",
 
39
  "Have you had any recent illnesses?",
40
- "Have you noticed any other unusual changes?"
 
 
41
  ]
42
- return (f"Nurse: I have recorded the symptoms: {symptoms}.\n"
43
- f"Here are some follow-up questions:\n- " + "\n- ".join(follow_up_questions))
44
 
45
- elif role.lower() == "doctor":
46
- # Delegating diagnosis to an external tool (e.g., AI search, RAG)
47
- return (f"Doctor: Based on the symptoms: {symptoms}, I will now analyze the data.\n"
48
- f"Fetching diagnosis, medication, and treatment recommendations from the medical database...")
 
 
49
 
50
- else:
51
- return "Invalid role! Please specify either 'nurse' or 'doctor'."
 
 
52
 
53
- final_answer = FinalAnswerTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  search_tool = DuckDuckGoSearchTool()
55
 
56
  @tool
@@ -90,13 +199,13 @@ with open("prompts.yaml", 'r') as stream:
90
 
91
  agent = CodeAgent(
92
  model=model,
93
- tools=[final_answer, med_tool], ## add your tools here (don't remove final answer)
94
  max_steps=6,
95
  verbosity_level=1,
96
  grammar=None,
97
  planning_interval=None,
98
- name=None,
99
- description=None,
100
  prompt_templates=prompt_templates
101
  )
102
 
 
18
  # """
19
  # return "What magic will you build ?"
20
 
21
+
22
+ def __init__(self):
23
+ self.patient_data = {}
24
+
25
  @tool
26
+ def collect_symptoms(self, patient_id: str, symptoms: str) -> str:
27
+ """
28
+ Nurse tool to collect symptoms and ask follow-up questions.
29
+
30
  Args:
31
+ patient_id: Unique identifier for the patient.
32
+ symptoms: Initial symptoms provided by the patient.
33
+
34
  Returns:
35
+ Follow-up questions based on the symptoms.
36
  """
37
+ try:
38
+ if not patient_id or not symptoms:
39
+ raise ValueError("Patient ID and symptoms must be provided.")
40
+
41
+ self.patient_data[patient_id] = {"symptoms": symptoms, "additional_info": {}}
42
+
43
+ questions = [
44
  "How long have you had these symptoms?",
45
  "Do you have any allergies?",
46
+ "Are you taking any medications?",
47
+ "Have you experienced these symptoms before?",
48
  "Have you had any recent illnesses?",
49
+ "Have you noticed any other unusual changes?",
50
+ "What is your medical history related to these symptoms?"
51
+
52
  ]
 
 
53
 
54
+ return f"Nurse: I have noted the symptoms ({symptoms}). Here are follow-up questions:\n" + "\n".join(questions)
55
+
56
+ except ValueError as e:
57
+ return f"Error: {str(e)}"
58
+ except Exception as e:
59
+ return f"Unexpected Error: {str(e)}"
60
 
61
+ @tool
62
+ def diagnose_patient(self, patient_id: str) -> str:
63
+ """
64
+ Doctor tool to diagnose the patient based on symptoms.
65
 
66
+ Args:
67
+ patient_id: Unique identifier for the patient.
68
+
69
+ Returns:
70
+ Diagnosis and recommendations.
71
+ """
72
+ try:
73
+ if patient_id not in self.patient_data:
74
+ raise ValueError("No symptoms found. Nurse must collect symptoms first.")
75
+
76
+ symptoms = self.patient_data[patient_id]["symptoms"]
77
+ diagnosis = self.fetch_diagnosis(symptoms)
78
+ medication = self.fetch_medication(symptoms)
79
+ advice = self.fetch_treatment_advice(symptoms)
80
+
81
+ return (f"Doctor: Based on the symptoms: {symptoms},\n"
82
+ f"Diagnosis: {diagnosis}\n"
83
+ f"Medication: {medication}\n"
84
+ f"Advice: {advice}")
85
+
86
+ except ValueError as e:
87
+ return f"Error: {str(e)}"
88
+ except Exception as e:
89
+ return f"Unexpected Error: {str(e)}"
90
+
91
+ @tool
92
+ def fetch_diagnosis(self, symptoms: str) -> str:
93
+ """
94
+ AI tool to retrieve a diagnosis based on symptoms.
95
+
96
+ Args:
97
+ symptoms: The symptoms provided by the patient.
98
+
99
+ Returns:
100
+ Detailed Diagnosis information.
101
+ """
102
+ search_query = f" A detailed medical diagnosis for this {symptoms}"
103
+ return search_tool(search_query)
104
+ try:
105
+ if not symptoms:
106
+ raise ValueError("Symptoms must be provided.")
107
+
108
+ return f"AI Diagnosis: Potential cause of {symptoms} found."
109
+
110
+ except ValueError as e:
111
+ return f"Error: {str(e)}"
112
+ except Exception as e:
113
+ return f"Unexpected Error: {str(e)}"
114
+
115
+ @tool
116
+ def fetch_medication(self, symptoms: str) -> str:
117
+ """
118
+ AI tool to suggest medications based on symptoms.
119
+
120
+ Args:
121
+ symptoms: The symptoms provided by the patient.
122
+
123
+ Returns:
124
+ Suggested medication.
125
+ """
126
+ search_query = f" A detailed suggested medication for this {symptoms}"
127
+ return search_tool(search_query)
128
+ try:
129
+ if not symptoms:
130
+ raise ValueError("Symptoms must be provided.")
131
+
132
+ return f"Suggested medication for {symptoms} found."
133
+
134
+ except ValueError as e:
135
+ return f"Error: {str(e)}"
136
+ except Exception as e:
137
+ return f"Unexpected Error: {str(e)}"
138
+
139
+ @tool
140
+ def fetch_treatment_advice(self, symptoms: str) -> str:
141
+ """
142
+ AI tool to provide treatment recommendations.
143
+
144
+ Args:
145
+ symptoms: The symptoms provided by the patient.
146
+
147
+ Returns:
148
+ Recommended treatment and advice.
149
+ """
150
+ search_query = f" A detailed recommended treatment and advice for this {symptoms}"
151
+ return search_tool(search_query)
152
+ try:
153
+ if not symptoms:
154
+ raise ValueError("Symptoms must be provided.")
155
+
156
+ return f"Treatment advice for {symptoms} retrieved."
157
+
158
+ except ValueError as e:
159
+ return f"Error: {str(e)}"
160
+ except Exception as e:
161
+ return f"Unexpected Error: {str(e)}"
162
+
163
  search_tool = DuckDuckGoSearchTool()
164
 
165
  @tool
 
199
 
200
  agent = CodeAgent(
201
  model=model,
202
+ tools=[final_answer, collect_symptoms, diagnose_patient, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
203
  max_steps=6,
204
  verbosity_level=1,
205
  grammar=None,
206
  planning_interval=None,
207
+ name="MedicalDoctor",
208
+ description"=The role of the healthcare provider ('nurse' or 'doctor')",
209
  prompt_templates=prompt_templates
210
  )
211