Spaces:
Sleeping
Sleeping
File size: 2,383 Bytes
d58f82d 23fe003 d58f82d f54faaa d58f82d | 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 78 79 80 81 82 83 84 85 86 87 | import streamlit as st
from huggingface_hub import InferenceClient
# Function to create InferenceClient dynamically based on model selection
def get_client(model_name):
return InferenceClient(model_name)
# Chat response function
def respond(message, history, max_tokens, top_p, model_name):
system_message = "You are a friendly Chatbot."
client = get_client(model_name)
messages = [{"role": "system", "content": system_message}]
for user_msg, bot_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
response = client.chat_completion(
messages,
max_tokens=max_tokens,
top_p=top_p,
stream=False,
)
return response.choices[0].message["content"]
# Streamlit UI
st.title("🤖 Chatbot")
st.markdown(
"This is a simple chatbot application built with Streamlit and Hugging Face Inference API."
)
# Session state untuk menyimpan history chat
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# Sidebar untuk konfigurasi
st.sidebar.title("Settings")
model_name = st.sidebar.selectbox(
"Choose a Model",
[
"meta-llama/Llama-3.1-8B-Instruct",
"mistralai/Mistral-7B-Instruct-v0.3",
"HuggingFaceH4/zephyr-7b-beta",
"microsoft/Phi-3.5-mini-instruct",
],
index=0,
)
max_tokens = st.sidebar.slider("Max new tokens", 1, 2048, 512, 1)
top_p = st.sidebar.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95, 0.05)
# Chat input dari user
user_input = st.chat_input("Ask anything...")
# Tampilkan chat history
for user_msg, bot_msg in st.session_state.chat_history:
with st.chat_message("user"):
st.markdown(user_msg)
with st.chat_message("assistant"):
st.markdown(bot_msg)
# Ketika ada input baru
if user_input:
with st.chat_message("user"):
st.markdown(user_input)
with st.chat_message("assistant"):
response = respond(
user_input,
st.session_state.chat_history,
max_tokens,
top_p,
model_name,
)
st.markdown(response)
# Simpan ke session state
st.session_state.chat_history.append((user_input, response))
|