File size: 2,919 Bytes
eb0685b
e2b285e
eb0685b
e66be57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb0685b
e2b285e
 
e66be57
e2b285e
eb0685b
e66be57
 
 
eb0685b
e2b285e
e66be57
 
e2b285e
e66be57
e2b285e
e66be57
 
 
e2b285e
e66be57
e2b285e
e66be57
e2b285e
e66be57
 
 
e2b285e
e66be57
 
 
 
 
 
e2b285e
e66be57
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit as st
import time

st.set_page_config(
    page_title="Chatbot Streamlit",
    page_icon="🤖",
    layout="wide"
)

st.markdown("""
<style>
.stApp {
    background: linear-gradient(180deg, #0f172a 0%, #111827 100%);
}
.block-container {
    padding-top: 2rem;
    max-width: 1100px;
}
[data-testid="stSidebar"] {
    background: #0b1220;
}
.chat-title {
    text-align: center;
    color: white;
    margin-bottom: 0.3rem;
}
.chat-subtitle {
    text-align: center;
    color: #cbd5e1;
    margin-bottom: 1.5rem;
}
.user-bubble {
    background: #2563eb;
    color: white;
    padding: 12px 16px;
    border-radius: 16px 16px 4px 16px;
    margin: 8px 0 8px auto;
    width: fit-content;
    max-width: 75%;
}
.bot-bubble {
    background: #1f2937;
    color: #f9fafb;
    padding: 12px 16px;
    border-radius: 16px 16px 16px 4px;
    margin: 8px auto 8px 0;
    width: fit-content;
    max-width: 75%;
    border: 1px solid #374151;
}
</style>
""", unsafe_allow_html=True)

with st.sidebar:
    st.title("⚙️ Painel")
    st.write("Chatbot em Streamlit")
    if st.button("🗑️ Nova conversa", use_container_width=True):
        st.session_state.messages = [
            {"role": "assistant", "content": "Olá! Sou o teu chatbot em Streamlit. Como te posso ajudar?"}
        ]
        st.rerun()
    st.info("Podes ligar isto depois a uma API, modelo local ou Hugging Face.")

st.markdown("<h1 class='chat-title'>🤖 Chatbot Streamlit</h1>", unsafe_allow_html=True)
st.markdown("<p class='chat-subtitle'>Versão com visual melhorado e histórico de conversa</p>", unsafe_allow_html=True)

if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "Olá! Sou o teu chatbot em Streamlit. Como te posso ajudar?"}
    ]

for msg in st.session_state.messages:
    css_class = "user-bubble" if msg["role"] == "user" else "bot-bubble"
    st.markdown(f"<div class='{css_class}'>{msg['content']}</div>", unsafe_allow_html=True)

def gerar_resposta(prompt):
    resposta = f"Recebi a tua mensagem: '{prompt}'. Esta é uma resposta exemplo em Streamlit com estilo melhorado."
    texto = ""
    for palavra in resposta.split():
        texto += palavra + " "
        time.sleep(0.03)
        yield texto

prompt = st.chat_input("Escreve a tua mensagem...")

if prompt:
    st.session_state.messages.append({"role": "user", "content": prompt})
    st.rerun()

if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
    placeholder = st.empty()
    resposta_final = ""

    for parcial in gerar_resposta(st.session_state.messages[-1]["content"]):
        resposta_final = parcial
        placeholder.markdown(
            f"<div class='bot-bubble'>{resposta_final}</div>",
            unsafe_allow_html=True
        )

    st.session_state.messages.append({"role": "assistant", "content": resposta_final})
    st.rerun()