Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,44 +17,51 @@ def get_gemini_response(prompt):
|
|
17 |
|
18 |
# Streamlit app configuration
|
19 |
st.set_page_config(page_title="Med ChatBot")
|
20 |
-
st.
|
21 |
|
22 |
# Initialize session state for chat history
|
23 |
if "chat_history" not in st.session_state:
|
24 |
st.session_state["chat_history"] = []
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if submit and input_text:
|
31 |
-
# Context for the LLM with history included
|
32 |
-
chat_history_text = " ".join([f"{role}: {text}" for role, text in st.session_state["chat_history"]])
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
full_response = " ".join([chunk.text for chunk in response])
|
52 |
-
st.write(full_response)
|
53 |
-
st.session_state['chat_history'].append(("Bot", full_response))
|
54 |
-
else:
|
55 |
-
st.write(response)
|
56 |
-
st.session_state['chat_history'].append(("Bot", response))
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
for role, text in st.session_state['chat_history']:
|
60 |
-
|
|
|
|
|
|
|
|
17 |
|
18 |
# Streamlit app configuration
|
19 |
st.set_page_config(page_title="Med ChatBot")
|
20 |
+
st.title("Medical ChatBot")
|
21 |
|
22 |
# Initialize session state for chat history
|
23 |
if "chat_history" not in st.session_state:
|
24 |
st.session_state["chat_history"] = []
|
25 |
|
26 |
+
# Function to handle submission (without the button)
|
27 |
+
def submit_input():
|
28 |
+
input_text = st.session_state["input"]
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
if input_text:
|
31 |
+
# Context for the LLM with history included
|
32 |
+
chat_history_text = " ".join([f"{role}: {text}" for role, text in st.session_state["chat_history"]])
|
33 |
+
|
34 |
+
context = (
|
35 |
+
"You are a medical chatbot designed to assist users in understanding their symptoms. "
|
36 |
+
"Provide clear, concise, and informative responses based on NHS guidelines. "
|
37 |
+
"Avoid technical jargon and code snippets. If asked a question unrelated to medical topics, "
|
38 |
+
"respond with: 'I am a medical bot and I don't have that knowledge.' "
|
39 |
+
f"Previous conversation: {chat_history_text} "
|
40 |
+
)
|
41 |
+
|
42 |
+
prompt = f"{context} User's latest input: {input_text}" # Include the latest user input
|
43 |
+
response = get_gemini_response(prompt)
|
44 |
+
|
45 |
+
# Add user query to session state chat history
|
46 |
+
st.session_state['chat_history'].append(("You", input_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Add bot response to session state chat history
|
49 |
+
if isinstance(response, list):
|
50 |
+
full_response = " ".join([chunk.text for chunk in response])
|
51 |
+
st.session_state['chat_history'].append(("Bot", full_response))
|
52 |
+
else:
|
53 |
+
st.session_state['chat_history'].append(("Bot", response))
|
54 |
+
|
55 |
+
# Clear input field after submission
|
56 |
+
st.session_state["input"] = ""
|
57 |
+
|
58 |
+
# Chat input (without button, submit on "Enter")
|
59 |
+
st.text_input("Ask the medical chatbot:", key="input", on_change=submit_input)
|
60 |
+
|
61 |
+
# Display chat history in a chatbox style
|
62 |
+
st.subheader("Chat History")
|
63 |
for role, text in st.session_state['chat_history']:
|
64 |
+
if role == "You":
|
65 |
+
st.markdown(f"<div style='text-align: right; color: blue;'>{role}: {text}</div>", unsafe_allow_html=True)
|
66 |
+
else:
|
67 |
+
st.markdown(f"<div style='text-align: left; color: green;'>{role}: {text}</div>", unsafe_allow_html=True)
|