Spaces:
Sleeping
Sleeping
Commit
·
d58f82d
1
Parent(s):
8770c25
add application files
Browse files- .gitignore +1 -0
- app.py +86 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# Function to create InferenceClient dynamically based on model selection
|
| 6 |
+
def get_client(model_name):
|
| 7 |
+
return InferenceClient(model_name)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Chat response function
|
| 11 |
+
def respond(message, history, max_tokens, top_p, model_name):
|
| 12 |
+
system_message = "You are a friendly Chatbot."
|
| 13 |
+
client = get_client(model_name)
|
| 14 |
+
|
| 15 |
+
messages = [{"role": "system", "content": system_message}]
|
| 16 |
+
for user_msg, bot_msg in history:
|
| 17 |
+
if user_msg:
|
| 18 |
+
messages.append({"role": "user", "content": user_msg})
|
| 19 |
+
if bot_msg:
|
| 20 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 21 |
+
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
+
response = client.chat_completion(
|
| 25 |
+
messages,
|
| 26 |
+
max_tokens=max_tokens,
|
| 27 |
+
top_p=top_p,
|
| 28 |
+
stream=False,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return response.choices[0].message["content"]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.title(f"🤖 Simple Chatbot")
|
| 36 |
+
st.markdown(
|
| 37 |
+
"This is a simple chatbot application built with Streamlit and Hugging Face Inference API."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Session state untuk menyimpan history chat
|
| 41 |
+
if "chat_history" not in st.session_state:
|
| 42 |
+
st.session_state.chat_history = []
|
| 43 |
+
|
| 44 |
+
# Sidebar untuk konfigurasi
|
| 45 |
+
st.sidebar.title("Settings")
|
| 46 |
+
model_name = st.sidebar.selectbox(
|
| 47 |
+
"Choose a Model",
|
| 48 |
+
[
|
| 49 |
+
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 50 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 51 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 52 |
+
"microsoft/Phi-3.5-mini-instruct",
|
| 53 |
+
],
|
| 54 |
+
index=0,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
max_tokens = st.sidebar.slider("Max new tokens", 1, 2048, 512, 1)
|
| 58 |
+
top_p = st.sidebar.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95, 0.05)
|
| 59 |
+
|
| 60 |
+
# Chat input dari user
|
| 61 |
+
user_input = st.chat_input("Ask anything...")
|
| 62 |
+
|
| 63 |
+
# Tampilkan chat history
|
| 64 |
+
for user_msg, bot_msg in st.session_state.chat_history:
|
| 65 |
+
with st.chat_message("user"):
|
| 66 |
+
st.markdown(user_msg)
|
| 67 |
+
with st.chat_message("assistant"):
|
| 68 |
+
st.markdown(bot_msg)
|
| 69 |
+
|
| 70 |
+
# Ketika ada input baru
|
| 71 |
+
if user_input:
|
| 72 |
+
with st.chat_message("user"):
|
| 73 |
+
st.markdown(user_input)
|
| 74 |
+
|
| 75 |
+
with st.chat_message("assistant"):
|
| 76 |
+
response = respond(
|
| 77 |
+
user_input,
|
| 78 |
+
st.session_state.chat_history,
|
| 79 |
+
max_tokens,
|
| 80 |
+
top_p,
|
| 81 |
+
model_name,
|
| 82 |
+
)
|
| 83 |
+
st.markdown(response)
|
| 84 |
+
|
| 85 |
+
# Simpan ke session state
|
| 86 |
+
st.session_state.chat_history.append((user_input, response))
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
huggingface_hub
|