|
import streamlit as st |
|
from huggingface_hub import InferenceClient |
|
|
|
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
|
def respond( |
|
message, |
|
history: list[tuple[str, str]], |
|
system_message, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
): |
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
response = "" |
|
|
|
for message in client.chat_completion( |
|
messages, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
): |
|
token = message.choices[0].delta.content |
|
response += token |
|
|
|
return response |
|
|
|
def send_message(): |
|
message = st.session_state["new_message"] |
|
if message: |
|
st.session_state.history.append((message, "")) |
|
response = respond( |
|
message=message, |
|
history=st.session_state.history, |
|
system_message=st.session_state.system_message, |
|
max_tokens=st.session_state.max_tokens, |
|
temperature=st.session_state.temperature, |
|
top_p=st.session_state.top_p, |
|
) |
|
st.session_state.history[-1] = (message, response) |
|
st.session_state["new_message"] = "" |
|
|
|
|
|
st.title("Emotional Support Chatbot") |
|
st.write("Hello! I'm here to support you emotionally and answer any questions. How are you feeling today?") |
|
st.markdown("<p style='color:green;'>Developed by Hashir Ehtisham</p>", unsafe_allow_html=True) |
|
|
|
|
|
if 'system_message' not in st.session_state: |
|
st.session_state.system_message = "You are a friendly Emotional Support Chatbot." |
|
|
|
with st.expander("Settings"): |
|
max_tokens_options = [64, 128, 256, 512, 1024, 2048] |
|
temperature_options = [0.1, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0, 3.0, 4.0] |
|
top_p_options = [0.1, 0.2, 0.3, 0.5, 0.7, 0.8, 0.9, 0.95, 1.0] |
|
|
|
max_tokens = st.selectbox("Max new tokens", options=max_tokens_options, index=max_tokens_options.index(512), key="max_tokens") |
|
temperature = st.selectbox("Temperature", options=temperature_options, index=temperature_options.index(0.7), key="temperature") |
|
top_p = st.selectbox("Top-p (nucleus sampling)", options=top_p_options, index=top_p_options.index(0.95), key="top_p") |
|
|
|
if 'history' not in st.session_state: |
|
st.session_state.history = [] |
|
|
|
|
|
st.text_area("Chat History", value="\n\n".join([f"User: {h[0]}\n\nBot: {h[1]}" for h in st.session_state.history]), height=400, key="chat_history") |
|
|
|
message = st.text_input("Your message", key="new_message", on_change=send_message) |
|
|