File size: 2,821 Bytes
45f2aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6c6602
45f2aac
d0f5dfb
 
 
45f2aac
 
 
 
 
 
 
 
 
 
 
 
 
 
5ef0cb6
45f2aac
9eab490
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"] = ""

# Streamlit UI
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)

# Set default system message directly
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 = []

# Display chat history above the message input
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)