Spaces:
Sleeping
Sleeping
File size: 20,843 Bytes
91a0d7e a8fd769 1c11e93 f31888d d51e47b f31888d 3e868ef f31888d 3e868ef f31888d accea52 f31888d 75403c5 05d9ee2 75403c5 432f99e 75403c5 f31888d cd03ba7 75403c5 cd03ba7 accea52 75403c5 accea52 fe8abb4 accea52 fe8abb4 cd03ba7 75403c5 accea52 75403c5 accea52 75403c5 accea52 fe8abb4 accea52 432f99e fe8abb4 cd03ba7 75403c5 accea52 75403c5 accea52 75403c5 accea52 432f99e fe8abb4 cd03ba7 75403c5 432f99e 75403c5 432f99e 75403c5 432f99e fe8abb4 cd03ba7 432f99e cd03ba7 f31888d 520b9e2 f31888d 2bef2ae ea8d9b4 f31888d ea8d9b4 b44807b f31888d b44807b bc96b9a f31888d b44807b f31888d e6db63d fd06656 f31888d b171e09 f31888d e6db63d 875b3a0 241c725 f31888d 432f99e 7992358 f31888d 7992358 f31888d 7992358 f31888d 7992358 f31888d 7992358 432f99e f31888d 875b3a0 f31888d 875b3a0 f31888d 875b3a0 241c725 f31888d 875b3a0 f31888d 875b3a0 f31888d 875b3a0 f31888d ea8d9b4 3f3ea02 f31888d 3f3ea02 762e1e8 3f3ea02 f31888d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
from dotenv import load_dotenv
import os
import google.generativeai as genai
import random
from src.data.story_formulas import story_formulas
class StoryGenerator:
def __init__(self):
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
self.generation_config = {
"temperature": 1.0,
"top_p": 0.65,
"top_k": 360,
"max_output_tokens": 8196,
}
self.model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config=self.generation_config
)
#Función para generar un titular basado en la historia
def generate_headline(self, story, formula_type, product, target_audience, mood, temperature):
"""Función para generar un titular basado en la historia"""
system_prompt = """Eres un experto copywriter especializado en crear titulares persuasivos.
IMPORTANTE:
- Genera SOLO UN TITULAR
- No incluyas explicaciones ni etiquetas
- El titular debe estar entre comillas "titular"
- No uses números ni viñetas
- El titular debe fluir naturalmente con la historia
- El titular DEBE estar directamente relacionado con el contenido de la historia
- Evita titulares genéricos que podrían aplicar a cualquier historia
- Extrae elementos específicos de la historia para crear un titular único
"""
formula_prompts = {
"GPS": f"""
{system_prompt}
INSTRUCCIONES PARA TITULAR GPS:
Crea un titular que combine:
1. Meta: Un resultado deseable para {target_audience}
2. Periodo: Una situación cotidiana o momento del día
3. Superación: Un conector con toque de humor (sin, incluso si, aunque, etc.)
ESTRUCTURA REQUERIDA:
[Meta deseable] + [Momento cotidiano] + [Conector humorístico]
EJEMPLOS EFECTIVOS:
- "Domina el arte de la inversión mientras te cepillas los dientes incluso si confundes Excel con PowerPoint"
- "Aprende un nuevo idioma durante tus visitas al baño aunque solo sepas decir gracias y por favor"
- "Conquista el miedo a hablar en público durante el desayuno a pesar de que te tiemblen hasta las pestañas"
- "Desarrolla músculos preparando café cuando levantar la taza te parece ejercicio extremo"
- "Domina la fotografía profesional en el supermercado aun con ese celular que sobrevivió tres caídas"
VALIDACIONES:
- ¿Incluye una meta clara y deseable?
- ¿Menciona un momento cotidiano específico?
- ¿Usa un conector humorístico natural?
- ¿Mantiene el humor sin ser ridículo?
- ¿Es relevante para {target_audience}?
Historia a titular:
{story}
""",
"AIDA": f"""
{system_prompt}
INSTRUCCIONES PARA TITULAR AIDA:
Crea un titular que combine:
1. Atención: Gancho sorprendente o contraintuitivo
2. Interés: Desarrollo que intriga
3. Deseo: Promesa transformadora
4. Acción: Siguiente paso natural
ESTRUCTURA REQUERIDA:
[Gancho sorprendente] + [Desarrollo intrigante] + [Promesa]
EJEMPLOS EFECTIVOS:
- "¿Sabías que el 83% de los emprendedores exitosos dedican menos de 2 horas al día a reuniones?"
- "La mayoría no sabe que existe una técnica de ventas basada en videojuegos"
- "Contrario a lo que piensas, el momento más productivo del día no es por la mañana"
- "Como el bambú japonés, este método crece invisible hasta explotar en resultados"
- "Los atletas olímpicos entrenan menos horas pero logran más gracias a esta técnica"
VALIDACIONES:
- ¿El gancho genera curiosidad inmediata?
- ¿El desarrollo mantiene el interés?
- ¿La promesa es creíble y atractiva?
- ¿Mantiene coherencia con la historia?
- ¿Es relevante para {target_audience}?
Historia a titular:
{story}
""",
"4U": f"""
{system_prompt}
INSTRUCCIONES PARA TITULAR 4U:
Combina estratégicamente:
1. Útil: Beneficio práctico y tangible
2. Urgente: Motivador de acción inmediata
3. Único: Diferenciador memorable
4. Ultra-específico: Detalles precisos y medibles
ESTRUCTURA REQUERIDA:
[Beneficio útil] + [Elemento único] + [Detalle ultra-específico] + [Urgencia]
EJEMPLOS EFECTIVOS:
- "Aprende 347 palabras en alemán memorizando solo 12 minutos al día mientras cocinas"
- "Cultiva 27 tipos de hierbas aromáticas en 1.5 metros cuadrados de balcón"
- "Automatiza 89% de tus tareas administrativas dedicando 31 minutos cada lunes"
- "La técnica de un cartero rural ayuda a 1893 personas a organizar su tiempo"
- "Un bibliotecario retirado genera 12437 euros vendiendo libros antiguos"
VALIDACIONES:
- ¿Incluye un beneficio claro y medible?
- ¿Tiene un elemento diferenciador único?
- ¿Usa datos específicos y creíbles?
- ¿Genera sensación de urgencia natural?
- ¿Es relevante para {target_audience}?
Historia a titular:
{story}
"""
}
headline_prompt = formula_prompts.get(formula_type, f"""
{system_prompt}
INSTRUCCIONES ESPECÍFICAS:
Genera un titular persuasivo y memorable que:
- Conecte directamente con la historia narrada
- Refleje la transformación o experiencia descrita
- Use elementos específicos mencionados en el contenido
- Mantenga un tono {mood} coherente con la narrativa
- Sea conciso pero impactante
- No mencione directamente "{product}"
- Genere curiosidad mientras mantiene la relevancia
IMPORTANTE:
- El titular debe surgir naturalmente de la historia
- Usa palabras clave y conceptos presentes en la narrativa
- Evita titulares genéricos que podrían aplicar a cualquier historia
Historia a titular:
{story}
""")
response = model.generate_content([headline_prompt])
if response and response.parts:
# Extraer el titular entre comillas si existe
headline_text = response.parts[0].text.strip()
import re
match = re.search(r'"([^"]*)"', headline_text)
if match:
return match.group(1)
return headline_text
return None
#Función unificada para generar historias"""
def generate_story(self, formula_type, target_audience, product, action, mood, length, temperature, story_topic=None):
# Update configuration
self.generation_config["temperature"] = temperature
if formula_type not in story_formulas:
raise ValueError("Fórmula no válida")
# Añadir instrucciones de naturalidad al inicio del prompt
natural_instruction = f"""
IMPORTANTE - NATURALIDAD EN LA HISTORIA:
- NO menciones "{product}" de forma literal o forzada
- Introduce el producto/servicio de manera sutil y orgánica en la narrativa
- Evita que suene como una fórmula publicitaria
- La solución debe surgir naturalmente de la historia
- Usa variaciones y descripciones naturales en lugar del nombre exacto
- La historia debe fluir como una conversación real, no como un anuncio
"""
# Análisis del público objetivo
audience_analysis = f"""
ANÁLISIS DEL PÚBLICO OBJETIVO: {target_audience}
INSTRUCCIONES DE ANÁLISIS:
1. Dolores y Problemas:
- Identifica los problemas cotidianos específicos de {target_audience}
- Reconoce sus frustraciones y miedos más profundos
- Analiza las consecuencias emocionales de estos problemas
- Considera el impacto en su vida diaria
2. Creencias y Valores:
- Comprende sus creencias limitantes
- Identifica sus aspiraciones y sueños
- Reconoce sus valores fundamentales
- Entiende sus motivaciones principales
3. Situaciones de Vida:
- Visualiza sus rutinas diarias
- Identifica momentos de tensión o conflicto
- Reconoce sus contextos sociales y profesionales
- Comprende sus responsabilidades y presiones
4. Puntos de Dolor Específicos:
- Problemas prácticos: [analiza sus dificultades concretas]
- Dolores emocionales: [identifica sus preocupaciones más profundas]
- Miedos: [reconoce sus temores principales]
- Frustraciones: [entiende sus obstáculos recurrentes]
CONTEXTO NARRATIVO:
- Si hay story_topic ("{story_topic if story_topic else 'No especificado'}"),
úsalo como escenario o contexto para desarrollar la historia
- El protagonista debe reflejar fielmente las características de {target_audience}
- Los problemas y situaciones deben ser 100% reconocibles por la audiencia
- La transformación debe abordar directamente sus dolores específicos
"""
# Definir el system prompt dentro de la función
system_prompt = """You are a world-class copywriter, specialized in crafting persuasive stories that emotionally connect with readers and drive them to action.
FORMAT RULES:
- Story must be structured in short paragraphs (2-4 lines maximum)
- Number of paragraphs should adjust to requested length
- Each section must flow naturally into the next
- No explicit section labels
- Include smooth transitions between parts
- Story must feel complete and cohesive
- Break text for better readability and impact
FORMULA APPLICATION:
- The selected formula MUST be applied to the entire story
- Review and follow the structure from story_formulas[formula_type]["description"]
- Use examples in story_formulas[formula_type]["examples"] as inspiration
- Formula provides framework, tone provides emotional layer
- Paragraphs can be more or fewer than formula steps
- Structure should be present but not obvious
CONTENT AND AUDIENCE:
- Focus on relatable, everyday situations
- Describe specific audience problems and obstacles
- Use details that generate immediate identification
- Story must faithfully reflect audience's real life
- Problems and situations must be 100% recognizable
- Base narrative on real audience pain points
- Each story must be unique and memorable
- Avoid clichés and generic scenarios
TONE AND STYLE:
- Mood only affects emotional tone, not structure
- Emotions should arise from real situations
- Keep focus on audience regardless of tone
- Selected tone must remain consistent
- Both formula and tone must complement each other
- Maintain voice appropriate for target audience
SPECIAL CONSIDERATIONS FOR GHA:
- Story must develop around specified topic
- Topic should be the main thread
- Integrate product/service naturally within topic context
- Keep topic as central narrative element
IMPORTANT:
- Each story must be unique and memorable
- Avoid clichés and generic scenarios
- Maintain a credible and authentic voice
- Adapt language to target audience
- Focus on emotional transformation
- Follow formula structure while maintaining selected tone
- Never explicitly label sections
- Integrate product and call-to-action organically
LANGUAGE INSTRUCTIONS:
- Generate the story in Spanish
- Use natural, fluent Spanish
- Maintain cultural relevance for Spanish-speaking audiences
- Ensure idioms and expressions are appropriate for Spanish
- Keep all story content in Spanish, only system instructions in English
CALL TO ACTION GUIDELINES:
In the end of each story create a natural and persuasive call to action that:
Flows naturally from the story
Focuses on transformation and benefits
KEY ELEMENTS IN EVERY CTA:
Transformational Benefit → What will change in their life
Implicit Social Proof → Others have already achieved it
Simple Next Step → A clear and achievable action
Natural Urgency → Based on benefits, not scarcity
IMPORTANT:
The CTA must flow naturally from the story
Maintain the established emotional tone
Use language that inspires rather than pressures
Connect with the transformation described in the story
"""
story_instruction = f"{system_prompt}\n\n{audience_analysis}\n\n"
# Instrucciones específicas para GHA cuando hay story_topic
if formula_type == "GHA" and story_topic:
story_instruction += f"""
INTEGRACIÓN DEL TEMA CENTRAL EN LA HISTORIA:
TEMA PRINCIPAL: "{story_topic}"
1. ESTRUCTURA NARRATIVA:
- Primer párrafo: Introduce el tema "{story_topic}" de forma cautivadora
- Desarrollo: Profundiza en la experiencia/situación relacionada con el tema
- Clímax: Momento de transformación donde el producto se integra naturalmente
- Cierre: Resolución que conecta el tema con la solución propuesta
2. DESARROLLO DEL TEMA:
- Usa el tema como hilo conductor de toda la historia
- Describe situaciones específicas relacionadas con {story_topic}
- Incluye detalles y experiencias únicas del tema
- Mantén el foco en la perspectiva de {target_audience} sobre {story_topic}
3. INTEGRACIÓN DEL PRODUCTO:
- NO menciones {product} hasta haber desarrollado bien el tema
- El producto debe surgir como solución natural a una situación del tema
- Conecta los beneficios de {product} con aspectos específicos de {story_topic}
- Mantén el protagonismo del tema por encima del producto
4. ELEMENTOS OBLIGATORIOS:
- Mínimo 3 referencias específicas a {story_topic}
- Al menos 2 situaciones o momentos relacionados con el tema
- Una conexión clara entre el tema y la necesidad del producto
- Un cierre que refuerce la relación entre tema y solución
5. VALIDACIONES:
- ¿La historia gira realmente en torno a {story_topic}?
- ¿Se mantiene el tema como elemento central?
- ¿La integración del producto es natural dentro del contexto del tema?
- ¿El llamado a la acción conecta con el tema desarrollado?
RECORDATORIO IMPORTANTE:
- La historia debe ser 100% sobre {story_topic}
- El producto es secundario al tema
- Cada párrafo debe contener elementos del tema
- La transformación debe estar directamente relacionada con {story_topic}
"""
# Añadir ejemplos específicos para el tipo de tema
story_instruction += f"""
EJEMPLOS DE INTEGRACIÓN DEL TEMA:
Si el tema es una experiencia personal:
❌ "Un día cualquiera..."
✓ "Aquella mañana en [situación específica del tema]..."
Si el tema es una situación:
❌ "Las personas suelen..."
✓ "En medio de [detalle específico del tema]..."
Si el tema es un concepto:
❌ "Todos queremos mejorar..."
✓ "Cuando te enfrentas a [aspecto específico del tema]..."
ESTRUCTURA DEL PÁRRAFO INICIAL:
1. Situación específica del {story_topic}
2. Conexión emocional con {target_audience}
3. Transición hacia el desarrollo
"""
# Añadir instrucciones sobre el producto y beneficios
story_instruction += f"""
PRODUCT INTEGRATION GUIDELINES:
- Evita menciones obvias de {product}
- Enfócate en despertar interés genuino mostrando beneficios transformacionales
- Integra el producto de manera natural en la narrativa
- Destaca cómo el producto mejora la vida del protagonista
- Mantén el tono {mood} al describir los beneficios
IMPORTANT REMINDER:
- The entire story must be written in Spanish
- Use natural expressions and cultural references appropriate for Spanish-speaking audiences
- Ensure the story flows naturally in Spanish
"""
# Si es GHA y hay story_topic, añadir instrucciones específicas
if formula_type == "GHA" and story_topic:
system_prompt += f"""
INSTRUCCIONES ESPECÍFICAS PARA GHA:
- El titular DEBE incorporar elementos del story_topic: "{story_topic}"
- Mantén el enfoque principal en la transformación o experiencia relacionada con el tema
- Usa palabras clave o conceptos específicos mencionados en la historia
- Asegúrate de que el titular refleje la esencia del story_topic
- La conexión entre el titular y el tema debe ser clara y natural
CALL TO ACTION GUIDELINES:
In the end of each story create a natural and persuasive call to action that:
Flows naturally from the story
Focuses on transformation and benefits
KEY ELEMENTS IN EVERY CTA:
Transformational Benefit → What will change in their life
Implicit Social Proof → Others have already achieved it
Simple Next Step → A clear and achievable action
Natural Urgency → Based on benefits, not scarcity
IMPORTANT:
The CTA must flow naturally from the story
Maintain the established emotional tone
Use language that inspires rather than pressures
Connect with the transformation described in the story
"""
# Instrucciones de estudio de ejemplos
story_instruction += f"""
IMPORTANTE: Estudia cuidadosamente estos ejemplos de la fórmula seleccionada.
Cada ejemplo representa el estilo y estructura a seguir, adaptados al tono {mood}:
"""
# Seleccionar ejemplos aleatorios
random_examples = random.sample(story_formulas[formula_type]['examples'],
min(3, len(story_formulas[formula_type]['examples'])))
# Agregar ejemplos seleccionados
story_instruction += "\nEJEMPLOS DE LA FÓRMULA A SEGUIR:\n"
for i, example in enumerate(random_examples, 1):
story_instruction += f"""
Ejemplo {i}:
Título: {example['title']}
Audiencia: {example['target']}
Acción: {example['action']}
Historia:
{example['story']}
"""
# Agregar instrucciones específicas
story_instruction += """
INSTRUCCIONES ESPECÍFICAS:
1. Mantén la misma estructura y longitud que los ejemplos anteriores
2. Usa el mismo tono y estilo de escritura, adaptado al mood especificado
3. Replica los patrones de construcción de frases y transiciones
4. Conserva el nivel de especificidad y detalle en las descripciones
5. Adapta el contenido para la audiencia manteniendo la esencia de los ejemplos
6. Integra los beneficios del producto de manera sutil y convincente
7. Asegura que la transformación del protagonista sea creíble y emotiva
"""
# Agregar la descripción de la fórmula
story_instruction += f"\nFÓRMULA A SEGUIR:\n{story_formulas[formula_type]['description']}\n\n"
# Recordatorio final
story_instruction += f"""
RECORDATORIO FINAL:
1. Sigue la estructura de la fórmula seleccionada
2. Aplica el tono {mood} de manera consistente
3. Mantén la coherencia narrativa
4. Asegura que la historia refleje una transformación auténtica
5. Integra {product} y sus beneficios de forma natural
6. Culmina con un llamado a la acción convincente: {action}
GENERA AHORA:
Crea una historia persuasiva de {length} palabras en español que siga fielmente el estilo y estructura de los ejemplos mostrados,
manteniendo un tono {mood} y enfocándote en la transformación que {product} puede traer a la vida de {target_audience}.
IMPORTANT FINAL REMINDER:
- Write the complete story in Spanish
- Ensure natural flow and cultural relevance
- Maintain authentic Spanish expressions and tone
"""
# Asegúrate de que las instrucciones de naturalidad estén al inicio del prompt
story_instruction = f"{natural_instruction}\n\n{story_instruction}"
# Generar la historia
response = self.model.generate_content([story_instruction])
if response and response.parts:
story = response.parts[0].text.strip()
formula_types = ["GPS", "AIDA", "4U", "Númerica Suprema"]
selected_formula = random.choice(formula_types)
headline = generate_headline_for_story(story, selected_formula, product, target_audience, mood, temperature)
if headline:
return f"{headline}\n\n{story}"
raise ValueError("No se pudo generar la historia") |