Spaces:
Runtime error
Runtime error
Update chatbot.py
Browse files- chatbot.py +3 -39
chatbot.py
CHANGED
|
@@ -167,36 +167,6 @@ glaucoma_db_path = 'glaucoma_results.db'
|
|
| 167 |
patient_data = fetch_patient_data(cataract_db_path, glaucoma_db_path)
|
| 168 |
readable_patient_data = transform_patient_data(patient_data)
|
| 169 |
|
| 170 |
-
# Function to extract details from the input prompt
|
| 171 |
-
def extract_details_from_prompt(prompt):
|
| 172 |
-
pattern = re.compile(r"(Glaucoma|Cataract) (\d+)", re.IGNORECASE)
|
| 173 |
-
matches = pattern.findall(prompt)
|
| 174 |
-
return [(match[0].capitalize(), int(match[1])) for match in matches]
|
| 175 |
-
|
| 176 |
-
# Function to fetch specific patient data based on the condition and ID
|
| 177 |
-
def get_specific_patient_data(patient_data, condition, patient_id):
|
| 178 |
-
specific_data = ""
|
| 179 |
-
if condition == "Cataract":
|
| 180 |
-
specific_data = "Cataract Results:\n"
|
| 181 |
-
for row in patient_data.get('cataract_results', []):
|
| 182 |
-
if isinstance(row, tuple) and row[0] == patient_id:
|
| 183 |
-
specific_data += f"Patient ID: {row[0]}, Red Quantity: {row[2]}, Green Quantity: {row[3]}, Blue Quantity: {row[4]}, Stage: {row[5]}\n"
|
| 184 |
-
break
|
| 185 |
-
elif condition == "Glaucoma":
|
| 186 |
-
specific_data = "Glaucoma Results:\n"
|
| 187 |
-
for row in patient_data.get('results', []):
|
| 188 |
-
if isinstance(row, tuple) and row[0] == patient_id:
|
| 189 |
-
specific_data += f"Patient ID: {row[0]}, Cup Area: {row[2]}, Disk Area: {row[3]}, Rim Area: {row[4]}, Rim to Disc Line Ratio: {row[5]}, DDLS Stage: {row[6]}\n"
|
| 190 |
-
break
|
| 191 |
-
return specific_data
|
| 192 |
-
|
| 193 |
-
# Function to aggregate patient history for all mentioned IDs in the question
|
| 194 |
-
def get_aggregated_patient_history(patient_data, details):
|
| 195 |
-
history = ""
|
| 196 |
-
for condition, patient_id in details:
|
| 197 |
-
history += get_specific_patient_data(patient_data, condition, patient_id) + "\n"
|
| 198 |
-
return history.strip()
|
| 199 |
-
|
| 200 |
# Toggle visibility of input elements based on input type
|
| 201 |
def toggle_visibility(input_type):
|
| 202 |
if input_type == "Voice":
|
|
@@ -211,6 +181,7 @@ def cleanup_response(response):
|
|
| 211 |
response = response[answer_start + len("Answer:"):].strip()
|
| 212 |
return response
|
| 213 |
|
|
|
|
| 214 |
def chatbot(audio, input_type, text):
|
| 215 |
if input_type == "Voice":
|
| 216 |
transcription = query_whisper(audio.name)
|
|
@@ -220,20 +191,13 @@ def chatbot(audio, input_type, text):
|
|
| 220 |
else:
|
| 221 |
query = text
|
| 222 |
|
| 223 |
-
#
|
| 224 |
-
details = extract_details_from_prompt(query)
|
| 225 |
-
|
| 226 |
-
# Get aggregated patient history based on the extracted details
|
| 227 |
-
patient_history = get_aggregated_patient_history(patient_data, details)
|
| 228 |
-
|
| 229 |
-
# Create the payload with the patient history and the user's query
|
| 230 |
payload = {
|
| 231 |
-
"inputs": f"role: ophthalmologist assistant patient history: {
|
| 232 |
}
|
| 233 |
|
| 234 |
logging.debug(f"Raw input to the LLM: {payload['inputs']}")
|
| 235 |
|
| 236 |
-
# Query the Hugging Face model with the payload
|
| 237 |
response = query_huggingface(payload)
|
| 238 |
if isinstance(response, list):
|
| 239 |
raw_response = response[0].get("generated_text", "Sorry, I couldn't generate a response.")
|
|
|
|
| 167 |
patient_data = fetch_patient_data(cataract_db_path, glaucoma_db_path)
|
| 168 |
readable_patient_data = transform_patient_data(patient_data)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
# Toggle visibility of input elements based on input type
|
| 171 |
def toggle_visibility(input_type):
|
| 172 |
if input_type == "Voice":
|
|
|
|
| 181 |
response = response[answer_start + len("Answer:"):].strip()
|
| 182 |
return response
|
| 183 |
|
| 184 |
+
# Gradio interface for the chatbot
|
| 185 |
def chatbot(audio, input_type, text):
|
| 186 |
if input_type == "Voice":
|
| 187 |
transcription = query_whisper(audio.name)
|
|
|
|
| 191 |
else:
|
| 192 |
query = text
|
| 193 |
|
| 194 |
+
# Directly use the transformed patient data as context input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
payload = {
|
| 196 |
+
"inputs": f"role: ophthalmologist assistant patient history: {readable_patient_data} question: {query}"
|
| 197 |
}
|
| 198 |
|
| 199 |
logging.debug(f"Raw input to the LLM: {payload['inputs']}")
|
| 200 |
|
|
|
|
| 201 |
response = query_huggingface(payload)
|
| 202 |
if isinstance(response, list):
|
| 203 |
raw_response = response[0].get("generated_text", "Sorry, I couldn't generate a response.")
|