Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,31 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
# Initialize the InferenceClient
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
for val in history:
|
| 12 |
-
if val[
|
| 13 |
-
messages.append({"role": "user", "content": val[
|
| 14 |
-
|
| 15 |
-
messages.append({"role": "assistant", "content": val[
|
| 16 |
|
| 17 |
messages.append({"role": "user", "content": message})
|
| 18 |
|
|
|
|
| 19 |
response = ""
|
| 20 |
-
response_container = st.empty() # Placeholder to update the response text
|
| 21 |
|
| 22 |
for message in client.chat_completion(
|
| 23 |
messages,
|
|
@@ -27,41 +38,41 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 27 |
top_p=top_p,
|
| 28 |
):
|
| 29 |
token = message.choices[0].delta.content
|
| 30 |
-
|
| 31 |
response += token
|
| 32 |
-
response_container.text(response) #
|
| 33 |
|
| 34 |
-
|
| 35 |
-
st.title("Health Care ChatBot")
|
| 36 |
|
|
|
|
| 37 |
with st.sidebar:
|
| 38 |
-
# User inputs
|
| 39 |
max_tokens = st.slider("Max new tokens", 1, 2048, 512)
|
| 40 |
temperature = st.slider("Temperature", 0.1, 4.0, 0.7)
|
| 41 |
top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
#system message
|
| 47 |
-
system_message = "You are a knowledgeable and empathetic medical assistant providing accurate and compassionate health advice based on user input."
|
| 48 |
-
#input message
|
| 49 |
-
message = st.text_input("User message", key="user_message")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Append the new message to the history
|
| 55 |
-
history.append((message, "")) # No assistant response yet
|
| 56 |
-
|
| 57 |
-
# Call the respond function and update the history with the assistant's reply
|
| 58 |
-
for response in respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 59 |
-
history[-1] = (message, response)
|
| 60 |
-
|
| 61 |
-
# Save updated history in session state
|
| 62 |
-
st.session_state['history'] = history
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Initialize the InferenceClient
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Streamlit app configuration
|
| 8 |
+
st.set_page_config(page_title="Health Care ChatBot", layout="wide")
|
| 9 |
+
st.title("Health Care ChatBot")
|
| 10 |
+
|
| 11 |
+
# Initialize session state for messages if not present
|
| 12 |
+
if 'messages' not in st.session_state:
|
| 13 |
+
st.session_state.messages = [
|
| 14 |
+
{"role": "system", "content": "You are a knowledgeable and empathetic medical assistant providing accurate and compassionate health advice based on user input."}
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
def respond(message, history, max_tokens, temperature, top_p):
|
| 18 |
+
# Prepare the list of messages for the chat completion
|
| 19 |
+
messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]
|
| 20 |
|
| 21 |
for val in history:
|
| 22 |
+
if val["role"] == "user":
|
| 23 |
+
messages.append({"role": "user", "content": val["content"]})
|
| 24 |
+
elif val["role"] == "assistant":
|
| 25 |
+
messages.append({"role": "assistant", "content": val["content"]})
|
| 26 |
|
| 27 |
messages.append({"role": "user", "content": message})
|
| 28 |
|
| 29 |
+
# Generate response
|
| 30 |
response = ""
|
| 31 |
+
response_container = st.empty() # Placeholder to update the response text dynamically
|
| 32 |
|
| 33 |
for message in client.chat_completion(
|
| 34 |
messages,
|
|
|
|
| 38 |
top_p=top_p,
|
| 39 |
):
|
| 40 |
token = message.choices[0].delta.content
|
|
|
|
| 41 |
response += token
|
| 42 |
+
response_container.text(response) # Stream the response
|
| 43 |
|
| 44 |
+
return response
|
|
|
|
| 45 |
|
| 46 |
+
# Sidebar for parameters
|
| 47 |
with st.sidebar:
|
|
|
|
| 48 |
max_tokens = st.slider("Max new tokens", 1, 2048, 512)
|
| 49 |
temperature = st.slider("Temperature", 0.1, 4.0, 0.7)
|
| 50 |
top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95)
|
| 51 |
|
| 52 |
+
# Display chat messages from history
|
| 53 |
+
for message in st.session_state.messages:
|
| 54 |
+
if message["role"] == "user":
|
| 55 |
+
with st.chat_message("user"):
|
| 56 |
+
st.write(message["content"])
|
| 57 |
+
|
| 58 |
+
elif message["role"] == "assistant":
|
| 59 |
+
with st.chat_message("assistant"):
|
| 60 |
+
st.write(message["content"])
|
| 61 |
|
| 62 |
+
# Get user input
|
| 63 |
+
user_input = st.text_input("You:", key="user_message")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
if user_input:
|
| 66 |
+
# Append user message to the chat history
|
| 67 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Generate assistant response
|
| 70 |
+
response = respond(user_input, st.session_state.messages, max_tokens, temperature, top_p)
|
| 71 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 72 |
+
|
| 73 |
+
# Display the latest messages
|
| 74 |
+
with st.chat_message("user"):
|
| 75 |
+
st.write(user_input)
|
| 76 |
+
|
| 77 |
+
with st.chat_message("assistant"):
|
| 78 |
+
st.write(response)
|