alphayomega
commited on
Commit
•
fd9c480
1
Parent(s):
1d325c0
Update app.py
Browse files
app.py
CHANGED
@@ -4,34 +4,35 @@ import streamlit as st
|
|
4 |
from typing import Generator
|
5 |
from groq import Groq
|
6 |
|
|
|
7 |
_ = load_dotenv(find_dotenv())
|
8 |
-
st.set_page_config(page_icon="📃", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...")
|
9 |
|
|
|
|
|
10 |
|
11 |
def icon(emoji: str):
|
12 |
-
"""
|
13 |
st.write(
|
14 |
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
15 |
unsafe_allow_html=True,
|
16 |
)
|
17 |
|
18 |
-
|
19 |
-
# icon("⚡️")
|
20 |
-
|
21 |
st.subheader("Groq Chat with LLaMA3.1 App", divider="rainbow", anchor=False)
|
22 |
|
|
|
23 |
client = Groq(
|
24 |
api_key=os.environ['GROQ_API_KEY'],
|
25 |
)
|
26 |
|
27 |
-
#
|
28 |
if "messages" not in st.session_state:
|
29 |
st.session_state.messages = []
|
30 |
|
31 |
if "selected_model" not in st.session_state:
|
32 |
st.session_state.selected_model = None
|
33 |
|
34 |
-
#
|
35 |
models = {
|
36 |
"llama-3.1-70b-versatile": {"name": "LLaMA3.1-70b", "tokens": 4096, "developer": "Meta"},
|
37 |
"llama-3.1-8b-instant": {"name": "LLaMA3.1-8b", "tokens": 4096, "developer": "Meta"},
|
@@ -46,16 +47,15 @@ models = {
|
|
46 |
},
|
47 |
}
|
48 |
|
49 |
-
#
|
50 |
-
col1, col2 = st.columns([1, 3]) #
|
51 |
-
|
52 |
|
53 |
with col1:
|
54 |
model_option = st.selectbox(
|
55 |
"Choose a model:",
|
56 |
options=list(models.keys()),
|
57 |
format_func=lambda x: models[x]["name"],
|
58 |
-
index=0, #
|
59 |
)
|
60 |
max_tokens_range = models[model_option]["tokens"]
|
61 |
max_tokens = st.slider(
|
@@ -67,36 +67,37 @@ with col1:
|
|
67 |
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
68 |
)
|
69 |
|
70 |
-
#
|
71 |
if st.session_state.selected_model != model_option:
|
72 |
st.session_state.messages = []
|
73 |
st.session_state.selected_model = model_option
|
74 |
|
75 |
-
#
|
76 |
if st.button("Clear Chat"):
|
77 |
st.session_state.messages = []
|
78 |
-
|
79 |
-
#
|
80 |
for message in st.session_state.messages:
|
81 |
avatar = "🔋" if message["role"] == "assistant" else "🧑💻"
|
82 |
with st.chat_message(message["role"], avatar=avatar):
|
83 |
st.markdown(message["content"])
|
84 |
|
85 |
-
|
86 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
87 |
-
"""
|
88 |
for chunk in chat_completion:
|
89 |
if chunk.choices[0].delta.content:
|
90 |
yield chunk.choices[0].delta.content
|
91 |
|
92 |
-
|
93 |
-
if prompt := st.chat_input(
|
|
|
|
|
94 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
95 |
|
96 |
with st.chat_message("user", avatar="🧑💻"):
|
97 |
st.markdown(prompt)
|
98 |
|
99 |
-
#
|
100 |
try:
|
101 |
chat_completion = client.chat.completions.create(
|
102 |
model=model_option,
|
@@ -108,21 +109,21 @@ if prompt := st.chat_input("# Extract the benefits of the product, not the featu
|
|
108 |
stream=True,
|
109 |
)
|
110 |
|
111 |
-
#
|
112 |
with st.chat_message("assistant", avatar="🔋"):
|
113 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
114 |
full_response = st.write_stream(chat_responses_generator)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
except Exception as e:
|
116 |
st.error(e, icon="❌")
|
117 |
-
|
118 |
-
# Append the full response to session_state.messages
|
119 |
-
if isinstance(full_response, str):
|
120 |
-
st.session_state.messages.append(
|
121 |
-
{"role": "assistant", "content": full_response}
|
122 |
-
)
|
123 |
-
else:
|
124 |
-
# Handle the case where full_response is not a string
|
125 |
-
combined_response = "\n".join(str(item) for item in full_response)
|
126 |
-
st.session_state.messages.append(
|
127 |
-
{"role": "assistant", "content": combined_response}
|
128 |
-
)
|
|
|
4 |
from typing import Generator
|
5 |
from groq import Groq
|
6 |
|
7 |
+
# Cargar variables de entorno
|
8 |
_ = load_dotenv(find_dotenv())
|
|
|
9 |
|
10 |
+
# Configurar la página de Streamlit
|
11 |
+
st.set_page_config(page_icon="📃", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...")
|
12 |
|
13 |
def icon(emoji: str):
|
14 |
+
"""Muestra un emoji como ícono de página estilo Notion."""
|
15 |
st.write(
|
16 |
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
17 |
unsafe_allow_html=True,
|
18 |
)
|
19 |
|
20 |
+
# Encabezado de la aplicación
|
|
|
|
|
21 |
st.subheader("Groq Chat with LLaMA3.1 App", divider="rainbow", anchor=False)
|
22 |
|
23 |
+
# Inicializar cliente Groq
|
24 |
client = Groq(
|
25 |
api_key=os.environ['GROQ_API_KEY'],
|
26 |
)
|
27 |
|
28 |
+
# Inicializar historial de chat y modelo seleccionado
|
29 |
if "messages" not in st.session_state:
|
30 |
st.session_state.messages = []
|
31 |
|
32 |
if "selected_model" not in st.session_state:
|
33 |
st.session_state.selected_model = None
|
34 |
|
35 |
+
# Detalles de los modelos
|
36 |
models = {
|
37 |
"llama-3.1-70b-versatile": {"name": "LLaMA3.1-70b", "tokens": 4096, "developer": "Meta"},
|
38 |
"llama-3.1-8b-instant": {"name": "LLaMA3.1-8b", "tokens": 4096, "developer": "Meta"},
|
|
|
47 |
},
|
48 |
}
|
49 |
|
50 |
+
# Diseño para la selección de modelo y slider de tokens
|
51 |
+
col1, col2 = st.columns([1, 3]) # Ajusta la proporción para hacer la primera columna más pequeña
|
|
|
52 |
|
53 |
with col1:
|
54 |
model_option = st.selectbox(
|
55 |
"Choose a model:",
|
56 |
options=list(models.keys()),
|
57 |
format_func=lambda x: models[x]["name"],
|
58 |
+
index=0, # Predeterminado al primer modelo en la lista
|
59 |
)
|
60 |
max_tokens_range = models[model_option]["tokens"]
|
61 |
max_tokens = st.slider(
|
|
|
67 |
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
68 |
)
|
69 |
|
70 |
+
# Detectar cambio de modelo y limpiar historial de chat si el modelo ha cambiado
|
71 |
if st.session_state.selected_model != model_option:
|
72 |
st.session_state.messages = []
|
73 |
st.session_state.selected_model = model_option
|
74 |
|
75 |
+
# Añadir un botón para "Limpiar Chat"
|
76 |
if st.button("Clear Chat"):
|
77 |
st.session_state.messages = []
|
78 |
+
|
79 |
+
# Mostrar mensajes de chat del historial en la aplicación
|
80 |
for message in st.session_state.messages:
|
81 |
avatar = "🔋" if message["role"] == "assistant" else "🧑💻"
|
82 |
with st.chat_message(message["role"], avatar=avatar):
|
83 |
st.markdown(message["content"])
|
84 |
|
|
|
85 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
86 |
+
"""Generar contenido de respuesta del chat a partir de la respuesta de la API de Groq."""
|
87 |
for chunk in chat_completion:
|
88 |
if chunk.choices[0].delta.content:
|
89 |
yield chunk.choices[0].delta.content
|
90 |
|
91 |
+
# Manejar la entrada del chat del usuario
|
92 |
+
if prompt := st.chat_input(
|
93 |
+
"# Extract the benefits of the product, not the features. # You should be as brief as possible. # Omit the price, if any. # Do not mention the name of the product. # Use 3 paragraphs. # Try to synthesize or summarize. # Focus only on the benefits. # Highlight how this product helps the customer. # Always respond in Spanish. # The text you create will be used in an e-commerce product sales page through the Internet, so it must be persuasive, attractive, and above all very short and summarized. # Remember to keep the text short, summarized, synthesized in three paragraphs. # Surprise me with your best ideas! # Always answers in AMERICAN SPANISH. Stop after finish the first content genreated."
|
94 |
+
):
|
95 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
96 |
|
97 |
with st.chat_message("user", avatar="🧑💻"):
|
98 |
st.markdown(prompt)
|
99 |
|
100 |
+
# Obtener respuesta de la API de Groq
|
101 |
try:
|
102 |
chat_completion = client.chat.completions.create(
|
103 |
model=model_option,
|
|
|
109 |
stream=True,
|
110 |
)
|
111 |
|
112 |
+
# Usar la función generadora con st.write_stream
|
113 |
with st.chat_message("assistant", avatar="🔋"):
|
114 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
115 |
full_response = st.write_stream(chat_responses_generator)
|
116 |
+
|
117 |
+
# Añadir la respuesta completa al historial de mensajes
|
118 |
+
if isinstance(full_response, str):
|
119 |
+
st.session_state.messages.append(
|
120 |
+
{"role": "assistant", "content": full_response}
|
121 |
+
)
|
122 |
+
else:
|
123 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
124 |
+
st.session_state.messages.append(
|
125 |
+
{"role": "assistant", "content": combined_response}
|
126 |
+
)
|
127 |
+
|
128 |
except Exception as e:
|
129 |
st.error(e, icon="❌")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|