sabssag commited on
Commit
9571559
1 Parent(s): b2c0bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
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.header("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
- # Input and submission button
27
- input_text = st.text_input("Input: ", key="input")
28
- submit = st.button("Ask the question")
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
- 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
- st.subheader("The Response is")
49
- # If the response is a list of chunks, combine them into a single string
50
- if isinstance(response, list):
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
- st.subheader("The Chat History is")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  for role, text in st.session_state['chat_history']:
60
- st.write(f"{role}: {text}")
 
 
 
 
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)