Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
| 16 |
# Fórmulas con ejemplos y explicaciones
|
| 17 |
# headline_formulas dictionary has been moved to formulas/headline_formulas.py
|
| 18 |
|
| 19 |
-
def generate_headlines(number_of_headlines, target_audience, product, temperature, selected_formula, selected_angle
|
| 20 |
# Crear la configuración del modelo
|
| 21 |
generation_config = {
|
| 22 |
"temperature": temperature,
|
|
@@ -71,39 +71,6 @@ IMPORTANT:
|
|
| 71 |
|
| 72 |
# Iniciar el prompt con las instrucciones del sistema
|
| 73 |
headlines_instruction = f"{system_prompt}\n\n"
|
| 74 |
-
|
| 75 |
-
# Añadir contenido del archivo si existe
|
| 76 |
-
if file_content:
|
| 77 |
-
headlines_instruction += f"""
|
| 78 |
-
REFERENCE CONTENT:
|
| 79 |
-
Carefully analyze the following content as a reference for generating headlines:
|
| 80 |
-
{file_content[:3000]}
|
| 81 |
-
|
| 82 |
-
ANALYSIS INSTRUCTIONS:
|
| 83 |
-
1. Extract key information about the product or service mentioned
|
| 84 |
-
2. Identify the tone, style, and language used
|
| 85 |
-
3. Detect any data about the target audience or customer avatar
|
| 86 |
-
4. Look for benefits, features, or pain points mentioned
|
| 87 |
-
5. Use relevant terms, phrases, or concepts from the content
|
| 88 |
-
6. Maintain consistency with the brand identity or main message
|
| 89 |
-
7. Adapt the headlines to resonate with the provided content
|
| 90 |
-
|
| 91 |
-
IMPORTANT COMBINATIONS:
|
| 92 |
-
"""
|
| 93 |
-
# Updated conditions for specific input combinations
|
| 94 |
-
if product and not target_audience:
|
| 95 |
-
headlines_instruction += f"""- FILE + PRODUCT: You have a reference document and product ({product}). Create headlines that highlight this specific product's benefits and features using insights from the document. Extract audience information from the document to better target the headlines.
|
| 96 |
-
"""
|
| 97 |
-
elif target_audience and not product:
|
| 98 |
-
headlines_instruction += f"""- FILE + TARGET AUDIENCE: You have a reference document and target audience ({target_audience}). Create headlines tailored to this specific audience using language and concepts from the document. Identify products or services from the document that would appeal to this audience.
|
| 99 |
-
"""
|
| 100 |
-
elif product and target_audience:
|
| 101 |
-
headlines_instruction += f"""- PRODUCT + TARGET AUDIENCE: You have both product ({product}) and target audience ({target_audience}). Create headlines that connect this specific product with this specific audience, using insights from the document to strengthen the connection.
|
| 102 |
-
"""
|
| 103 |
-
|
| 104 |
-
headlines_instruction += """
|
| 105 |
-
IMPORTANT: Naturally integrate the elements found in the content with the selected formula and angle.
|
| 106 |
-
"""
|
| 107 |
|
| 108 |
# Añadir instrucciones de ángulo solo si no es "NINGUNO"
|
| 109 |
if selected_angle != "NINGUNO":
|
|
@@ -199,6 +166,7 @@ Crea {number_of_headlines} titulares que sigan fielmente el estilo y estructura
|
|
| 199 |
)
|
| 200 |
response = chat_session.send_message("Genera los titulares siguiendo exactamente el estilo de los ejemplos mostrados, inspirándote en la imagen proporcionada.")
|
| 201 |
else:
|
|
|
|
| 202 |
chat_session = model.start_chat(
|
| 203 |
history=[
|
| 204 |
{
|
|
@@ -208,8 +176,8 @@ Crea {number_of_headlines} titulares que sigan fielmente el estilo y estructura
|
|
| 208 |
]
|
| 209 |
)
|
| 210 |
response = chat_session.send_message("Genera los titulares siguiendo exactamente el estilo de los ejemplos mostrados.")
|
| 211 |
-
|
| 212 |
-
|
| 213 |
|
| 214 |
# Configurar la interfaz de usuario con Streamlit
|
| 215 |
st.set_page_config(page_title="Enchanted Hooks", layout="wide")
|
|
@@ -257,65 +225,6 @@ with col1:
|
|
| 257 |
"Selecciona el ángulo para tus titulares",
|
| 258 |
options=angle_keys
|
| 259 |
)
|
| 260 |
-
|
| 261 |
-
# Añadir cargador de archivos dentro del acordeón
|
| 262 |
-
uploaded_file = st.file_uploader("📄 Archivo o imagen de referencia",
|
| 263 |
-
type=['txt', 'pdf', 'docx', 'jpg', 'jpeg', 'png'])
|
| 264 |
-
|
| 265 |
-
file_content = ""
|
| 266 |
-
is_image = False
|
| 267 |
-
image_parts = None
|
| 268 |
-
|
| 269 |
-
if uploaded_file is not None:
|
| 270 |
-
file_type = uploaded_file.name.split('.')[-1].lower()
|
| 271 |
-
|
| 272 |
-
# Manejar archivos de texto
|
| 273 |
-
if file_type in ['txt', 'pdf', 'docx']:
|
| 274 |
-
if file_type == 'txt':
|
| 275 |
-
try:
|
| 276 |
-
file_content = uploaded_file.read().decode('utf-8')
|
| 277 |
-
st.success(f"Archivo TXT cargado correctamente: {uploaded_file.name}")
|
| 278 |
-
except Exception as e:
|
| 279 |
-
st.error(f"Error al leer el archivo TXT: {str(e)}")
|
| 280 |
-
file_content = ""
|
| 281 |
-
|
| 282 |
-
elif file_type == 'pdf':
|
| 283 |
-
try:
|
| 284 |
-
import PyPDF2
|
| 285 |
-
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 286 |
-
file_content = ""
|
| 287 |
-
for page in pdf_reader.pages:
|
| 288 |
-
file_content += page.extract_text() + "\n"
|
| 289 |
-
st.success(f"Archivo PDF cargado correctamente: {uploaded_file.name}")
|
| 290 |
-
except Exception as e:
|
| 291 |
-
st.error(f"Error al leer el archivo PDF: {str(e)}")
|
| 292 |
-
file_content = ""
|
| 293 |
-
|
| 294 |
-
elif file_type == 'docx':
|
| 295 |
-
try:
|
| 296 |
-
import docx
|
| 297 |
-
doc = docx.Document(uploaded_file)
|
| 298 |
-
file_content = "\n".join([para.text for para in doc.paragraphs])
|
| 299 |
-
st.success(f"Archivo DOCX cargado correctamente: {uploaded_file.name}")
|
| 300 |
-
except Exception as e:
|
| 301 |
-
st.error(f"Error al leer el archivo DOCX: {str(e)}")
|
| 302 |
-
file_content = ""
|
| 303 |
-
|
| 304 |
-
# Manejar archivos de imagen
|
| 305 |
-
elif file_type in ['jpg', 'jpeg', 'png']:
|
| 306 |
-
try:
|
| 307 |
-
from PIL import Image
|
| 308 |
-
image = Image.open(uploaded_file)
|
| 309 |
-
image_bytes = uploaded_file.getvalue()
|
| 310 |
-
image_parts = {
|
| 311 |
-
"mime_type": uploaded_file.type,
|
| 312 |
-
"data": image_bytes
|
| 313 |
-
}
|
| 314 |
-
is_image = True
|
| 315 |
-
st.image(image, caption="Imagen cargada", use_column_width=True)
|
| 316 |
-
except Exception as e:
|
| 317 |
-
st.error(f"Error al procesar la imagen: {str(e)}")
|
| 318 |
-
is_image = False
|
| 319 |
|
| 320 |
selected_formula = headline_formulas[selected_formula_key]
|
| 321 |
|
|
@@ -324,20 +233,12 @@ with col1:
|
|
| 324 |
|
| 325 |
# Mostrar los titulares generados
|
| 326 |
if submit:
|
| 327 |
-
# Check if we have
|
| 328 |
-
has_file = 'file_content' in locals() and file_content.strip() != ""
|
| 329 |
has_product = product.strip() != ""
|
| 330 |
has_audience = target_audience.strip() != ""
|
| 331 |
|
| 332 |
-
# Valid
|
| 333 |
-
|
| 334 |
-
# 2. File + Audience (no product needed)
|
| 335 |
-
# 3. Product + Audience (traditional way)
|
| 336 |
-
valid_inputs = (
|
| 337 |
-
(has_file and has_product) or
|
| 338 |
-
(has_file and has_audience) or
|
| 339 |
-
(has_product and has_audience)
|
| 340 |
-
)
|
| 341 |
|
| 342 |
if valid_inputs and selected_formula:
|
| 343 |
try:
|
|
@@ -347,10 +248,7 @@ if submit:
|
|
| 347 |
product,
|
| 348 |
temperature,
|
| 349 |
selected_formula,
|
| 350 |
-
selected_angle
|
| 351 |
-
file_content if 'file_content' in locals() else "",
|
| 352 |
-
image_parts if 'image_parts' in locals() else None,
|
| 353 |
-
is_image if 'is_image' in locals() else False
|
| 354 |
)
|
| 355 |
col2.markdown(f"""
|
| 356 |
<div class="results-container">
|
|
@@ -364,4 +262,4 @@ if submit:
|
|
| 364 |
if not selected_formula:
|
| 365 |
col2.error("Por favor, selecciona una fórmula.")
|
| 366 |
else:
|
| 367 |
-
col2.error("Por favor, proporciona
|
|
|
|
| 16 |
# Fórmulas con ejemplos y explicaciones
|
| 17 |
# headline_formulas dictionary has been moved to formulas/headline_formulas.py
|
| 18 |
|
| 19 |
+
def generate_headlines(number_of_headlines, target_audience, product, temperature, selected_formula, selected_angle):
|
| 20 |
# Crear la configuración del modelo
|
| 21 |
generation_config = {
|
| 22 |
"temperature": temperature,
|
|
|
|
| 71 |
|
| 72 |
# Iniciar el prompt con las instrucciones del sistema
|
| 73 |
headlines_instruction = f"{system_prompt}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Añadir instrucciones de ángulo solo si no es "NINGUNO"
|
| 76 |
if selected_angle != "NINGUNO":
|
|
|
|
| 166 |
)
|
| 167 |
response = chat_session.send_message("Genera los titulares siguiendo exactamente el estilo de los ejemplos mostrados, inspirándote en la imagen proporcionada.")
|
| 168 |
else:
|
| 169 |
+
# Enviar el mensaje al modelo
|
| 170 |
chat_session = model.start_chat(
|
| 171 |
history=[
|
| 172 |
{
|
|
|
|
| 176 |
]
|
| 177 |
)
|
| 178 |
response = chat_session.send_message("Genera los titulares siguiendo exactamente el estilo de los ejemplos mostrados.")
|
| 179 |
+
|
| 180 |
+
return response.text
|
| 181 |
|
| 182 |
# Configurar la interfaz de usuario con Streamlit
|
| 183 |
st.set_page_config(page_title="Enchanted Hooks", layout="wide")
|
|
|
|
| 225 |
"Selecciona el ángulo para tus titulares",
|
| 226 |
options=angle_keys
|
| 227 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
selected_formula = headline_formulas[selected_formula_key]
|
| 230 |
|
|
|
|
| 233 |
|
| 234 |
# Mostrar los titulares generados
|
| 235 |
if submit:
|
| 236 |
+
# Check if we have valid inputs
|
|
|
|
| 237 |
has_product = product.strip() != ""
|
| 238 |
has_audience = target_audience.strip() != ""
|
| 239 |
|
| 240 |
+
# Valid combination: Product + Audience
|
| 241 |
+
valid_inputs = has_product and has_audience
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
if valid_inputs and selected_formula:
|
| 244 |
try:
|
|
|
|
| 248 |
product,
|
| 249 |
temperature,
|
| 250 |
selected_formula,
|
| 251 |
+
selected_angle
|
|
|
|
|
|
|
|
|
|
| 252 |
)
|
| 253 |
col2.markdown(f"""
|
| 254 |
<div class="results-container">
|
|
|
|
| 262 |
if not selected_formula:
|
| 263 |
col2.error("Por favor, selecciona una fórmula.")
|
| 264 |
else:
|
| 265 |
+
col2.error("Por favor, proporciona el público objetivo y el producto.")
|