File size: 2,139 Bytes
6a02c0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from huggingface_hub import InferenceClient

# Initialize the Inference client with the model
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# Function to generate a response
def respond(message, history, 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 = ""

    # Make the API call and stream the 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
        yield response

# Streamlit app layout
st.title("Zephyr Chatbot")

# Textbox for user input
user_message = st.text_input("Your message:")

# Text area for displaying chat history
history = st.session_state.get("history", [])

# System message (initialization)
system_message = st.text_area("System message", value="You are a friendly Chatbot.")

# Sliders for max tokens, temperature, and top-p
max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=512, step=1)
temperature = st.slider("Temperature", min_value=0.1, max_value=4.0, value=0.7, step=0.1)
top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)

# Button to send the message
if st.button("Send"):
    # Get the response from the model
    response_text = ""
    for text in respond(user_message, history, system_message, max_tokens, temperature, top_p):
        response_text = text

    # Update chat history in session state
    history.append((user_message, response_text))
    st.session_state["history"] = history

    # Display chat history
    for user_msg, assistant_msg in history:
        st.write(f"**You:** {user_msg}")
        st.write(f"**Bot:** {assistant_msg}")