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(""" """, 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'
{speaker}: {text}
', unsafe_allow_html=True)