File size: 2,170 Bytes
982ff1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from datetime import datetime
import random

# --- CONFIGURATION ---
APP_TITLE = "πŸ€– Streamlit Chatbot"
AVATAR_BOT = "πŸ€–"
AVATAR_USER = "πŸ§‘β€πŸ’»"

# --- SESSION STATE INITIALIZATION ---
if "messages" not in st.session_state:
    st.session_state.messages = []  # list of dicts: {"role": "user"|"assistant", "content": str}

# --- HELPER FUNCTIONS ---
def fake_llm_response(user_input: str) -> str:
    """
    A very small, fake language model.
    Replace this function with a real LLM call (OpenAI, Anthropic, local model, etc.).
    """
    responses = [
        "Interesting point! Tell me more.",
        "I see what you mean. Could you elaborate?",
        "That's a great question. I'm still learning, but here's what I think...",
        "Hmm, I hadn't considered that angle.",
        "Thanks for sharing that with me!",
    ]
    return random.choice(responses) + f"\n\n*(You said: {user_input})*"

# --- STREAMLIT UI ---
st.set_page_config(page_title=APP_TITLE, page_icon=AVATAR_BOT)
st.title(APP_TITLE)

# --- CHAT HISTORY ---
for message in st.session_state.messages:
    avatar = AVATAR_USER if message["role"] == "user" else AVATAR_BOT
    with st.chat_message(message["role"], avatar=avatar):
        st.markdown(message["content"])

# --- USER INPUT ---
if prompt := st.chat_input("Type your message here..."):
    # 1. Append user message to session state
    st.session_state.messages.append({"role": "user", "content": prompt})

    # 2. Display user message immediately
    with st.chat_message("user", avatar=AVATAR_USER):
        st.markdown(prompt)

    # 3. Generate assistant response
    response = fake_llm_response(prompt)

    # 4. Append assistant response to session state
    st.session_state.messages.append({"role": "assistant", "content": response})

    # 5. Display assistant response
    with st.chat_message("assistant", avatar=AVATAR_BOT):
        st.markdown(response)

# --- SIDEBAR OPTIONS ---
with st.sidebar:
    st.header("Options")
    if st.button("Clear Chat"):
        st.session_state.messages = []
        st.rerun()
    st.caption(f"Chat started at {datetime.now():%Y-%m-%d %H:%M:%S}")