Spaces:
Build error
Build error
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}") | |