|
|
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_dotenv() |
|
|
hf_token = st.sidebar.text_input("π Hugging Face Token", type="password") |
|
|
if hf_token: |
|
|
os.environ["HF_TOKEN"] = hf_token |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Mini Chatbot", layout="wide") |
|
|
st.title("π¬ Chat with Your LLM") |
|
|
|
|
|
|
|
|
st.sidebar.title("π§ Settings") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]) |
|
|
|
|
|
|
|
|
if "history" not in st.session_state: |
|
|
st.session_state.history = [] |
|
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
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) |