Programer123 commited on
Commit
e66be57
·
verified ·
1 Parent(s): e50af67

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +85 -14
streamlit_app.py CHANGED
@@ -1,31 +1,102 @@
1
  import streamlit as st
2
  import time
3
 
4
- st.set_page_config(page_title="Meu Chatbot", page_icon="💬", layout="centered")
5
- st.title("💬 Meu Chatbot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  if "messages" not in st.session_state:
8
  st.session_state.messages = [
9
- {"role": "assistant", "content": "Olá! Sou o teu chatbot. Em que posso ajudar?"}
10
  ]
11
 
12
- for message in st.session_state.messages:
13
- with st.chat_message(message["role"]):
14
- st.markdown(message["content"])
15
 
16
  def gerar_resposta(prompt):
17
- resposta = f"Tu disseste: {prompt}. Isto é uma resposta de teste mais bonita."
 
18
  for palavra in resposta.split():
19
- yield palavra + " "
20
  time.sleep(0.03)
 
 
 
21
 
22
- if prompt := st.chat_input("Escreve a tua mensagem..."):
23
  st.session_state.messages.append({"role": "user", "content": prompt})
 
24
 
25
- with st.chat_message("user"):
26
- st.markdown(prompt)
 
27
 
28
- with st.chat_message("assistant"):
29
- resposta_final = st.write_stream(gerar_resposta(prompt))
 
 
 
 
30
 
31
- st.session_state.messages.append({"role": "assistant", "content": resposta_final})
 
 
1
  import streamlit as st
2
  import time
3
 
4
+ st.set_page_config(
5
+ page_title="Chatbot Streamlit",
6
+ page_icon="🤖",
7
+ layout="wide"
8
+ )
9
+
10
+ st.markdown("""
11
+ <style>
12
+ .stApp {
13
+ background: linear-gradient(180deg, #0f172a 0%, #111827 100%);
14
+ }
15
+ .block-container {
16
+ padding-top: 2rem;
17
+ max-width: 1100px;
18
+ }
19
+ [data-testid="stSidebar"] {
20
+ background: #0b1220;
21
+ }
22
+ .chat-title {
23
+ text-align: center;
24
+ color: white;
25
+ margin-bottom: 0.3rem;
26
+ }
27
+ .chat-subtitle {
28
+ text-align: center;
29
+ color: #cbd5e1;
30
+ margin-bottom: 1.5rem;
31
+ }
32
+ .user-bubble {
33
+ background: #2563eb;
34
+ color: white;
35
+ padding: 12px 16px;
36
+ border-radius: 16px 16px 4px 16px;
37
+ margin: 8px 0 8px auto;
38
+ width: fit-content;
39
+ max-width: 75%;
40
+ }
41
+ .bot-bubble {
42
+ background: #1f2937;
43
+ color: #f9fafb;
44
+ padding: 12px 16px;
45
+ border-radius: 16px 16px 16px 4px;
46
+ margin: 8px auto 8px 0;
47
+ width: fit-content;
48
+ max-width: 75%;
49
+ border: 1px solid #374151;
50
+ }
51
+ </style>
52
+ """, unsafe_allow_html=True)
53
+
54
+ with st.sidebar:
55
+ st.title("⚙️ Painel")
56
+ st.write("Chatbot em Streamlit")
57
+ if st.button("🗑️ Nova conversa", use_container_width=True):
58
+ st.session_state.messages = [
59
+ {"role": "assistant", "content": "Olá! Sou o teu chatbot em Streamlit. Como te posso ajudar?"}
60
+ ]
61
+ st.rerun()
62
+ st.info("Podes ligar isto depois a uma API, modelo local ou Hugging Face.")
63
+
64
+ st.markdown("<h1 class='chat-title'>🤖 Chatbot Streamlit</h1>", unsafe_allow_html=True)
65
+ st.markdown("<p class='chat-subtitle'>Versão com visual melhorado e histórico de conversa</p>", unsafe_allow_html=True)
66
 
67
  if "messages" not in st.session_state:
68
  st.session_state.messages = [
69
+ {"role": "assistant", "content": "Olá! Sou o teu chatbot em Streamlit. Como te posso ajudar?"}
70
  ]
71
 
72
+ for msg in st.session_state.messages:
73
+ css_class = "user-bubble" if msg["role"] == "user" else "bot-bubble"
74
+ st.markdown(f"<div class='{css_class}'>{msg['content']}</div>", unsafe_allow_html=True)
75
 
76
  def gerar_resposta(prompt):
77
+ resposta = f"Recebi a tua mensagem: '{prompt}'. Esta é uma resposta exemplo em Streamlit com estilo melhorado."
78
+ texto = ""
79
  for palavra in resposta.split():
80
+ texto += palavra + " "
81
  time.sleep(0.03)
82
+ yield texto
83
+
84
+ prompt = st.chat_input("Escreve a tua mensagem...")
85
 
86
+ if prompt:
87
  st.session_state.messages.append({"role": "user", "content": prompt})
88
+ st.rerun()
89
 
90
+ if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
91
+ placeholder = st.empty()
92
+ resposta_final = ""
93
 
94
+ for parcial in gerar_resposta(st.session_state.messages[-1]["content"]):
95
+ resposta_final = parcial
96
+ placeholder.markdown(
97
+ f"<div class='bot-bubble'>{resposta_final}</div>",
98
+ unsafe_allow_html=True
99
+ )
100
 
101
+ st.session_state.messages.append({"role": "assistant", "content": resposta_final})
102
+ st.rerun()