ariankhalfani commited on
Commit
8ad240b
1 Parent(s): 125f3ef

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +13 -11
chatbot.py CHANGED
@@ -202,7 +202,7 @@ def toggle_input_visibility(input_type):
202
  def cleanup_response(response):
203
  # Extract only the part after "Answer:" and remove any trailing spaces
204
  answer_start = response.find("Answer:")
205
- if answer_start != -1:
206
  response = response[answer_start + len("Answer:"):].strip()
207
  return response
208
 
@@ -214,22 +214,24 @@ def chatbot(audio, input_type, text):
214
  return "Error transcribing audio: " + transcription["error"], None
215
  query = transcription['text']
216
  condition, patient_id = extract_details_from_prompt(query)
 
217
  if condition and patient_id:
218
  patient_history = get_specific_patient_data(patient_data, condition, patient_id)
219
- payload = {
220
- "inputs": f"role: ophthalmologist assistant patient history: {patient_history} question: {query}"
221
- }
222
- response = query_huggingface(payload)
223
- if isinstance(response, list):
224
- raw_response = response[0].get("generated_text", "Sorry, I couldn't generate a response.")
225
- else:
226
- raw_response = response.get("generated_text", "Sorry, I couldn't generate a response.")
227
 
228
- clean_response = cleanup_response(raw_response)
229
- return clean_response, None
230
 
231
  elif input_type == "Text":
232
  condition, patient_id = extract_details_from_prompt(text)
 
233
  if condition and patient_id:
234
  patient_history = get_specific_patient_data(patient_data, condition, patient_id)
235
  payload = {
 
202
  def cleanup_response(response):
203
  # Extract only the part after "Answer:" and remove any trailing spaces
204
  answer_start = response.find("Answer:")
205
+ if (answer_start != -1):
206
  response = response[answer_start + len("Answer:"):].strip()
207
  return response
208
 
 
214
  return "Error transcribing audio: " + transcription["error"], None
215
  query = transcription['text']
216
  condition, patient_id = extract_details_from_prompt(query)
217
+ patient_history = ""
218
  if condition and patient_id:
219
  patient_history = get_specific_patient_data(patient_data, condition, patient_id)
220
+ payload = {
221
+ "inputs": f"role: ophthalmologist assistant patient history: {patient_history} question: {query}"
222
+ }
223
+ response = query_huggingface(payload)
224
+ if isinstance(response, list):
225
+ raw_response = response[0].get("generated_text", "Sorry, I couldn't generate a response.")
226
+ else:
227
+ raw_response = response.get("generated_text", "Sorry, I couldn't generate a response.")
228
 
229
+ clean_response = cleanup_response(raw_response)
230
+ return clean_response, None
231
 
232
  elif input_type == "Text":
233
  condition, patient_id = extract_details_from_prompt(text)
234
+ patient_history = ""
235
  if condition and patient_id:
236
  patient_history = get_specific_patient_data(patient_data, condition, patient_id)
237
  payload = {