evelynstarlight's picture
Deploy chatbot to Hugging Face
f5e6b7b
import streamlit as st
from utils import get_response, apply_tone, apply_language, apply_format
from config import MODEL_LIST
import os
from dotenv import load_dotenv
# πŸ” Load Hugging Face token dari .env atau sidebar
load_dotenv()
hf_token = st.sidebar.text_input("πŸ”‘ Hugging Face Token", type="password")
if hf_token:
os.environ["HF_TOKEN"] = hf_token
# πŸ’… Custom background and styling
st.markdown("""
<style>
.stApp {
background-color: #fff8dc; /* warm yellow */
}
.user-bubble {
background-color: #fef3c7; /* soft yellow */
padding: 10px;
border-radius: 10px;
margin-bottom: 5px;
}
.bot-bubble {
background-color: #e0f2fe; /* soft blue */
padding: 10px;
border-radius: 10px;
margin-bottom: 10px;
}
.element-container pre {
background-color: #1e1e1e !important; /* dark code block */
color: #dcdcdc !important;
border-radius: 8px;
padding: 12px;
font-size: 14px;
}
</style>
""", unsafe_allow_html=True)
# 🧠 Page config
st.set_page_config(page_title="Mini Chatbot", layout="wide")
st.title("πŸ’¬ Chat with Your LLM")
# πŸ”§ Sidebar settings
st.sidebar.title("πŸ”§ Settings")
# ❌ Kalau mau aktifin OpenAI lagi, uncomment ini:
# openai_api_key = st.sidebar.text_input("πŸ”‘ OpenAI API Key", type="password")
# if openai_api_key:
# os.environ["OPENAI_API_KEY"] = openai_api_key
# use_openai = st.sidebar.checkbox("Use OpenAI backend", value=True)
model_choice = st.sidebar.selectbox("Choose Model", MODEL_LIST)
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7)
max_tokens = st.sidebar.slider("Max Tokens", 100, 1000, 500)
tone = st.sidebar.selectbox("🎭 Pilih Gaya Bahasa (Tone)", ["Formal", "Casual", "Humorous", "Professional", "Sarcastic"])
language = st.sidebar.selectbox("🌐 Pilih Bahasa Respon", ["Indonesia", "English", "Chinese"])
response_format = st.sidebar.selectbox("🧾 Format Respon", ["Text", "Bullet Points", "Code"])
# πŸ’¬ Chat history
if "history" not in st.session_state:
st.session_state.history = []
# πŸ“ Chat input
user_input = st.chat_input("Type your message here...")
if user_input:
with st.spinner("Thinking..."):
modified_prompt = apply_tone(user_input, tone)
modified_prompt = apply_language(modified_prompt, language)
modified_prompt = apply_format(modified_prompt, response_format)
response = get_response(modified_prompt, "HuggingFace", model_choice, temperature, max_tokens)
decorated_response = f"πŸ€– {response}"
st.session_state.history.append(("You", user_input))
st.session_state.history.append(("Bot", decorated_response))
# πŸ–ΌοΈ Display chat (user + bot)
for speaker, text in st.session_state.history:
bubble_class = "user-bubble" if speaker == "You" else "bot-bubble"
if speaker == "Bot" and response_format == "Code":
st.code(text.replace("πŸ€– ", ""), language="python")
else:
st.markdown(f'<div class="{bubble_class}"><strong>{speaker}:</strong> {text}</div>', unsafe_allow_html=True)