Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
import time
|
|
@@ -5,25 +7,32 @@ import time
|
|
| 5 |
# 1. Título da Página e Configuração de Layout
|
| 6 |
st.set_page_config(page_title="Iza - Assistente Groq", layout="wide")
|
| 7 |
|
| 8 |
-
# --- CSS CORRIGIDO E ATUALIZADO ---
|
| 9 |
-
# Este CSS oculta os avatares e justifica o texto da conversa.
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
| 12 |
-
/* Oculta o
|
| 13 |
[data-testid="stChatAvatar"] {
|
| 14 |
-
display: none;
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
-
|
|
|
|
| 17 |
[data-testid="stChatMessage"] {
|
| 18 |
padding-left: 0px;
|
| 19 |
padding-right: 0px;
|
| 20 |
-
gap: 0.
|
| 21 |
}
|
| 22 |
-
|
|
|
|
| 23 |
[data-testid="stChatMessageContent"] {
|
| 24 |
text-align: justify;
|
| 25 |
width: 100%;
|
| 26 |
}
|
|
|
|
|
|
|
| 27 |
table {
|
| 28 |
width: 100% !important;
|
| 29 |
table-layout: fixed;
|
|
@@ -48,6 +57,7 @@ with st.sidebar:
|
|
| 48 |
# A lógica para usar o arquivo seria adicionada aqui.
|
| 49 |
|
| 50 |
# 3. Configuração do Cliente Groq
|
|
|
|
| 51 |
client = Groq()
|
| 52 |
|
| 53 |
# 4. Inicialização do Histórico da Conversa
|
|
@@ -62,6 +72,7 @@ if "messages" not in st.session_state:
|
|
| 62 |
st.session_state.messages = [{"role": "system", "content": system_prompt}]
|
| 63 |
|
| 64 |
# 5. Exibição das Mensagens Anteriores
|
|
|
|
| 65 |
for message in st.session_state.messages:
|
| 66 |
if message["role"] != "system":
|
| 67 |
with st.chat_message(message["role"]):
|
|
@@ -69,15 +80,16 @@ for message in st.session_state.messages:
|
|
| 69 |
|
| 70 |
# 6. Lógica de Interação do Chat
|
| 71 |
if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
|
|
|
| 72 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 73 |
with st.chat_message("user"):
|
| 74 |
st.markdown(prompt)
|
| 75 |
|
|
|
|
| 76 |
with st.chat_message("assistant"):
|
| 77 |
try:
|
| 78 |
def stream_groq_response():
|
| 79 |
-
#
|
| 80 |
-
# O correto é 'client.chat.completions.create' com um ponto.
|
| 81 |
stream = client.chat.completions.create(
|
| 82 |
model="groq/compound",
|
| 83 |
messages=[
|
|
@@ -96,11 +108,16 @@ if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
|
| 96 |
full_response = ""
|
| 97 |
for token in stream_groq_response():
|
| 98 |
full_response += token
|
|
|
|
| 99 |
placeholder.markdown(full_response + "▌")
|
| 100 |
-
|
|
|
|
| 101 |
placeholder.markdown(full_response)
|
| 102 |
|
|
|
|
| 103 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
-
st.error(f"Ocorreu um erro ao contatar a API da Groq: {e}")
|
|
|
|
|
|
|
|
|
| 1 |
+
#--- START OF FILE app (23).py ---
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
from groq import Groq
|
| 5 |
import time
|
|
|
|
| 7 |
# 1. Título da Página e Configuração de Layout
|
| 8 |
st.set_page_config(page_title="Iza - Assistente Groq", layout="wide")
|
| 9 |
|
| 10 |
+
# --- CSS CORRIGIDO E ATUALIZADO (REMOÇÃO DO AVATAR E ESPAÇO) ---
|
| 11 |
+
# Este CSS oculta os avatares, remove o espaço que ocupavam e justifica o texto da conversa.
|
| 12 |
st.markdown("""
|
| 13 |
<style>
|
| 14 |
+
/* Oculta e remove o espaço reservado para o avatar (ícone) */
|
| 15 |
[data-testid="stChatAvatar"] {
|
| 16 |
+
display: none !important;
|
| 17 |
+
width: 0px !important;
|
| 18 |
+
height: 0px !important;
|
| 19 |
+
min-width: 0px !important;
|
| 20 |
}
|
| 21 |
+
|
| 22 |
+
/* Remove o espaçamento extra para alinhar o texto à esquerda e remove o gap */
|
| 23 |
[data-testid="stChatMessage"] {
|
| 24 |
padding-left: 0px;
|
| 25 |
padding-right: 0px;
|
| 26 |
+
gap: 0.0rem; /* Garante que o espaçamento entre o avatar (agora zero) e o texto seja zero */
|
| 27 |
}
|
| 28 |
+
|
| 29 |
+
/* Mantém a justificação do texto e garante a largura total para o conteúdo da mensagem */
|
| 30 |
[data-testid="stChatMessageContent"] {
|
| 31 |
text-align: justify;
|
| 32 |
width: 100%;
|
| 33 |
}
|
| 34 |
+
|
| 35 |
+
/* Regras de Tabela (Mantidas para garantir a legibilidade) */
|
| 36 |
table {
|
| 37 |
width: 100% !important;
|
| 38 |
table-layout: fixed;
|
|
|
|
| 57 |
# A lógica para usar o arquivo seria adicionada aqui.
|
| 58 |
|
| 59 |
# 3. Configuração do Cliente Groq
|
| 60 |
+
# Assume que a chave GROQ_API_KEY está configurada no ambiente ou no .streamlit/secrets.toml
|
| 61 |
client = Groq()
|
| 62 |
|
| 63 |
# 4. Inicialização do Histórico da Conversa
|
|
|
|
| 72 |
st.session_state.messages = [{"role": "system", "content": system_prompt}]
|
| 73 |
|
| 74 |
# 5. Exibição das Mensagens Anteriores
|
| 75 |
+
# O 'role' é mantido para fins de estrutura, mas o avatar está oculto pelo CSS
|
| 76 |
for message in st.session_state.messages:
|
| 77 |
if message["role"] != "system":
|
| 78 |
with st.chat_message(message["role"]):
|
|
|
|
| 80 |
|
| 81 |
# 6. Lógica de Interação do Chat
|
| 82 |
if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
| 83 |
+
# 6a. Adiciona a mensagem do usuário e exibe
|
| 84 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 85 |
with st.chat_message("user"):
|
| 86 |
st.markdown(prompt)
|
| 87 |
|
| 88 |
+
# 6b. Obtém a resposta do assistente (Streaming)
|
| 89 |
with st.chat_message("assistant"):
|
| 90 |
try:
|
| 91 |
def stream_groq_response():
|
| 92 |
+
# Chama a API Groq com o histórico completo
|
|
|
|
| 93 |
stream = client.chat.completions.create(
|
| 94 |
model="groq/compound",
|
| 95 |
messages=[
|
|
|
|
| 108 |
full_response = ""
|
| 109 |
for token in stream_groq_response():
|
| 110 |
full_response += token
|
| 111 |
+
# Exibe a resposta com um cursor de digitação '▌'
|
| 112 |
placeholder.markdown(full_response + "▌")
|
| 113 |
+
# Atraso mínimo para o efeito de digitação (opcional)
|
| 114 |
+
time.sleep(0.005)
|
| 115 |
placeholder.markdown(full_response)
|
| 116 |
|
| 117 |
+
# 6c. Adiciona a resposta completa ao histórico
|
| 118 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
+
st.error(f"Ocorreu um erro ao contatar a API da Groq: {e}")
|
| 122 |
+
|
| 123 |
+
# --- END OF FILE app (23).py ---
|