Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,36 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
|
|
|
| 3 |
|
| 4 |
-
# 1. Título da Página e Configuração
|
| 5 |
-
st.set_page_config(page_title="Iza", layout="
|
| 6 |
-
st.title("Iza - by Carlex 🚀")
|
| 7 |
-
#st.caption("Um chatbot simples usando a API da Groq e o modelo Compound.")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
client = Groq()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
# O `st.session_state` é a forma como o Streamlit mantém a memória entre as interações.
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
system_prompt = (
|
| 17 |
"Você é um assistente de pesquisa avançado chamado Iza. "
|
|
@@ -20,27 +39,21 @@ if "messages" not in st.session_state:
|
|
| 20 |
)
|
| 21 |
st.session_state.messages = [{"role": "system", "content": system_prompt}]
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
# Percorre o histórico salvo e exibe cada mensagem na tela.
|
| 25 |
for message in st.session_state.messages:
|
| 26 |
-
# Não exibe a mensagem de 'system' na interface do chat.
|
| 27 |
if message["role"] != "system":
|
| 28 |
with st.chat_message(message["role"]):
|
| 29 |
st.markdown(message["content"])
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
# `st.chat_input` cria a caixa de texto para o usuário digitar.
|
| 33 |
if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
| 34 |
-
# Adiciona a mensagem do usuário ao histórico e exibe na tela.
|
| 35 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 36 |
with st.chat_message("user"):
|
| 37 |
st.markdown(prompt)
|
| 38 |
|
| 39 |
-
# Prepara para exibir a resposta do assistente.
|
| 40 |
with st.chat_message("assistant"):
|
| 41 |
try:
|
| 42 |
-
#
|
| 43 |
-
# Ela irá consumir a função geradora e exibir os tokens na tela em tempo real.
|
| 44 |
def stream_groq_response():
|
| 45 |
stream = client.chat.completions.create(
|
| 46 |
model="groq/compound",
|
|
@@ -55,11 +68,19 @@ if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
|
| 55 |
)
|
| 56 |
for chunk in stream:
|
| 57 |
yield chunk.choices[0].delta.content or ""
|
| 58 |
-
|
| 59 |
-
# Executa o streaming e armazena a resposta completa.
|
| 60 |
-
full_response = st.write_stream(stream_groq_response)
|
| 61 |
|
| 62 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 64 |
|
| 65 |
except Exception as e:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
+
import time
|
| 4 |
|
| 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 para garantir a quebra de linha do texto em qualquer tela
|
| 9 |
+
st.markdown("""
|
| 10 |
+
<style>
|
| 11 |
+
.main .block-container {
|
| 12 |
+
word-wrap: break-word;
|
| 13 |
+
overflow-wrap: break-word;
|
| 14 |
+
}
|
| 15 |
+
</style>
|
| 16 |
+
""", unsafe_allow_html=True)
|
| 17 |
+
|
| 18 |
+
st.title("Iza - Assistente com Groq 🚀")
|
| 19 |
+
st.caption("Um chatbot com memória, upload de arquivos e controle de velocidade.")
|
| 20 |
+
|
| 21 |
+
# 2. Barra Lateral para Opções e Upload de Arquivos
|
| 22 |
+
with st.sidebar:
|
| 23 |
+
st.header("Opções")
|
| 24 |
+
uploaded_file = st.file_uploader("Anexe um arquivo (opcional)", type=None)
|
| 25 |
+
if uploaded_file:
|
| 26 |
+
st.success(f"Arquivo '{uploaded_file.name}' carregado!")
|
| 27 |
+
# NOTA: A lógica para ler e usar o arquivo precisaria ser adicionada aqui.
|
| 28 |
+
# Por enquanto, ele apenas confirma o upload.
|
| 29 |
+
|
| 30 |
+
# 3. Configuração do Cliente Groq
|
| 31 |
client = Groq()
|
| 32 |
|
| 33 |
+
# 4. Inicialização do Histórico da Conversa
|
|
|
|
| 34 |
if "messages" not in st.session_state:
|
| 35 |
system_prompt = (
|
| 36 |
"Você é um assistente de pesquisa avançado chamado Iza. "
|
|
|
|
| 39 |
)
|
| 40 |
st.session_state.messages = [{"role": "system", "content": system_prompt}]
|
| 41 |
|
| 42 |
+
# 5. Exibição das Mensagens Anteriores
|
|
|
|
| 43 |
for message in st.session_state.messages:
|
|
|
|
| 44 |
if message["role"] != "system":
|
| 45 |
with st.chat_message(message["role"]):
|
| 46 |
st.markdown(message["content"])
|
| 47 |
|
| 48 |
+
# 6. Lógica de Interação do Chat
|
|
|
|
| 49 |
if prompt := st.chat_input("Pergunte algo sobre um site..."):
|
|
|
|
| 50 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 51 |
with st.chat_message("user"):
|
| 52 |
st.markdown(prompt)
|
| 53 |
|
|
|
|
| 54 |
with st.chat_message("assistant"):
|
| 55 |
try:
|
| 56 |
+
# Função geradora que obtém o stream da API
|
|
|
|
| 57 |
def stream_groq_response():
|
| 58 |
stream = client.chat.completions.create(
|
| 59 |
model="groq/compound",
|
|
|
|
| 68 |
)
|
| 69 |
for chunk in stream:
|
| 70 |
yield chunk.choices[0].delta.content or ""
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Loop manual para controlar a velocidade da "digitação"
|
| 73 |
+
placeholder = st.empty()
|
| 74 |
+
full_response = ""
|
| 75 |
+
for token in stream_groq_response():
|
| 76 |
+
full_response += token
|
| 77 |
+
placeholder.markdown(full_response + "▌")
|
| 78 |
+
# AJUSTE A VELOCIDADE AQUI (valores menores = mais rápido, maiores = mais lento)
|
| 79 |
+
time.sleep(0.005)
|
| 80 |
+
# Exibe a resposta final sem o cursor
|
| 81 |
+
placeholder.markdown(full_response)
|
| 82 |
+
|
| 83 |
+
# Adiciona a resposta completa do assistente ao histórico
|
| 84 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 85 |
|
| 86 |
except Exception as e:
|