|
import streamlit as st |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
|
|
|
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 = "" |
|
|
|
|
|
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 |
|
|
|
|
|
st.title("Zephyr Chatbot") |
|
|
|
|
|
user_message = st.text_input("Your message:") |
|
|
|
|
|
history = st.session_state.get("history", []) |
|
|
|
|
|
system_message = st.text_area("System message", value="You are a friendly Chatbot.") |
|
|
|
|
|
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) |
|
|
|
|
|
if st.button("Send"): |
|
|
|
response_text = "" |
|
for text in respond(user_message, history, system_message, max_tokens, temperature, top_p): |
|
response_text = text |
|
|
|
|
|
history.append((user_message, response_text)) |
|
st.session_state["history"] = history |
|
|
|
|
|
for user_msg, assistant_msg in history: |
|
st.write(f"**You:** {user_msg}") |
|
st.write(f"**Bot:** {assistant_msg}") |
|
|