from dotenv import load_dotenv import streamlit as st import os import google.generativeai as genai import random from streamlit import session_state as state from formulas import email_formulas # Updated import statement from angles import angles # Cargar las variables de entorno load_dotenv() # Configurar la API de Google genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) # Fórmulas con ejemplos y explicaciones # email_formulas dictionary has been moved to formulas/email_formulas.py # Cambiar el nombre de la función def generate_emails(number_of_emails, target_audience, product, temperature, selected_formula, selected_angle, file_content="", image_parts=None, is_image=False, emotion="", desired_action=""): # Crear la configuración del modelo generation_config = { "temperature": temperature, "top_p": 0.65, "top_k": 360, "max_output_tokens": 8196, } model = genai.GenerativeModel( model_name="gemini-2.0-flash", generation_config=generation_config, ) # Angle dictionaries have been moved to angles/angle_data.py # Incluir las instrucciones del sistema en el prompt principal system_prompt = f"""You are a world-class direct response copywriter trained by Gary Halbert, Gary Bencivenga, and David Ogilvy. You have helped many marketers before me persuade their clients through emotional email sequences. Your task is to create email sequences that make my [buyer persona] feel [emotion] about my [product/service] and convince them to register/take [desired action]. FORMAT RULES: - Each email must have a clear and attractive subject line - Include personalized greeting - The email body must be persuasive and emotional - Include a clear call to action - Add a professional signature - Separate each email with a dividing line IMPORTANT ANGLE INSTRUCTIONS: - The selected angle MUST be applied to EACH email - The angle modifies HOW the message is expressed, not its structure - Think of the angle as a "tone overlay" on the content - The formula provides the structure, the angle provides the style - Both must work together seamlessly FORMAT EXAMPLE: --- SUBJECT: [Attractive subject line] [Email body with persuasive and emotional content] [Clear call to action] [Signature] --- IMPORTANT: - Each email must be unique and memorable - Avoid clichés and generalities - Maintain a persuasive but credible tone - Adapt language to the target audience - Focus on transformative benefits - Follow the selected angle style while maintaining the structure""" # Iniciar el prompt con las instrucciones del sistema email_instruction = f"{system_prompt}\n\n" # Añadir contenido del archivo si existe if file_content: email_instruction += f""" REFERENCE CONTENT: Carefully analyze the following content as a reference for generating emails: {file_content[:3000]} ANALYSIS INSTRUCTIONS: 1. Extract key information about the product or service mentioned 2. Identify the tone, style, and language used 3. Detect any data about the target audience or customer avatar 4. Look for benefits, features, or pain points mentioned 5. Use relevant terms, phrases, or concepts from the content 6. Maintain consistency with the brand identity or main message 7. Adapt the emails to resonate with the provided content IMPORTANT COMBINATIONS: """ # Updated conditions for specific input combinations if product and not target_audience: email_instruction += f"""- FILE + PRODUCT: You have a reference document and product ({product}). Create emails that highlight this specific product's benefits and features using insights from the document. Extract audience information from the document to better target the emails. """ elif target_audience and not product: email_instruction += f"""- FILE + TARGET AUDIENCE: You have a reference document and target audience ({target_audience}). Create emails 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. """ elif product and target_audience: email_instruction += f"""- PRODUCT + TARGET AUDIENCE: You have both product ({product}) and target audience ({target_audience}). Create emails that connect this specific product with this specific audience, using insights from the document to strengthen the connection. """ email_instruction += """ IMPORTANT: Naturally integrate the elements found in the content with the selected formula and angle. """ # Añadir instrucciones de ángulo solo si no es "NINGUNO" if selected_angle != "NINGUNO": email_instruction += f""" MAIN ANGLE: {selected_angle} SPECIFIC ANGLE INSTRUCTIONS: {angles[selected_angle]["instruction"]} IMPORTANT: The angle {selected_angle} must be applied as a "style layer" over the formula structure: 1. Keep the base structure of the formula intact 2. Apply the tone and style of the {selected_angle} angle 3. Ensure each element of the formula reflects the angle 4. The angle affects "how" it's said, not "what" is said SUCCESSFUL EXAMPLES OF THE {selected_angle} ANGLE: """ for example in angles[selected_angle]["examples"]: email_instruction += f"- {example}\n" # Dentro de la función, actualizar el prompt para incluir emoción y acción deseada email_instruction += ( f"\nYour task is to create {number_of_emails} persuasive emails for {target_audience} " f"that evoke {emotion} and convince them to {desired_action} about {product}. " ) if selected_angle != "NINGUNO": email_instruction += f"IMPORTANT: Each email MUST follow the {selected_angle} angle clearly and consistently.\n\n" email_instruction += ( f"Avoid obvious mentions of {product} and focus on generating genuine interest" ) if selected_angle != "NINGUNO": email_instruction += f" using the selected angle" email_instruction += ".\n\n" email_instruction += ( f"IMPORTANT: Carefully study these examples of the selected formula. " f"Each example represents the style and structure to follow" ) if selected_angle != "NINGUNO": email_instruction += f", adapted to the {selected_angle} angle" email_instruction += ":\n\n" # Agregar 5 ejemplos aleatorios de la fórmula random_examples = random.sample(selected_formula['examples'], min(5, len(selected_formula['examples']))) email_instruction += "FORMULA EXAMPLES TO FOLLOW:\n" for i, example in enumerate(random_examples, 1): email_instruction += f"{i}. {example}\n" email_instruction += "\nSPECIFIC INSTRUCTIONS:\n" email_instruction += "1. Maintain the same structure and length as the previous examples\n" email_instruction += "2. Use the same tone and writing style\n" email_instruction += "3. Replicate the phrase construction patterns\n" email_instruction += "4. Preserve the level of specificity and detail\n" email_instruction += f"5. Adapt the content for {target_audience} while maintaining the essence of the examples\n\n" email_instruction += f"FORMULA TO FOLLOW:\n{selected_formula['description']}\n\n" # CORRECTO (con indentación): if selected_angle != "NINGUNO": email_instruction += f""" FINAL REMINDER: 1. Follow the structure of the selected formula 2. Apply the angle as a "style layer" 3. Maintain coherence between formula and angle 4. Ensure each email reflects both elements GENERATE NOW: Create {number_of_emails} emails that faithfully follow the style and structure of the examples shown. """ else: email_instruction += f""" GENERATE NOW: Create {number_of_emails} emails that faithfully follow the style and structure of the examples shown. """ # Modificar la forma de enviar el mensaje según si hay imagen o no message_parts = [email_instruction] # Add the image to the message parts if it exists if is_image and image_parts: message_parts.append(image_parts) instruction_text = "Generate the emails in Spanish following exactly the style of the examples shown, drawing inspiration from the provided image." else: instruction_text = "Generate the emails in Spanish following exactly the style of the examples shown." # Common instruction for both cases instruction_text += " Do not include explanations, only the emails. IMPORTANT: Do not include greetings like 'Hello [Name]' and make sure that the postscripts (P.D.) are smaller and more discrete than the main body of the email, using a lighter format." # Create the chat session with the message parts chat_session = model.start_chat( history=[ { "role": "user", "parts": message_parts, }, ] ) # Enviar el mensaje con las instrucciones response = chat_session.send_message(instruction_text) return response.text # Configurar la interfaz de usuario con Streamlit st.set_page_config(page_title="Email Composer", layout="wide") # Leer el contenido del archivo manual.md with open("manual.md", "r", encoding="utf-8") as file: manual_content = file.read() # Mostrar el contenido del manual en el sidebar st.sidebar.markdown(manual_content) # Load CSS from file with open("styles/main.css", "r") as f: css = f.read() # Apply the CSS st.markdown(f"", unsafe_allow_html=True) # Centrar el título y el subtítulo st.markdown("

Generador de Emails

", unsafe_allow_html=True) st.markdown("

Transforma tu marketing con emails persuasivos que convierten. Esta aplicación es tu arma secreta para crear emails emocionales de respuesta directa que impulsan a la acción.

", unsafe_allow_html=True) # Crear columnas col1, col2 = st.columns([1, 2]) # Columnas de entrada with col1: target_audience = st.text_input("¿Quién es tu público objetivo?", placeholder="Ejemplo: Estudiantes Universitarios") product = st.text_input("¿Qué producto/servicio estás promocionando?", placeholder="Ejemplo: Curso de Inglés") number_of_emails = st.selectbox("Número de Emails", options=[1, 2, 3, 4, 5], index=2) # Moved the submit button here, right after number_of_emails submit = st.button("Generar Emails") # Crear un único acordeón para fórmula, creatividad y ángulo with st.expander("Personaliza tus emails"): temperature = st.slider("Creatividad", min_value=0.0, max_value=2.0, value=1.0, step=0.1) emotion = st.selectbox( "¿Qué emoción quieres evocar?", options=["Curiosidad", "Miedo", "Esperanza", "Entusiasmo", "Confianza", "Urgencia"] ) desired_action = st.text_input("Acción deseada", placeholder="Ejemplo: Registrarse para una prueba gratuita") selected_formula_key = st.selectbox( "Selecciona una fórmula para tus emails", options=list(email_formulas.email_formulas.keys()) # Updated reference ) # Automatically use the keys from the angles dictionary # Make sure "NINGUNO" appears first, then the rest alphabetically angle_keys = ["NINGUNO"] + sorted([key for key in angles.keys() if key != "NINGUNO"]) selected_angle = st.selectbox( "Selecciona un ángulo para tus emails", options=angle_keys ) # Añadir cargador de archivos dentro del acordeón uploaded_file = st.file_uploader("📄 Archivo o imagen de referencia", type=['txt', 'pdf', 'docx', 'jpg', 'jpeg', 'png']) file_content = "" is_image = False image_parts = None if uploaded_file is not None: file_type = uploaded_file.name.split('.')[-1].lower() # Manejar archivos de texto if file_type in ['txt', 'pdf', 'docx']: if file_type == 'txt': try: file_content = uploaded_file.read().decode('utf-8') st.success(f"Archivo TXT cargado correctamente: {uploaded_file.name}") except Exception as e: st.error(f"Error al leer el archivo TXT: {str(e)}") file_content = "" elif file_type == 'pdf': try: import PyPDF2 pdf_reader = PyPDF2.PdfReader(uploaded_file) file_content = "" for page in pdf_reader.pages: file_content += page.extract_text() + "\n" st.success(f"Archivo PDF cargado correctamente: {uploaded_file.name}") except Exception as e: st.error(f"Error al leer el archivo PDF: {str(e)}") file_content = "" elif file_type == 'docx': try: import docx doc = docx.Document(uploaded_file) file_content = "\n".join([para.text for para in doc.paragraphs]) st.success(f"Archivo DOCX cargado correctamente: {uploaded_file.name}") except Exception as e: st.error(f"Error al leer el archivo DOCX: {str(e)}") file_content = "" # Manejar archivos de imagen elif file_type in ['jpg', 'jpeg', 'png']: try: from PIL import Image image = Image.open(uploaded_file) image_bytes = uploaded_file.getvalue() image_parts = { "mime_type": uploaded_file.type, "data": image_bytes } is_image = True st.image(image, caption="Imagen cargada", use_column_width=True) except Exception as e: st.error(f"Error processing image: {str(e)}") is_image = False selected_formula = email_formulas.email_formulas[selected_formula_key] # Updated reference # Removed the submit button from here # Mostrar los emails generados if submit: # Check if we have a valid combination of inputs has_file = 'file_content' in locals() and file_content.strip() != "" has_product = product.strip() != "" has_audience = target_audience.strip() != "" has_emotion = 'emotion' in locals() and emotion.strip() != "" has_action = 'desired_action' in locals() and desired_action.strip() != "" # Valid combinations: # 1. File + Product (no audience needed) # 2. File + Audience (no product needed) # 3. Product + Audience (traditional way) valid_inputs = ( (has_file and has_product) or (has_file and has_audience) or (has_product and has_audience and has_emotion and has_action) ) if valid_inputs and selected_formula: try: # Update the function call to include emotion and desired_action generated_emails = generate_emails( number_of_emails, target_audience, product, temperature, selected_formula, selected_angle, file_content if 'file_content' in locals() else "", image_parts if 'image_parts' in locals() else None, is_image if 'is_image' in locals() else False, emotion, desired_action ) # Remove the inline CSS since we've moved it to main.css col2.markdown(f"""

Tus emails persuasivos:

{generated_emails}

""", unsafe_allow_html=True) except ValueError as e: col2.error(f"Error: {str(e)}") else: if not selected_formula: col2.error("Por favor selecciona una fórmula.") elif not (has_emotion and has_action): col2.error("Por favor especifica la emoción que quieres evocar y la acción deseada.") else: col2.error("Por favor proporciona al menos una de estas combinaciones: archivo + producto, archivo + público objetivo, o producto + público objetivo + emoción + acción deseada.")