Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -78,141 +78,77 @@ st.markdown(styles["button"], unsafe_allow_html=True)
|
|
78 |
# Crear columnas
|
79 |
col1, col2 = st.columns([1, 2])
|
80 |
|
81 |
-
# Modificar la parte de la interfaz de usuario para usar session_state
|
82 |
-
|
83 |
-
# Inicializar valores en session_state si no existen
|
84 |
-
if 'ad_target_audience' not in st.session_state:
|
85 |
-
st.session_state.ad_target_audience = ""
|
86 |
-
if 'ad_product' not in st.session_state:
|
87 |
-
st.session_state.ad_product = ""
|
88 |
-
if 'input_prompt' not in st.session_state:
|
89 |
-
st.session_state.input_prompt = ""
|
90 |
-
if 'ad_formula_key' not in st.session_state:
|
91 |
-
st.session_state.ad_formula_key = list(ads_formulas.keys())[0]
|
92 |
-
if 'emotional_angle_key' not in st.session_state:
|
93 |
-
st.session_state.emotional_angle_key = list(emotional_angles.keys())[0]
|
94 |
-
if 'ad_persona_key' not in st.session_state:
|
95 |
-
st.session_state.ad_persona_key = list(copywriter_personas.keys())[0]
|
96 |
-
if 'ad_objective_key' not in st.session_state:
|
97 |
-
st.session_state.ad_objective_key = list(ad_objectives.keys())[0]
|
98 |
-
if 'ad_temperature' not in st.session_state:
|
99 |
-
st.session_state.ad_temperature = 1.0
|
100 |
-
|
101 |
-
# Funciones para actualizar el estado
|
102 |
-
def update_target_audience():
|
103 |
-
st.session_state.ad_target_audience = st.session_state.target_audience_input
|
104 |
-
|
105 |
-
def update_product():
|
106 |
-
st.session_state.ad_product = st.session_state.product_input
|
107 |
-
|
108 |
-
def update_prompt():
|
109 |
-
st.session_state.input_prompt = st.session_state.prompt_input
|
110 |
-
|
111 |
# Columnas de entrada
|
112 |
with col1:
|
113 |
-
st.text_input(
|
114 |
-
|
115 |
-
|
116 |
-
key="target_audience_input",
|
117 |
-
value=st.session_state.ad_target_audience,
|
118 |
-
on_change=update_target_audience
|
119 |
-
)
|
120 |
-
|
121 |
-
st.text_input(
|
122 |
-
"¿Qué producto tienes en mente?",
|
123 |
-
placeholder="Ejemplo: Curso de gestión del tiempo",
|
124 |
-
key="product_input",
|
125 |
-
value=st.session_state.ad_product,
|
126 |
-
on_change=update_product
|
127 |
-
)
|
128 |
-
|
129 |
-
st.text_area(
|
130 |
-
"Escribe de qué quieres que trate la historia:",
|
131 |
-
placeholder="Escribe aquí tu idea...",
|
132 |
-
key="prompt_input",
|
133 |
-
value=st.session_state.input_prompt,
|
134 |
-
on_change=update_prompt
|
135 |
-
)
|
136 |
-
|
137 |
-
# Modify the selectbox sections to avoid directly updating session_state after widget creation
|
138 |
|
139 |
with st.expander("Opciones avanzadas"):
|
140 |
# Selector de fórmula de anuncio
|
141 |
ad_formula_key = st.selectbox(
|
142 |
"Fórmula de anuncio",
|
143 |
options=list(ads_formulas.keys()),
|
144 |
-
label_visibility="visible"
|
145 |
-
key="ad_formula_key",
|
146 |
-
index=list(ads_formulas.keys()).index(st.session_state.ad_formula_key)
|
147 |
)
|
148 |
-
# Remove this line: st.session_state.ad_formula_key = ad_formula_key
|
149 |
ad_formula = ads_formulas[ad_formula_key]
|
150 |
|
151 |
# Selector de ángulo emocional
|
152 |
emotional_angle_key = st.selectbox(
|
153 |
"Ángulo emocional",
|
154 |
options=list(emotional_angles.keys()),
|
155 |
-
label_visibility="visible"
|
156 |
-
key="emotional_angle_key",
|
157 |
-
index=list(emotional_angles.keys()).index(st.session_state.emotional_angle_key)
|
158 |
)
|
159 |
-
# Remove this line: st.session_state.emotional_angle_key = emotional_angle_key
|
160 |
emotional_angle = emotional_angles[emotional_angle_key]
|
161 |
|
162 |
# Selector de personalidad de copywriter
|
163 |
ad_persona_key = st.selectbox(
|
164 |
"Estilo de copywriter",
|
165 |
options=list(copywriter_personas.keys()),
|
|
|
166 |
format_func=lambda x: x.replace("_", " "),
|
167 |
-
label_visibility="visible"
|
168 |
-
key="ad_persona_key",
|
169 |
-
index=list(copywriter_personas.keys()).index(st.session_state.ad_persona_key)
|
170 |
)
|
171 |
-
# Remove this line: st.session_state.ad_persona_key = ad_persona_key
|
172 |
ad_persona = copywriter_personas[ad_persona_key]
|
173 |
|
174 |
# Selector de objetivo de anuncio
|
175 |
ad_objective_key = st.selectbox(
|
176 |
"Objetivo de la campaña",
|
177 |
options=list(ad_objectives.keys()),
|
178 |
-
label_visibility="visible"
|
179 |
-
key="ad_objective_key",
|
180 |
-
index=list(ad_objectives.keys()).index(st.session_state.ad_objective_key)
|
181 |
)
|
182 |
-
# Remove this line: st.session_state.ad_objective_key = ad_objective_key
|
183 |
selected_objective = ad_objectives[ad_objective_key] if ad_objective_key != "ninguno" else None
|
184 |
-
|
185 |
-
#
|
|
|
186 |
ad_temperature = st.slider(
|
187 |
"Nivel de creatividad",
|
188 |
min_value=0.0,
|
189 |
max_value=2.0,
|
190 |
-
value=
|
191 |
step=0.1,
|
192 |
-
label_visibility="visible"
|
193 |
-
key="ad_temperature_slider"
|
194 |
)
|
195 |
-
# Remove this line: st.session_state.ad_temperature = ad_temperature
|
196 |
|
197 |
-
# Usar los valores de session_state para el botón de generación
|
198 |
submit_ad = st.button("GENERAR ANUNCIO")
|
199 |
|
200 |
# Mostrar el anuncio generado
|
201 |
if submit_ad:
|
202 |
-
if
|
203 |
-
#
|
204 |
with col2:
|
205 |
with st.spinner('Generando anuncio...'):
|
206 |
generated_ad = generate_fb_ad(
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
ad_formula,
|
211 |
emotional_angle,
|
212 |
ad_persona,
|
213 |
-
|
214 |
selected_objective
|
215 |
)
|
|
|
216 |
if not isinstance(generated_ad, str):
|
217 |
st.error("Error al generar el anuncio")
|
218 |
else:
|
|
|
78 |
# Crear columnas
|
79 |
col1, col2 = st.columns([1, 2])
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# Columnas de entrada
|
82 |
with col1:
|
83 |
+
ad_target_audience = st.text_input("¿Quién es tu público objetivo?", placeholder="Ejemplo: Madres trabajadoras de 30-45 años")
|
84 |
+
ad_product = st.text_input("¿Qué producto tienes en mente?", placeholder="Ejemplo: Curso de gestión del tiempo")
|
85 |
+
input_prompt = st.text_area("Escribe de qué quieres que trate la historia:", placeholder="Escribe aquí tu idea...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
with st.expander("Opciones avanzadas"):
|
88 |
# Selector de fórmula de anuncio
|
89 |
ad_formula_key = st.selectbox(
|
90 |
"Fórmula de anuncio",
|
91 |
options=list(ads_formulas.keys()),
|
92 |
+
label_visibility="visible"
|
|
|
|
|
93 |
)
|
|
|
94 |
ad_formula = ads_formulas[ad_formula_key]
|
95 |
|
96 |
# Selector de ángulo emocional
|
97 |
emotional_angle_key = st.selectbox(
|
98 |
"Ángulo emocional",
|
99 |
options=list(emotional_angles.keys()),
|
100 |
+
label_visibility="visible"
|
|
|
|
|
101 |
)
|
|
|
102 |
emotional_angle = emotional_angles[emotional_angle_key]
|
103 |
|
104 |
# Selector de personalidad de copywriter
|
105 |
ad_persona_key = st.selectbox(
|
106 |
"Estilo de copywriter",
|
107 |
options=list(copywriter_personas.keys()),
|
108 |
+
index=0,
|
109 |
format_func=lambda x: x.replace("_", " "),
|
110 |
+
label_visibility="visible"
|
|
|
|
|
111 |
)
|
|
|
112 |
ad_persona = copywriter_personas[ad_persona_key]
|
113 |
|
114 |
# Selector de objetivo de anuncio
|
115 |
ad_objective_key = st.selectbox(
|
116 |
"Objetivo de la campaña",
|
117 |
options=list(ad_objectives.keys()),
|
118 |
+
label_visibility="visible"
|
|
|
|
|
119 |
)
|
|
|
120 |
selected_objective = ad_objectives[ad_objective_key] if ad_objective_key != "ninguno" else None
|
121 |
+
|
122 |
+
# Removed language selector
|
123 |
+
|
124 |
ad_temperature = st.slider(
|
125 |
"Nivel de creatividad",
|
126 |
min_value=0.0,
|
127 |
max_value=2.0,
|
128 |
+
value=1.0,
|
129 |
step=0.1,
|
130 |
+
label_visibility="visible"
|
|
|
131 |
)
|
|
|
132 |
|
|
|
133 |
submit_ad = st.button("GENERAR ANUNCIO")
|
134 |
|
135 |
# Mostrar el anuncio generado
|
136 |
if submit_ad:
|
137 |
+
if ad_target_audience and ad_product:
|
138 |
+
# Mostrar el spinner en la segunda columna
|
139 |
with col2:
|
140 |
with st.spinner('Generando anuncio...'):
|
141 |
generated_ad = generate_fb_ad(
|
142 |
+
ad_target_audience,
|
143 |
+
ad_product,
|
144 |
+
ad_temperature,
|
145 |
ad_formula,
|
146 |
emotional_angle,
|
147 |
ad_persona,
|
148 |
+
input_prompt, # Pass the new story prompt
|
149 |
selected_objective
|
150 |
)
|
151 |
+
|
152 |
if not isinstance(generated_ad, str):
|
153 |
st.error("Error al generar el anuncio")
|
154 |
else:
|