import streamlit as st import time st.set_page_config( page_title="Chatbot Streamlit", page_icon="🤖", layout="wide" ) st.markdown(""" """, 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("

🤖 Chatbot Streamlit

", unsafe_allow_html=True) st.markdown("

VersĂŁo com visual melhorado e histĂłrico de conversa

", 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"
{msg['content']}
", 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"
{resposta_final}
", unsafe_allow_html=True ) st.session_state.messages.append({"role": "assistant", "content": resposta_final}) st.rerun()