Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,7 @@ if st.button("Send"):
|
|
| 33 |
A headache can have many causes, ranging from stress, dehydration, or fatigue to more severe conditions like migraines, infections, or neurological problems. Common remedies include over-the-counter pain relievers, hydration, and rest.
|
| 34 |
If headaches are persistent or severe, it may indicate an underlying condition such as tension headaches, cluster headaches, or even infections like sinusitis. If the headache is accompanied by other symptoms such as nausea, vision changes, or confusion, it is recommended to seek medical attention.
|
| 35 |
"""
|
|
|
|
| 36 |
inputs = medical_tokenizer.encode_plus(user_input, context, add_special_tokens=True, return_tensors="pt")
|
| 37 |
input_ids = inputs["input_ids"].tolist()[0]
|
| 38 |
|
|
@@ -44,16 +45,17 @@ if st.button("Send"):
|
|
| 44 |
|
| 45 |
answer_start = torch.argmax(answer_start_scores)
|
| 46 |
answer_end = torch.argmax(answer_end_scores) + 1
|
|
|
|
| 47 |
medical_answer = medical_tokenizer.convert_tokens_to_string(medical_tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if medical_answer.strip()
|
| 51 |
medical_answer = "I'm not sure about that. You may want to consult a medical professional."
|
| 52 |
-
|
| 53 |
# Append medical response to the conversation history
|
| 54 |
st.session_state.conversation_history.append(f"Bot (Medical): {medical_answer}")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
conversation_input_ids = conversation_tokenizer.encode(user_input + conversation_tokenizer.eos_token, return_tensors='pt')
|
| 58 |
conversation_bot_input_ids = torch.cat([conversation_tokenizer.encode(convo + conversation_tokenizer.eos_token, return_tensors='pt') for convo in st.session_state.conversation_history], dim=-1)
|
| 59 |
|
|
@@ -64,8 +66,11 @@ if st.button("Send"):
|
|
| 64 |
# Append conversational response to conversation history
|
| 65 |
st.session_state.conversation_history.append(f"Bot (Conversational): {conversation_response}")
|
| 66 |
|
| 67 |
-
# Display conversation history
|
| 68 |
for message in st.session_state.conversation_history:
|
| 69 |
st.write(message)
|
| 70 |
else:
|
| 71 |
st.write("Please enter a medical question.")
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
A headache can have many causes, ranging from stress, dehydration, or fatigue to more severe conditions like migraines, infections, or neurological problems. Common remedies include over-the-counter pain relievers, hydration, and rest.
|
| 34 |
If headaches are persistent or severe, it may indicate an underlying condition such as tension headaches, cluster headaches, or even infections like sinusitis. If the headache is accompanied by other symptoms such as nausea, vision changes, or confusion, it is recommended to seek medical attention.
|
| 35 |
"""
|
| 36 |
+
|
| 37 |
inputs = medical_tokenizer.encode_plus(user_input, context, add_special_tokens=True, return_tensors="pt")
|
| 38 |
input_ids = inputs["input_ids"].tolist()[0]
|
| 39 |
|
|
|
|
| 45 |
|
| 46 |
answer_start = torch.argmax(answer_start_scores)
|
| 47 |
answer_end = torch.argmax(answer_end_scores) + 1
|
| 48 |
+
|
| 49 |
medical_answer = medical_tokenizer.convert_tokens_to_string(medical_tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
|
| 50 |
|
| 51 |
+
# Check if a valid medical answer was found
|
| 52 |
+
if not medical_answer.strip(): # If the answer is empty or only whitespace
|
| 53 |
medical_answer = "I'm not sure about that. You may want to consult a medical professional."
|
| 54 |
+
|
| 55 |
# Append medical response to the conversation history
|
| 56 |
st.session_state.conversation_history.append(f"Bot (Medical): {medical_answer}")
|
| 57 |
|
| 58 |
+
# Generate conversational response with DialoGPT
|
| 59 |
conversation_input_ids = conversation_tokenizer.encode(user_input + conversation_tokenizer.eos_token, return_tensors='pt')
|
| 60 |
conversation_bot_input_ids = torch.cat([conversation_tokenizer.encode(convo + conversation_tokenizer.eos_token, return_tensors='pt') for convo in st.session_state.conversation_history], dim=-1)
|
| 61 |
|
|
|
|
| 66 |
# Append conversational response to conversation history
|
| 67 |
st.session_state.conversation_history.append(f"Bot (Conversational): {conversation_response}")
|
| 68 |
|
| 69 |
+
# Display the entire conversation history
|
| 70 |
for message in st.session_state.conversation_history:
|
| 71 |
st.write(message)
|
| 72 |
else:
|
| 73 |
st.write("Please enter a medical question.")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|