from dotenv import load_dotenv import streamlit as st import os import google.generativeai as genai import time import datetime import random # Add this import from ads_formulas import ads_formulas # Import the ads formulas from style import styles from prompts import create_fb_ad_instruction from emotional_angles import emotional_angles from copywriter_personas import copywriter_personas from ad_objectives import ad_objectives # Import ad objectives import PyPDF2 import docx from PIL import Image import io from headline_generator import generate_random_headlines, generate_headlines_with_model, headline_formulas # Import headline_formulas too # Cargar las variables de entorno load_dotenv() # Configurar la API de Google genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) # Función para generar modelo @st.cache_resource def get_model(temperature): generation_config = { "temperature": temperature, } return genai.GenerativeModel('gemini-2.0-flash', generation_config=generation_config) # Function to generate headlines and select one randomly def generate_and_select_headline(target_audience, product, temperature, selected_angle, file_content="", image_parts=None): # Get the model model = get_model(temperature) # Select a random formula for the headline formula_name = random.choice(list(headline_formulas.keys())) selected_formula = headline_formulas[formula_name] # Create instruction with file content if available headline_instruction = f"Generate 3 compelling headlines for {product} targeting {target_audience}. Use this formula: {selected_formula}. Apply this emotional angle: {selected_angle}." # Add file content as context if available if file_content: headline_instruction += f"\n\nUse the following information as additional context:\n{file_content[:5000]}" # Generate headlines with or without image if image_parts: # If we have an image, use it for headline generation response = model.generate_content([headline_instruction, image_parts], generation_config={"temperature": temperature}) headlines = response.parts[0].text if response and response.parts else "" else: # Generate 3 headlines using text only headlines = generate_headlines_with_model( model=model, target_audience=target_audience, product=product, selected_formula=selected_formula, selected_angle=selected_angle, number_of_headlines=3 ) # Parse the headlines (assuming they come in a numbered format) headline_list = [] for line in headlines.strip().split('\n'): if line.strip() and line[0].isdigit() and '.' in line: # Extract the headline text after the number and period headline_text = line.split('.', 1)[1].strip() headline_list.append(headline_text) # If we couldn't parse any headlines, return a default one if not headline_list: return f"Descubre cómo {product} puede transformar tu vida como {target_audience}" # Select a random headline from the generated ones selected_headline = random.choice(headline_list) return selected_headline # Move the clean_ad_text function up with other utility functions # After the generate_headline function and before generate_fb_ad def generate_headline(target_audience=None, product=None, temperature=1.0, angle=None): # Check if UI variables exist, otherwise use parameters try: target_audience = target_audience or ad_target_audience product = product or ad_product temperature = temperature or ad_temperature angle = angle or emotional_angle_key except NameError: # If UI variables don't exist yet, use the provided parameters pass # Obtener el modelo usando la función existente model = get_model(temperature) # Remove duplicate imports and use the ones at the top of the file formula_name = random.choice(list(headline_formulas.keys())) selected_formula = headline_formulas[formula_name] # Generar el titular usando el modelo ya inicializado headline = generate_headlines_with_model( model=model, target_audience=target_audience, product=product, selected_formula=selected_formula, selected_angle=angle ) return headline # Function to clean generated ad text def clean_ad_text(text): """ Cleans the generated ad text by removing unwanted characters and formatting issues. Args: text (str): The raw generated ad text Returns: str: Cleaned ad text """ if not isinstance(text, str): return "" # Split text into lines lines = text.split('\n') # Clean lines cleaned_lines = [] for line in lines: # Skip lines that are just a single letter if len(line.strip()) == 1 and line.strip().isalpha(): continue # Remove triple backticks and language indicators (like ```text) if line.strip().startswith('```'): line = line.replace('```', '').strip() # If line now only contains a language indicator, skip it if line.strip() in ['text', 'markdown', 'md', 'html']: continue # Remove any other markdown artifacts line = line.replace('```', '') cleaned_lines.append(line) # Join lines back together cleaned_text = '\n'.join(cleaned_lines) # Remove any consecutive newlines (more than 2) import re cleaned_text = re.sub(r'\n{3,}', '\n\n', cleaned_text) return cleaned_text.strip() # Modify the generate_fb_ad function to accept a headline parameter def generate_fb_ad(target_audience, product, temperature, selected_formula, selected_angle, selected_persona, story_prompt="", ad_objective=None, file_content="", image_parts=None, max_file_chars=50000, headline=None): if not target_audience or not product: return "Por favor, completa todos los campos requeridos." # Generate a headline if none is provided if not headline: headline = generate_and_select_headline(target_audience, product, temperature, selected_angle) # Enfatizar el tema de la historia si se proporciona emphasized_story_prompt = story_prompt if story_prompt and story_prompt.strip(): emphasized_story_prompt = story_prompt.strip() model = get_model(temperature) # Crear la instrucción base, ahora incluyendo el titular ad_instruction = create_fb_ad_instruction( target_audience, product, selected_formula, selected_angle, selected_persona, ad_objective, language="español", story_prompt=emphasized_story_prompt, headline=headline # Pass the headline to the instruction creator ) # Si hay contenido de archivo, añadirlo a la instrucción if file_content: ad_instruction += f"\n\nAdemás, utiliza la siguiente información como referencia para crear el anuncio:\n\n{file_content[:max_file_chars]}" # Si hay un tema específico, ajustar la temperatura para mayor coherencia effective_temperature = temperature if story_prompt and story_prompt.strip(): effective_temperature = max(0.1, temperature * 0.9) # Generar el contenido con o sin imagen if image_parts: response = model.generate_content([ad_instruction, image_parts], generation_config={"temperature": effective_temperature}) else: response = model.generate_content([ad_instruction], generation_config={"temperature": effective_temperature}) return response.parts[0].text if response and response.parts else "Error generating content." # Configurar la interfaz de usuario con Streamlit st.set_page_config(page_title="CopyLegend AI", layout="wide") # Ocultar el menú de tres puntos de Streamlit hide_menu_style = """ """ st.markdown(hide_menu_style, unsafe_allow_html=True) # 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) # Ocultar elementos de la interfaz st.markdown(styles["main_layout"], unsafe_allow_html=True) # Centrar el título y el subtítulo con el nuevo texto st.markdown("
{cleaned_ad}